# LRU Page Replacement Algorithm in C

Here you will get program for lru page replacement algorithm in C.

Least Recently Used (LRU) page replacement algorithm works on the concept that the pages that are heavily used in previous instructions are likely to be used heavily in next instructions. And the page that are used very less are likely to be used less in future. Whenever a page fault occurs, the page that is least recently used is removed from the memory frames. Page fault occurs when a referenced page in not found in the memory frames.

Also Read: LRU Page Replacement Algorithm in C

Below program shows how to implement this algorithm in C.

## Program for LRU Page Replacement Algorithm in C

Output

Enter number of frames: 3
Enter number of pages: 6
Enter reference string: 5 7 5 6 7 3

5 -1 -1
5 7 -1
5 7 -1
5 7 6
5 7 6
3 7 6

Total Page Faults = 4

Video Tutorial

Comment below if you have doubts or found anything incorrect in above lru page replacement algorithm in C.

Category: Algorithm A crazy computer and programming lover. He spend most of his time in programming, blogging and helping other programming geeks.

## 17 thoughts on “LRU Page Replacement Algorithm in C”

1. biky

plz execute in manually the programming code..

2. nushaba

can u please explain why `int time’ is for?

1. Mahaboob

For to store,how many times that particular page has been repeated

3. Angara sruthi

It is not executing

1. Admin Post author

what problem you are getting?

1. meenal

For what purpose we are using flag1 and flag2

1. Vrushant domde

Under biggest for loop logic there are three conditions are there first one for checking page is is in frame or not second one is inserting the page in in frame where -1 are there, the third one for finding the lru
So if the first condition occurred then flag 1 flag 2 to become one then the control cannot go to the second or third condition ok.
If we are in second condition when the flag 1 and flag 2 is 0 , so after executing second condition flag 2 become 0. So that we cannot go to the 3rd condition.
If first and second condition not execute then we can go to the third condition.
Conclusion flag 1 and flag 2 used for executing any one condition out of 3rd condition.

2. udit

dont no but the page faults are not showing in the output and no any error

4. I love myself for this

*FCFS*

#include

#include

int main()

{

int i,j,sum=0,n;

int ar,tm;

int disk;

clrscr();

printf(“enter number of location\t”);

scanf(“%d”,&n);

scanf(“%d”,&disk);

printf(“enter elements of disk queue\n”);

for(i=0;i<n;i++)

{

scanf("%d",&ar[i]);

tm[i]=disk-ar[i];

if(tm[i]<0)

{

tm[i]=ar[i]-disk;

}

disk=ar[i];

sum=sum+tm[i];

}

/*for(i=0;i<n;i++)

{

printf("\n%d",tm[i]);

}   */

printf("\nmovement of total cylinders %d",sum);

getch();

return 0;

}

*sstf*

#include
#include
#include
void main()
{
float avg;
// clrscr();
printf(“*** SSTF Disk Scheduling Algorithm ***\n”);
printf(“Enter the size of Queue\t”);
scanf(“%d”,&n);
printf(“Enter the Queue\t”);
for(i=0;i<n;i++)
{
scanf("%d",&queue[i]);
}
for(i=1;i<n;i++)
for(i=0;i<n;i++)
{
for(j=i+1;jt[j])
{
temp=t[i];
t[i]=t[j];
t[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
for(i=1;i<n-1;i++)
{
}
printf("\nTotal Seek Time is%d\t",seek);
avg=seek/(float)n;
printf("\nAverage Seek Time is %f\t",avg);
getch();
}

*scan*

#include

using namespace std;

int main()
{
clrscr();
int n,d,a,b,c=0,j,i=0;
char ch=’y’;
cout<>n;
cout<>a;

for(i=0;i<n;i++)
{
cout<<"Enter value of cylinder "<<i+1<>d[i];
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{

if(d[i]<d[j])
{
b=d[i];
d[i]=d[j];
d[j]=b;
}

}
}

for(i=0;ia)
{
j=i;
break;
}
}

c=0;
b=0;

do
{
c+=abs(b-d[j]);
b=d[j];
j++;
}while(j<n);

c=c+a;
cout<<" \nTotal head movement = "<<c<<" cylinders";
getch();
}

*c-scan*

#include
#include
void scan(int left[],int right[],int i,int head,int n)
{
int ans;
int a,b,c,d;
a=i-1;
d=0;
c=n-1-i;
b=i;
while(a>-1)
{ cout<<“\n a is “<<a;
cout<<“\n left[a] is”<<left[a];

ans[d]=left[a];
a–;
d++;
}
cout<<“d is”<<d;
while(d<n)
{
ans[d]=right[c];
cout<<“\n right c is”<<right[c];
cout<<“\n ans[d] is “<<ans[d];
c–;
d++;
}
cout<<“order is”;
getch();
for(int j=0;j<n;j++)
{
cout<<“\n”<<ans[j];
}
}
{
cout<<“array is”;
for(int q=0;q<n;q++)
{
cout<<“\n “<<d[q];
}
int left,right;
{
cout<<“breaking at “<<d[i];
break;
}
}
cout<<“value of i”<<i;
int k,l,m;
l=1;
k=0;
m=n;
left=d;
cout<<“left is”<<left;
while(l<i)
{
cout<<“\n d[l] value” <<d[l];
left[l]=d[l];
cout<<“\n left is “<<left[l];
l++;
cout<<“l is “<<l;
}
int o;
o=i;
while(o<m)
{
right[k]=d[o];
cout<<“\n right is”<<right[k];
cout<<“\n d[i] is”<<d[o];
k++;
o++;
}
}
void sort(int d[],int n)
{
int temp,small,pos;
for(int i=0;i<n-1;i++)
{
small=d[i];
pos=i;
for(int j=i+1;jd[j])
{
small=d[j];
pos=j;
}
}
temp=d[pos];
d[pos]=d[i];
d[i]=temp;
}
}
void main()
{
clrscr();
cout<>n;
for(int i=0;i<n;i++)
{
cout<>d[i];
}
sort(d,n);
getch();
}

5. Manisha

What data structures do we have to use in this …. Is it queue and hash ?

6. Venkat

You must comment your code anyway, it was helpful thankyou for posting.

7. B.sai Jyothi

Thanks for the best code
It helped me alot

8. K Anilkumar

how to find a hits and misses of cache and generate sequance of page requests

9. ramesh

what are the flags1 and flags2 used for?

10. BetterProgrammerthanOP

This code does not work, the last if statement checking flag2 == 0 is not reachable. And the for loop under if(flag1 == 0) is just wrong and is not what LRU algorithm is supposed to be doing.

11. anonymous

Hey,

Thanks for the program. It helps.
Was wondering how can I find total number of dirty bits(or pages) from the code above?

12. Dani Reid

How to find the smallest frame ? Using a counter