Lab 12 walkthrough — Page replacement algorithms: FIFO, LRU, Optimal
Lab 12 walkthrough — Page replacement algorithms: FIFO, LRU, Optimal
Recorded by Dr. Krithika L.B. Slot lab12-walkthrough
Theory link: 4.6 Page replacement
Lab 12 walkthrough — Page replacement algorithms: FIFO, LRU, Optimal
Recorded by Dr. Krithika L.B. Slot lab12-walkthrough
Aim. To simulate FIFO, LRU and Optimal page replacement on a reference string and count the page faults.
Frames start empty (−1). For each reference: if the page is in a frame → hit; otherwise → fault: use an empty frame if any, else choose a victim:
A circular pointer next marks the oldest frame.
last[j] stores when frame j was last used; evict the smallest.
For each frame, find when its page is used next; evict the one used farthest away (or never).
Compare with the simulator in Session 4.6 — the frame contents may be arranged differently, but the fault counts must match.
/* pagerep.c - page replacement: FIFO, LRU, Optimal (BACSE106 Lab 12) */
#include <stdio.h>
int main(void)
{
int n, f, choice, i, j, k;
int ref[50], frame[10], last[10], faults = 0, next = 0;
printf("Length of reference string: ");
scanf("%d", &n);
printf("Reference string: ");
for (i = 0; i < n; i++) scanf("%d", &ref[i]);
printf("Number of frames: ");
scanf("%d", &f);
printf("1.FIFO 2.LRU 3.Optimal Choice: ");
scanf("%d", &choice);
for (j = 0; j < f; j++) frame[j] = -1; /* -1 = empty frame */
printf("\nRef Frames Result\n");
for (i = 0; i < n; i++) {
int hit = -1;
for (j = 0; j < f; j++)
if (frame[j] == ref[i]) hit = j;
if (hit != -1) {
last[hit] = i; /* LRU: remember last use */
} else {
int victim = -1;
for (j = 0; j < f; j++) /* use an empty frame first */
if (frame[j] == -1) { victim = j; break; }
if (victim == -1) {
if (choice == 1) { /* FIFO: circular pointer */
victim = next;
next = (next + 1) % f;
} else if (choice == 2) { /* LRU: smallest last-use time */
victim = 0;
for (j = 1; j < f; j++)
if (last[j] < last[victim]) victim = j;
} else { /* Optimal: used farthest in future */
int far = -1;
for (j = 0; j < f; j++) {
for (k = i + 1; k < n; k++)
if (ref[k] == frame[j]) break;
if (k > far) { far = k; victim = j; } /* k == n means never used again */
}
}
} else if (choice == 1) {
next = (victim + 1) % f;
}
frame[victim] = ref[i];
last[victim] = i;
faults++;
}
printf("%-4d ", ref[i]);
for (j = 0; j < f; j++)
if (frame[j] == -1) printf("- "); else printf("%-2d ", frame[j]);
printf(" %s\n", hit != -1 ? "Hit" : "Fault");
}
printf("\nTotal page faults = %d\nHit ratio = %.2f%%\n", faults, 100.0 * (n - faults) / n);
return 0;
}gcc pagerep.c -o pagerep./pagerepRef Frames Result
7 7 - - Fault
0 7 0 - Fault
1 7 0 1 Fault
2 2 0 1 Fault
0 2 0 1 Hit
3 2 0 3 Fault
0 2 0 3 Hit
4 4 0 3 Fault
2 4 0 2 Fault
3 4 3 2 Fault
0 0 3 2 Fault
3 0 3 2 Hit
2 0 3 2 Hit
Total page faults = 9
Hit ratio = 30.77%FIFO : Total page faults = 15 Hit ratio = 31.82%
LRU : Total page faults = 12 Hit ratio = 45.45%
Optimal : Total page faults = 9 Hit ratio = 59.09%3 frames: Total page faults = 9
4 frames: Total page faults = 10Prepare these for the lab evaluation.
Revealed 0Optimal — used as a benchmark because it needs future knowledge.
More frames causing more faults; it occurs with FIFO.
It stores the time of last use of each frame and evicts the frame with the smallest time.
(references − faults) / references × 100.