0% found this document useful (0 votes)
9 views3 pages

C Program for FIFO and LRU Page Replacement

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

C Program for FIFO and LRU Page Replacement

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROFRAM 7: Develop a C program to simulate page replacement algorithms:

a) FIFO b) LRU

a)FIFO
#include<stdio.h>
main()
{
int i,j,k,f,pf=0,count=0,rs[25],m[10],n;
printf("\nEnter the length of reference string:");
scanf("%d",&n);
printf("\nEnter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("\nEnter [Link] frames:");
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
printf("\nThe Page Replacement Process is...\n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
break;
}
if(k==f)
{
m[count++]=rs[i];
pf++;
}
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPFNo.%d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("\nThe number of Page Faults using FIFO are %d",pf);
}
b)LRU
#include<stdio.h>
main()
{
int i,j ,k,min,rs[25],m[10],count[10],flag[25],n,f,pf=0,next=1;
printf("Enter the length of reference string:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter thenumber of frames:");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement process is...\n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
count[i]=next;
next++;
}
else
{
min=0;
for(j=1;j<f;j++)
if(count[min]>count[j])
min=j;
m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}

for(j=0;j<f;j++)
printf("%d\t",m[j]);
if(flag[i]==0)
printf("PFNo.--%d",pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf);
}

You might also like