Monday, November 26, 2012

Page Replacement algorithm using LRU.

/* Simulate the page replacement algorithm using LRU.                  */

#include<stdio.h>
#include<conio.h>
void main()
{
    int ref[50],i,j,k,fault=0,count=0,frame[5],n;
    int temp,m;
    clrscr();
    printf("\n Enter the no. of Frames :;");
    scanf("%d",&n);
    printf("\nEnter the reference string and end with -1 ::");
    scanf("%d",&temp);
    while(temp!=-1)
    {
        ref[count++]=temp;
        scanf("%d",&temp);
    }
    for(i=0;i<n;i++)
    {
        frame[i]=ref[i];
        fault++;
        printf("\n\n After inserting %2d the frame status is ::",ref[i]);
        for(j=0;j<=i;j++)
            printf("%4d",frame[j]);
    }
    for(i=n;i<count;i++)
    {
        for(j=0;j<n;j++)
        {
            if(frame[j]==ref[i])
                break;
            else
            {
                if(frame[j]==ref[i-n])
                {
                    frame[j]=ref[i];
                    fault++;
                }
            }
        }
        printf("\n\n After inserting %2d the frame status is ::",ref[i]);
        for(k=0;k<n;k++)
            printf("%4d",frame[k]);
    }
    printf("\n\n Total no. of page faults ::%d",fault);
    getch();
}
/* Input and Output :-

 Enter the no. of Frames :;3

 Enter the reference string and end with -1 ::1 2 3 4 1 2 5 1 2 3 4 5 -1

 After inserting  1 the frame status is ::   1

 After inserting  2 the frame status is ::   1   2

 After inserting  3 the frame status is ::   1   2   3

 After inserting  4 the frame status is ::   4   2   3

 After inserting  1 the frame status is ::   4   1   3

 After inserting  2 the frame status is ::   4   1   2

 After inserting  5 the frame status is ::   5   1   2

 After inserting  1 the frame status is ::   5   1   2

 After inserting  2 the frame status is ::   5   1   2

 After inserting  3 the frame status is ::   3   1   2

 After inserting  4 the frame status is ::   3   4   2

 After inserting  5 the frame status is ::   3   4   5

 Total no. of page faults ::10                    */











                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               

No comments:

Post a Comment