0% found this document useful (0 votes)
34 views

Lru Fif

This document contains code implementing the FIFO and LRU page replacement algorithms. The FIFO code tracks page faults as it iterates through a sample page stream, replacing the oldest page on a frame when needed. The LRU code similarly tracks faults, but replaces the least recently used page based on its position in the stream. Both outputs show the frame contents at each step and total faults.

Uploaded by

manasa16cse
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Lru Fif

This document contains code implementing the FIFO and LRU page replacement algorithms. The FIFO code tracks page faults as it iterates through a sample page stream, replacing the oldest page on a frame when needed. The LRU code similarly tracks faults, but replaces the least recently used page based on its position in the stream. Both outputs show the frame contents at each step and total faults.

Uploaded by

manasa16cse
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

/*FIFO */

#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,p[20],f[20],count=0,i,j,k,l;
clrscr();
printf(“\n enter number of frames:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
f[i]=0;
printf(“\n enter the size of page stream:”);
scanf(“%d”,&m);
printf(“enter the page stream:”);
for(i=0;i<m;i++)
scanf(“%d”,&p[i]);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
if(p[i]==f[j]);
break;
if(j==n)
{
count++;
for(k=0;k<n;k++)
if(f[k]==0)
{
f[k]=p[i];
break;
}
if(k==n)
{
for(l=0;l<n-1;l++)
f[l]=f[l+1];
f[n-1]=p[i];
}
}
for(j=0;j<n;j++)
printf(“%5d”,f[j]);
printf(“\n\n”);
}
printf(“\n number of page faults=%d”,count);
getch();
}

Output:

enter number of frames:3


enter size of page stream:12
enter the page stream: 2 3 2 1 5 2 4 5 3 2 5 2
200
230
230
231
315
152
524
243
243
435
352

number of page faults=9


/*LRU */

#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,p[20],f[20],pf=0,i,j,k,l,rear=0;
int lru, flru;
clrscr();
printf(“\n enter number of frames:”);
scanf(“%d”,&n);
printf(“\n enter the size of page stream:”);
scanf(“%d”,&m);
printf(“enter the page stream:”);
for(i=0;i<m;i++)
scanf(“%d”,&p[i]);
for(i=0;i<m;i++)
{
for(j=0;j<rear;j++)
{
if(f[j]==p[i])
break;
}
if(j==rear)
{
if(rear<n)
{
f[rear]=p[i];
rear++;
pf++;
}
else
{
pf++;
lru=m;
for(k=0;k<n;k++)
{
for(l=i-1;l>=0;l++)
{
if(f[k]==p[l])
break;
}
if(l<lru)
{
lru=l;
flru=k;
}
}
f[flru]=p[i];
}
f[flru]=p[i];
}
for(j=0;j<rear;j++)
printf(“%5d”,f[j]);
printf(“\n\n”);
}
printf(“\n number of page faults=%d”,pf);
getch();
}

Output:

enter number of frames:3


enter size of page stream:12
enter the page stream: 2 3 2 1 5 2 4 5 3 2 5 2
200
230
231
251
251
254
254
354
352
352
352

number of page faults=7

You might also like