012013101t1t2t3t4t5t6t7t8t9SWAPframe Aframe Bframe Ckernel policyCLOCK010101FIFOLRUclock0123
Back: the reference string and the disk (swap)Middle: three RAM frames and the kernel's policy; front: use bits, clock hand, hitsDiscs: pages moving between disk and framesThe reference string 0 1 2 0 1 3 1 0 1 at the back, the disk (swap) beside it, three RAM frames in the middle, and the clock board with its use bits and hand in front. Ticks 1 to 5 fill the frames. At tick 6 page 3 needs a frame: FIFO evicts page 0 (the first in), LRU evicts page 2 (the longest unused), and clock clears every use bit on a full lap and evicts page 0, then at tick 8 gives page 1 a second chance. Hits: FIFO 3, clock 4, LRU 5 of 9.

The problem

  1. A program uses four pages, numbered 0 to 3, but the kernel has given it only three frames of RAM: A, B and C. Every page has a copy on disk, in swap. A page must sit in a frame to be used.

FIFO

LRU

Clock

What happens

;; step 1/11 · setup
pages    0 1 2 3          (4 pages of the program)
frames   A B C            (3 frames of RAM)
disk     a copy of every page, in swap

a page fault that must read the disk is a "major" fault.
a program can count its own faults (C99, POSIX + BSD/Linux):

#include <stdio.h>
#include <sys/resource.h>

int main(void) {
    struct rusage u;
    if (getrusage(RUSAGE_SELF, &u) != 0) return 1;
    printf("minor faults: %ld\n", u.ru_minflt);
    printf("major faults: %ld\n", u.ru_majflt);
    return 0;
}

Real machines have thousands to millions of frames and pages of 4 KiB or more; three frames and four pages keep every choice visible. The policies work the same at any size. The hand starts at frame A, and a page loaded on a fault gets use bit 1, because the faulting access itself touches it. Other textbook variants differ in such details, which can change which page a given string evicts.