Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Lab 12: Page replacement algorithms: FIFO, LRU, Optimal

Theory link: 4.6 Page replacement

Experiment 12 of 15 Module 4 2 hours

Video walkthrough

Lab 12 walkthrough — Page replacement algorithms: FIFO, LRU, Optimal

Video coming soon

Lab 12 walkthrough — Page replacement algorithms: FIFO, LRU, Optimal

Recorded by Dr. Krithika L.B. Slot lab12-walkthrough

Aim

Aim. To simulate FIFO, LRU and Optimal page replacement on a reference string and count the page faults.

Explanation

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:

FIFO

A circular pointer next marks the oldest frame.

LRU

last[j] stores when frame j was last used; evict the smallest.

Optimal

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.

Code

pagerep.c
/* 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;
}

Download pagerep.c

How to run

  1. gcc pagerep.c -o pagerep
  2. ./pagerep
  3. Enter the length of the reference string, the string, the number of frames, and the algorithm.

Sample input & output

LRU, 3 frames

Input
13 · 7 0 1 2 0 3 0 4 2 3 0 3 2 · 3 · choice 2
Output
Output
Ref  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%

All three on the textbook string

Input
22 · 7 0 1 2 0 3 0 4 2 3 0 3 0 3 2 1 2 0 1 7 0 1 · 3 frames
Output
Output
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%

Belady's anomaly (FIFO)

Input
12 · 1 2 3 4 1 2 5 1 2 3 4 5 · 3 frames, then 4 frames
Output
Output
3 frames: Total page faults = 9
4 frames: Total page faults = 10

Try it yourself

  1. Print which page was replaced on each fault.
  2. Run all three algorithms in one execution and print a comparison table.
  3. Implement the second-chance (clock) algorithm with a reference bit.
  4. Plot faults vs number of frames (1–7) for FIFO and LRU.

Viva questions

Prepare these for the lab evaluation.

Revealed 0
Which algorithm gives the minimum number of faults?

Optimal — used as a benchmark because it needs future knowledge.

What is Belady's anomaly?

More frames causing more faults; it occurs with FIFO.

How does your program implement LRU?

It stores the time of last use of each frame and evicts the frame with the smallest time.

What is the hit ratio?

(references − faults) / references × 100.