Fall 2024 - CS604P - 2
Fall 2024 - CS604P - 2
Question Statement:
Write a C program to implement the LRU Page Replacement algorithm. If we consider the
recent past as a predictor of the near future, LRU will replace the page that has been unused for
the longest time. Pages that are rarely used are likely to remain infrequently accessed in the
future.
Also write and display your Student ID at the end of the program output.
Sample Input:
Enter Total number of frames: 4
Enter number of Pages: 8
Enter Reference string
Output Sample:
My Student ID: BC81902320
Total Page Faults: 7
Analysis:
To implement the LRU Page Replacement algorithm, we need to keep track of the order in which
pages are accessed. When a page fault occurs, we replace the page that was accessed the longest
time ago.
C Program Implementation:
#include <stdio.h>
int main() {
scanf("%d", &frames);
scanf("%d", &pages);
scanf("%d", &reference_string[i]);
frames_array[i] = -1;
counter[i] = 0;
flag = 0;
if(frames_array[j] == reference_string[i]) {
counter[j] = i;
flag = 1;
break;
}
if(flag == 0) {
if(frames_array[j] == -1) {
page_faults++;
frames_array[j] = reference_string[i];
counter[j] = i;
flag = 1;
break;
if(flag == 0) {
page_faults++;
temp[j] = counter[j];
int t = temp[j];
temp[j] = temp[k];
temp[k] = t;
t = frames_array[j];
frames_array[j] = frames_array[k];
frames_array[k] = t;
}
if(frames_array[j] != reference_string[i]) {
frames_array[j] = reference_string[i];
counter[j] = i;
break;
return 0;
Explanation:
1. Initialization:
Declare variables for frames, pages, page_faults, arrays for reference string,
frames, counters, and a temporary array.
Initialize frames_array with -1 to indicate empty frames.
Initialize counter array to 0.
4. Display Output:
Output: