6/recent/ticker-posts

First In First Out (FIFO) 2

FIFO is the simplest technique for replacing pages in a frame. It decides when a page fault occurs then which frames are to be replaced.  Create a program to counts the number of page fault occurs when an input reference string is given.  Initially, all the frames contain default value as -1. The input contains integer values for a number of pages in a queue, sequence of reference strings and frame size. The output should display the reference string given, pages in frames and the total number of page faults.


Note: Text in Bold corresponds to the input 
  For spacing between Ref.string and PageFrames use ("%s%40s\n","Ref.string","PageFrames") and space between 1 (Ref.string) and 1(PageFrames) use ("%45s",""). To display pages inside a frame use additional spacing ("%10d",frame) between pages.

Sample Input and Output
Enter the number of pages:
6

Enter the reference string:
1
2
2
3
1
4


Enter frame size:
3
Ref.string                              PageFrames
1                                                      1        -1        -1
2                                                      1         2        -1
2
3                                                      1         2         3
1
4                                                      4         2         3
Total Page Fault is 4


Answer:

#include<stdio.h>
int main()
{
    int n,pages,frames,m,i,s,pagef=0;
    printf("Enter the number of pages:\n");
    scanf("%d",&pages);
    int r_arr[pages];
    printf("Enter the reference string:\n");
    for(m=0;m<pages;m++){
        scanf("%d",&r_arr[m]);
    }
    printf("Enter frame size:\n");
    scanf("%d",&frames);
    int temp[frames];
    for(i=0;i<frames;i++)
    {
        temp[i]=-1;
    }
    printf("%s%40s\n","Ref.string","PageFrames");
    for(m=0;m<pages;m++){
        printf("%d%45s",r_arr[m],"");
        s=0;
        for(i=0;i<frames;i++)
        {
            if(r_arr[m]==temp[i]){
                s++;
                pagef--;
            }
        }
        pagef++;
        if((pagef<=frames) && s==0){
            temp[pagef-1]=r_arr[m];
            for(n=0;n<frames;n++){
                printf("%10d",temp[n]);
            }
        }
        else if(s==0){
            temp[(pagef-1)%frames]=r_arr[m];
            for(n=0;n<frames;n++)
            {
                printf("%10d",temp[n]);
            }
        }
        printf("\n");
    }
    printf("Total Page Fault is %d",pagef);
}



Post a Comment

0 Comments