Monday, November 26, 2012

Page Replacement algorithm using FIFO.

/* Simulate the page replacement algorith using FIFO.                */

#include<stdio.h>
#include<conio.h>
void main()
{
    int ref[50],i,j,fault=0,count=0,frame[5],n;
    int temp;
    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]=-1;
    for(i=0;i<count;i++)
    {
        for(j=0;j<n;j++)
            if(frame[j]==ref[i])
                break;
        if(j==n)
        {
            frame[fault%n]=ref[i];
            fault++;
        }
        printf("\n\n After inserting %2d the Frame status is ::",ref[i]);
        for(j=0;j<n;j++)
            printf("%4d",frame[j]);
    }
    printf("\n\n\t 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  -1  -1

 After inserting  2 the Frame status is ::   1   2  -1

 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 ::   5   3   2

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

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

     Total no. of page faults ::9                    */












                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               

No comments:

Post a Comment