Program 13

:
AIM:
Write a C program for LRU page replacement algorithm.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int a[50],b[40],c[15],n,f,i,y,s,p=0,k=0,l=0,m,x,g;
printf("\nenter no of requests");
scanf("%d",&n);
printf("\nenter no of frames");
scanf("%d",&f);
printf("enter requests one by one");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(k<f)
{
for(x=0;x<k;x++)
if(b[x]==a[i])
break;
if(x==k)
{
b[k]=a[i];
c[k]=a[i];
k++;
m=i;
}
}
}
for(i=m;i<n;i++)
{
printf("%d",a[i]);
for(x=0;x<f;x++)
printf("%4d",b[x]);
printf("---------");
for(x=0;x<f;x++)
if(b[x]==a[i])
break;
if(x==f)
{
p++;
l=0;
for(g=i-1;g>0;g--)
11

b[s]).s++) if(c[s]!=-1) { b[s]=a[i].s<f.s++) c[s]=b[s]. } for(s=0. } for(s=0.x++) if(a[g]==b[x]&&c[x]==b[x]) { y=c[x]. break.x<f&&l<=f. getch().p).s<f.s<f. } printf("\n no of pagefaults=%d". } 22 .s++) if(y==b[s]) b[s]=a[i]. printf("\n"). l++.s++) printf("%4d".for(x=0. } if(l<f) for(s=0. c[x]=-1. } if(l==f) { for(s=0.s<f.

Input: enter no of requests 13 enter no of frames 3 enter requests one by one 2 5 3 2 0 1 3 5 4 2 2 3 4 Output: 3 2 5 3 -----------2 2 5 3 -----------0 2 5 3 -----------1 2 0 3 -----------3 2 0 1 -----------5 2 0 1 -----------4 2 5 1 -----------2 2 5 4 -----------2 2 5 4 -----------3 2 5 4 -----------4 2 3 4 -----------No of pagefaults=7 2 2 2 2 2 2 2 2 2 2 2 5 5 0 0 0 5 5 5 5 3 3 3 3 3 1 1 1 4 4 4 4 4 33 .

Sign up to vote on this title
UsefulNot useful