FIFO page replacement algorithm in C with output of program

The operating system uses the method of paging for memory management. This method involves various algorithms known as page replacements algorithms that decide which page should be replaced by a new one on demand. A demand is made when the operating system needs a page for processing which is not contained in the main memory. Such situations are termed as page faults.

At such times, a page from secondary memory is replaced with the existing page from the main memory. The method used here is FIFO , First In First Out . This method is one of the simplest methods in which the operating system maintains all the pages in a queue. The oldest pages are kept in the front and the latest ones at the back. When a page fault occurs, the oldest pages are removed first and the latest ones are added.

Let’s see how the algorithm works. The steps are as follows:

FIFO page replacement algorithm (Working)

  • Start the process and traverse through the pages.
  • Declare the size with respect to the page length
  • Check if there’s a need for replacement of a page from memory.
  • Check the need of replacement from old page to new page in memory
  • Form a queue to hold all pages
  • Insert the page require memory into the queue
  • Check for bad replacement and page fault
  • Get the number of processes to be inserted
  • Display the values
  • End the process

Hope the algorithm was clear. Let’s get into the coding part now.

FIFO page replacement program in C

#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
            printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
            printf("\n ENTER THE PAGE NUMBER :\n");
            for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
            printf("\n ENTER THE NUMBER OF FRAMES :");
            scanf("%d",&no);
for(i=0;i<no;i++)
            frame[i]= -1;
                        j=0;
                        printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
                        {
                                    printf("%d\t\t",a[i]);
                                    avail=0;
                                    for(k=0;k<no;k++)
if(frame[k]==a[i])
                                                avail=1;
                                    if (avail==0)
                                    {
                                                frame[j]=a[i];
                                                j=(j+1)%no;
                                                count++;
                                                for(k=0;k<no;k++)
                                                printf("%d\t",frame[k]);
}
                                    printf("\n");
}
                        printf("Page Fault Is %d",count);
                        return 0;
}

Output of Program

 
ENTER THE NUMBER OF PAGES:  20
ENTER THE PAGE NUMBER :       7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
ENTER THE NUMBER OF FRAMES :3
      ref string       page frames
7               7       -1      -1
0               7       0       -1
1               7       0       1
2               2       0       1
0
3               2       3       1
0               2       3       0
4               4       3       0
2               4       2       0
3               4       2       3
0               0       2       3
3
2
1               0       1       3
2               0       1       2
0
1
7               7       1       2
0               7       0       2
1               7       0       1
Page Fault Is 15

Hope you learnt something useful from the tutorial.

Leave a Comment