Pages

Thursday 14 February 2019

Page Replacement Algorithms - FIFO

Page Replacement Algorithms - FIFO

When a page fault occurs, the operating system has to choose a page to remove from memory to make room for the page that has to be brought in. The page replacement is done by swapping the required pages from backup storage to main memory and vice-versa. 

The page replacement algorithm is evaluated by running the particular algorithm on a string of memory references and compute the page faults.Referenced string is a sequence of pages being referenced.

Page faults are not errors and are common and necessary to increase the amount of memory available to programs in any operating system that utilizes virtual memory, including Microsoft Windows, Mac OS X, Linux and Unix.

Each operating system uses different page replacement algorithms. To select the particular algorithm, the algorithm with lowest page fault rate is considered.

1.First-In, First-Out page replacement

2.Least recently used page replacement

3.Optimal page replacement algorithm

 Program on FIFO( First-In, First-Out ) page replacement

//Program to illustrate First In First Come Page Replacement Alogarthim
#include<stdio.h>
void main()
{
 int frames[10],n,seq[30],c=0,i,j,n1,x=0,m,k,flag=0,l;
 printf("\n enter the number of pages : ");
 scanf("%d",&n1);
 printf("\n enter the pages : ");
 for(i=0;i<n1;i++)
   scanf("%d",&seq[i]);
 printf("enter the number of frames : ");
   scanf("%d",&n);
 for(i=0;i<n;i++)
   frames[i]=-1;
 for(x=0,i=0;x<n;i++)
 {
   for(j=0;j<n;j++)
   {
     flag=0;
     if(seq[i]==frames[j])
     {
          flag=1;
          break;
     }
   }
 
    if(flag==0)
   {
     frames[x]=seq[i];
     m=i;
     x++;
     c=c+1;
     printf("\n\n");
    for(k=0;k<x;k++)
      printf("\t %d",frames[k]);
   }
  }
  k=0;
  for(i=m+1;i<n1;i++)
  {
   if(k==n)
     k=0;
   for(j=0;j<n;j++)
   {
    flag=0;
    if(seq[i]==frames[j])
    {
      flag=1;break;
    }
   }
   if(flag==0)
   {
    frames[k]=seq[i];
    k++;
    c=c+1;
    printf("\n \n");
    for(l=0;l<n;l++)
     printf("\t %d",frames[l]);
   }
  }
  printf("\n \n the no of page faults = %d",c);
 }

output:

 

No comments:

Post a Comment