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

LRUPageReplacementAlgorithm

This document contains a C program implementing the Least Recently Used (LRU) page replacement algorithm. It allows users to input the number of frames and a reference string, then calculates and displays the number of page faults that occur during the execution of the algorithm. The program uses an array to track recent page references and determines which page to replace when a page fault occurs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

LRUPageReplacementAlgorithm

This document contains a C program implementing the Least Recently Used (LRU) page replacement algorithm. It allows users to input the number of frames and a reference string, then calculates and displays the number of page faults that occur during the execution of the algorithm. The program uses an array to track recent page references and determines which page to replace when a page fault occurs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*LRUPageReplacementAlgorithm*/

#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
printf("\n\t\t\tLRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames. . .");
scanf("%d",&nof);
printf("Enter no.of reference string..");
scanf("%d",&nor);
printf("\nEnter reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\n\t\tLRU PAGE REPLACEMENT ALGORITHM");
printf("\n\tThe given reference string:");
printf("\n…..............................................");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0; printf("\
n"); for(i=0;i<nor;i+
+)
{
flag=0;
printf("\n\tReferenceNO%d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else

Page31
vict
im
=lr
uvi
cti
m()
;
pf+
+;
frm
[vi
cti
m]
=re
f[i]
;
for(
j=0
;j<
nof
;j+
+)
pri
ntf(
"%
4d"
,fr
m[j
]);
}
recent[ref[i]]=i;
}
printf("\n\n\tNo.of page faults...%d",pf);
}
int lruvictim()
{
i
n
t
i,
j,
t
e
m
p
1
,t
e
m
p
2
;
f
o
r
(i
=
0
;i
<
n
o
f;
i
+
+
)
{
temp1=frm[
i];
lrucal[i]=rec
ent[temp1];
}
temp2=lrucal
[0];
for(j=1;j<nof;
j++)
{
if(temp2>lr
ucal[j])
temp2=lruca
l[j];
}
for(i
=0;i
<nof
;i++)
if(ref
[tem
p2]=
=frm
[i])
retur
n i;
return 0;
}

You might also like