Least Recently Used Algorithm
Least Recently Used Algorithm
Least Recently Used Algorithm
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.
This idea is somewhat based on locality of reference, that any page that has been unused for a
great amount of time is more likely to remain unused. Therefore, it is better to replace that
page.
Code
In this algorithm, we replace the element which is the current least recently used element in
the current stack.That is, when we look to the left of the table, that we have created we
choose the further most page to get replaced.
#include<stdio.h>
#include<limits.h>
if(incomingPage == queue[i])
return 1;
return 0;
}
printf("%d\t\t\t",queue[i]);
int main()
int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3;
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0;
printf("%d: \t\t",incomingStream[i]);
queue[occupied] = incomingStream[i];
pagefault++;
occupied++;
printFrame(queue, occupied);
else{
int index;
distance[j] = 0;
++distance[j];
if(queue[j] == incomingStream[k])
break;
max = distance[j];
index = j;
queue[index] = incomingStream[i];
printFrame(queue, occupied);
pagefault++;
printf("\n");
return 0;
Example :
1. Time reference string : 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1
Allocation frame = 3
7 0 1 2 0 3 0 4 2 3 0 3 2 1
7 7 7 2 2 2 2 4 4 4 0 0 0 1
0 0 0 0 0 0 0 0 3 3 3 3 3
1 1 1 3 3 3 2 2 2 2 2 2
Output :
Page Frame1 Frame2 Frame3
7: 7
0: 7 0
1: 7 0 1
2: 2 0 1
0: 2 0 1
3: 2 0 3
0: 2 0 3
4: 4 0 3
2: 4 0 2
3: 4 3 2
0: 0 3 2
3: 0 3 2
2: 0 3 2
1: 1 3 2
Page Fault: 10