First in First out page replacement algorithm

page replacement algorithm:-
In a  computer operating system that uses paging for virtual memory managementpage replacement algorithms decide which memory pages to page out (swap out, write to disk) when a page of memory needs to be allocated. Paging happens when a page fault occurs and a free page cannot be used to satisfy the allocation, either because there are none, or because the number of free pages is lower than some threshold.
 Click here for First in First out page replacement algorithm
PROGRAM:-

#include<unistd.h>
#include<stdio.h>
int main()
 {
int tn;  // tn: total numbe rof pages
int fs;  // fs: frame size
int cnt; // to count no of page faults
int i,j,s,ss,pg;
int pr[20]; // page name
int fm[10]; //frame
printf("Enter number of pages    :   ");


scanf("%d",&tn);
printf("Enter size of frame  :   ");
scanf("%d",&fs);
printf("Enter page name :\n");
for(i=0;i<tn;i++)
{
printf("\t");
scanf("%d",&pr[i]);
 }
s=0;ss=0;
for(i=0;i<fs;i++)
{  
  fm[i]=-1;
 }
for(i=0;i<tn;i++)
 {
printf("\n page :   %d",pr[i]);
if(i<fs)
  {
pg=0;
for(j=0;j<fs;j++)
{
if(fm[j]==pr[i])  pg++;
}
if(pg==0)
{
fm[s]=pr[i]; s=(s+1)%fs; cnt++;
}
   }
else
   {   pg=0;
    for(j=0;j<fs;j++)
{
if(fm[j]==pr[i])  pg++;
}
if(pg==0)
   { fm[ss]=pr[i]; cnt++; ss=(ss+1)%fs; }
    }
printf("\n\t\t\t frame:");
for(j=0;j<fs;j++)
   {  if(fm[j]>-1)
printf("\t%d",fm[j]);
            }
}

printf("\n page fault :  %d\n",cnt);
 }

Output:-



Comments