Let us learn how to implement LRU Page Replacement Algorithm in C programming language. This code for Least Recently Used Page Replacement makes use of Arrays.
What is LRU Page Replacement Algorithm?
The page replacement algorithms help an operating system in deciding the memory pages that needs to be swapped out, written to the disk when a page of memory needs to be allocated in the system.
The LRU Page Replacement method is a marking algorithm. It keeps a track of the page usage in a given period of time. The LRU algorithm offers
The LRU algorithm offers optimum performance but is costly in its implementation. The LRU page replacement technique is modified for implementation, and its successors are
The LRU page replacement technique is modified for implementation, and its successors are LRU – K and ARC algorithms.
C Program To Implement LRU Page Replacement Algorithm in OS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include<stdio.h> int main() { int frames[10], temp[10], pages[10]; int total_pages, m, n, position, k, l, total_frames; int a = 0, b = 0, page_fault = 0; printf("\nEnter Total Number of Frames:\t"); scanf("%d", &total_frames); for(m = 0; m < total_frames; m++) { frames[m] = -1; } printf("Enter Total Number of Pages:\t"); scanf("%d", &total_pages); printf("Enter Values for Reference String:\n"); for(m = 0; m < total_pages; m++) { printf("Value No.[%d]:\t", m + 1); scanf("%d", &pages[m]); } for(n = 0; n < total_pages; n++) { a = 0, b = 0; for(m = 0; m < total_frames; m++) { if(frames[m] == pages[n]) { a = 1; b = 1; break; } } if(a == 0) { for(m = 0; m < total_frames; m++) { if(frames[m] == -1) { frames[m] = pages[n]; b = 1; break; } } } if(b == 0) { for(m = 0; m < total_frames; m++) { temp[m] = 0; } for(k = n - 1, l = 1; l <= total_frames - 1; l++, k--) { for(m = 0; m < total_frames; m++) { if(frames[m] == pages[k]) { temp[m] = 1; } } } for(m = 0; m < total_frames; m++) { if(temp[m] == 0) position = m; } frames[position] = pages[n]; page_fault++; } printf("\n"); for(m = 0; m < total_frames; m++) { printf("%d\t", frames[m]); } } printf("\nTotal Number of Page Faults:\t%d\n", page_fault); return 0; } |
Output

If you have any doubts or compilation errors in this C program to implement Least Recently Used Page Replacement Algorithm in operating system, let us know about it in the comment section below.
Excellent code. Thanks. Finally it works. I executed this code in my Linux Mint OS.
A page fault normally is encountered when a referenced page in not found in the memory frames.
The Least Recently Used Algorithm has a disadvantage that that its performance tends to degenerate under some of the most commonly used reference patterns.
Thanks for the lru program.
Can anyone plz xplain me the code in for loop
(K=n-1,l=1;l lessthan or equal to total frames-1;l++,k–)
Hi, I need some help to understand this for loop :
for(k = n – 1, l = 1; l <= total_frames – 1; l++, k–). Anyone can help me ?
Tq bro