The problem
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.
The program touches one page per tick, in this order, its reference string: 0 1 2 0 1 3 1 0 1. A hit: the page is in a frame. A page fault: it is not, so the kernel reads it from disk.
Ticks 1 to 3: pages 0, 1 and 2 fault, because memory starts empty. The kernel copies each one from disk into a free frame. Ticks 4 and 5: pages 0 and 1 are already in frames, so both are hits.
Tick 6 touches page 3. It faults, and all three frames are full, so the kernel must evict one page, the victim. The replacement policy picks it. The ideal pick is the page needed furthest ahead, but the kernel cannot see ahead.
FIFO
FIFO, first in first out, evicts the page that arrived first. Each frame notes when its page came in. Page 0 came in at tick 1, the earliest, so it leaves, even though tick 4 just used it. Page 3 takes frame A.
Tick 7: page 1 hits. Tick 8: page 0 faults, right after FIFO threw it out, and evicts page 1, now the oldest. Tick 9: page 1 faults again and evicts page 2. FIFO ends with 3 hits and 6 faults.
LRU
Same string, new policy. LRU, least recently used, evicts the page left unused the longest. Each frame notes its page’s last use, updated on every hit. At tick 6, page 2 was last used at tick 3, the earliest, so page 2 leaves.
Ticks 7, 8 and 9 touch pages 1, 0 and 1. All are still in memory, so all three hit. LRU ends with 5 hits and 4 faults. Here that equals the best possible, because page 2 was never needed again.
Clock
Exact LRU updates a note on every memory access: too slow. Instead, hardware sets a frame’s use bit on each touch. Clock sweeps a hand: a 1 is cleared and skipped, a 0 evicted. At tick 6 all are 1, so page 0 goes.
Tick 7: page 1 hits, so B's bit becomes 1. Tick 8: page 0 faults. The hand is at B, bit 1: page 1 stays, its bit cleared. C's bit is 0: page 2 leaves. Clock: 4 hits, 5 faults.
Same nine touches: FIFO 3 hits, clock 4, LRU 5, the best possible here. Clock costs one bit per frame yet beats FIFO. That is why real kernels, Linux included, approximate LRU with the hardware's accessed bits.
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.