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.
Below program shows how to implement this algorithm in C.
Program for LRU Page Replacement Algorithm in C
#include<stdio.h> int findLRU(int time[], int n){ int i, minimum = time[0], pos = 0; for(i = 1; i < n; ++i){ if(time[i] < minimum){ minimum = time[i]; pos = i; } } return pos; } int main() { int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos, faults = 0; printf("Enter number of frames: "); scanf("%d", &no_of_frames); printf("Enter number of pages: "); scanf("%d", &no_of_pages); printf("Enter reference string: "); for(i = 0; i < no_of_pages; ++i){ scanf("%d", &pages[i]); } for(i = 0; i < no_of_frames; ++i){ frames[i] = -1; } for(i = 0; i < no_of_pages; ++i){ flag1 = flag2 = 0; for(j = 0; j < no_of_frames; ++j){ if(frames[j] == pages[i]){ counter++; time[j] = counter; flag1 = flag2 = 1; break; } } if(flag1 == 0){ for(j = 0; j < no_of_frames; ++j){ if(frames[j] == -1){ counter++; faults++; frames[j] = pages[i]; time[j] = counter; flag2 = 1; break; } } } if(flag2 == 0){ pos = findLRU(time, no_of_frames); counter++; faults++; frames[pos] = pages[i]; time[pos] = counter; } printf("\n"); for(j = 0; j < no_of_frames; ++j){ printf("%d\t", frames[j]); } } printf("\n\nTotal Page Faults = %d", faults); return 0; }
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.
The post LRU Page Replacement Algorithm in C appeared first on The Crazy Programmer.