Page replacement: FIFO, LRU and clock
About this lesson
When a page fault needs a frame of RAM and every frame is full, the kernel must evict a page to make room. Which page it picks decides how many slow disk reads the program suffers. Follow one program with three frames through the same nine page touches under three policies: FIFO evicts the oldest arrival, LRU the page unused the longest, and clock approximates LRU cheaply with one hardware use bit per frame. The lesson Page faults and swapping to disk covers how a fault is handled and how pages move to and from swap; here we look only at the choice of victim.
More pages than frames
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.
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;
}Read the lesson step by step
More pages than frames
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.
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; }The reference string
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.
reference string 0 1 2 0 1 3 1 0 1 tick 1 2 3 4 5 6 7 8 9 hit the page is in a frame: fast, no kernel work fault it is not: the kernel reads it from disk (OSTEP: about 100 ns for RAM, 10 ms for a disk) goal of every policy: as few faults as possibleTicks 1 to 5: filling up
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 page result frames A B C 1 0 fault 0 - - 2 1 fault 0 1 - 3 2 fault 0 1 2 4 0 hit 0 1 2 5 1 hit 0 1 2 no frame was full yet, so no page had to leave: every policy does exactly thisTick 6: memory is full
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.
tick 6 page 3 fault frames A B C = 0 1 2 (all full) which page leaves? that is the replacement policy optimal (Belady's MIN): evict the page whose next use is furthest in the future. Here page 2 is never used again. MIN gets 5 hits, the most possible, but needs the future. real policies guess the future from the past.FIFO: oldest arrival leaves
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.
FIFO each frame notes when its page came in frame page came in A 0 t1 <- earliest: victim B 1 t2 C 2 t3 page 0 leaves; page 3 is read into frame A (in t6) a hit never changes the note: use does not countFIFO pays for it
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.
tick page result victim frames A B C 6 3 fault page 0 3 1 2 7 1 hit - 3 1 2 8 0 fault page 1 3 0 2 9 1 fault page 2 3 0 1 FIFO 3 hits 6 faultsLRU: longest unused leaves
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.
LRU each frame notes when its page was last used (ticks 4 and 5 moved pages 0 and 1 forward) frame page last used A 0 t4 B 1 t5 C 2 t3 <- earliest: victim page 2 leaves; page 3 is read into frame C (used t6)LRU keeps what is used
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.
tick page result frames A B C last used 6 3 fault 0 1 3 t4 t5 t6 7 1 hit 0 1 3 t4 t7 t6 8 0 hit 0 1 3 t8 t7 t6 9 1 hit 0 1 3 t8 t9 t6 LRU 5 hits 4 faults MIN 5 hits (the best possible here)Clock: a cheap LRU
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.
use bit set to 1 by the hardware on every access (the accessed bit in the page table entry); only the kernel clears it tick 6: page 3 faults, hand at A, bits A B C = 1 1 1 A: 1 -> clear, move on B: 1 -> clear, move on C: 1 -> clear, move on A: 0 -> evict page 0, load page 3, bit 1 a full lap: this time clock picks what FIFO pickedPage 1 gets a second chance
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.
tick 7 page 1 hit bits A B C = 1 1 0 tick 8 page 0 fault hand at B B: 1 -> clear (second chance for page 1) C: 0 -> evict page 2, load page 0, bit 1 tick 9 page 1 hit page 1 was kept: no fault clock 4 hits 5 faultsThree rules, one string
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.
the three policies as a Python simulation: refs = [0, 1, 2, 0, 1, 3, 1, 0, 1] N = 3 # frames def fifo(refs): mem, hits = [], 0 # oldest first for p in refs: if p in mem: hits += 1; continue if len(mem) == N: mem.pop(0) # evict first in mem.append(p) return hits def lru(refs): mem, hits = [], 0 # least recent first for p in refs: if p in mem: hits += 1; mem.remove(p) elif len(mem) == N: mem.pop(0) # evict least recent mem.append(p) # now most recent return hits def clock(refs): page, use, hand, hits = [], [], 0, 0 for p in refs: if p in page: hits += 1; use[page.index(p)] = 1; continue if len(page) < N: page.append(p); use.append(1); continue while use[hand]: # second chance use[hand] = 0; hand = (hand + 1) % N page[hand], use[hand] = p, 1 # evict, load hand = (hand + 1) % N return hits for f in (fifo, lru, clock): h = f(refs) print(f.__name__, h, "hits", len(refs) - h, "faults") prints: fifo 3 hits 6 faults lru 5 hits 4 faults clock 4 hits 5 faults
6 quick questions
Choose one answer per question. You can change your choices before checking them.
01When does the kernel need a page-replacement policy at all?
A hit needs no kernel work, and a fault with a free frame simply loads the page there. Only when all frames are full must the kernel evict a victim, and the policy decides which one.
02Three frames hold pages 0, 1 and 2, loaded at ticks 1, 2 and 3. Page 0 was used again at tick 4 and page 1 at tick 5. At tick 6 page 3 faults. Which page does FIFO evict?
FIFO only looks at arrival order. Page 0 came in first (tick 1), so it leaves, even though tick 4 just used it. In the lesson's string, page 0 is needed again at tick 8, so FIFO pays with an extra fault.
03Same situation, but with LRU. Which page is evicted at tick 6?
LRU evicts the page whose last use is the earliest. Page 2 was last touched at tick 3, before pages 0 (tick 4) and 1 (tick 5).
04In the clock algorithm, the hand reaches a frame whose use bit is 1. What happens?
A use bit of 1 means the page was touched since the hand last passed. The kernel clears it and moves on; the first frame found with bit 0 is evicted. If every bit is 1, the hand clears them all and comes back to where it started.
05Why do real kernels approximate LRU (for example with clock) instead of running exact LRU?
LRU needs to know which page was used longest ago, so something would have to note the time of every load and store. Hardware does set one accessed (use) bit per page on each touch for free, and clock uses just that bit to get close to LRU.
06On the reference string 0 1 2 0 1 3 1 0 1 with 3 frames, which ranking of hits is right?
FIFO evicts pages that are still in use, so it gets 3 hits. Clock keeps page 1 thanks to its use bit and gets 4. LRU gets 5, the same as the optimal policy on this string.
Glossary
- Frame
- A page-sized slot of physical memory (RAM). Each frame holds one page at a time.
- Reference string
- The list of pages a program touches, in order. Used to compare replacement policies.
- Page fault
- A touch of a page that is not in a frame. The kernel must map a frame for it, and often has to read the page from disk (swap) first, before the program can go on.
- Victim
- The page the kernel evicts from its frame to make room for a page that faulted.
- FIFO
- First in, first out: evict the page that has been in memory the longest, however recently it was used.
- LRU
- Least recently used: evict the page whose last use is the furthest in the past.
- Use bit
- One bit per page (the accessed or reference bit), set to 1 by the hardware whenever the page is touched. Only the kernel clears it.
- Clock algorithm
- An LRU approximation: a hand sweeps over the frames, clearing use bits of 1 and evicting the first page whose bit is 0.
Notes and sources
- 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.
- An evicted page whose copy on disk is still up to date (a clean page) can simply be dropped. A page that was written to (dirty, marked by the hardware dirty bit) must first be written back to swap, so many kernels prefer to evict clean pages.
- 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.
- FIFO has a strange flaw, Belady's anomaly: for some reference strings, giving it more frames causes more faults. LRU and the optimal policy never get worse with more frames.
- Linux does not run textbook clock. Its page reclaim keeps LRU lists (the multi-gen LRU is one implementation of them) and reads and clears the accessed bits the MMU sets, to tell recently used pages from idle ones.
- Why faults matter: OSTEP estimates a memory access at about 100 ns and a disk access at about 10 ms, 100,000 times longer, so even a small fault rate dominates a program's running time.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 22: Beyond Physical Memory: Policies
- Berkeley CS162, Lecture 17: demand paging and page replacement (FIFO, LRU, MIN, clock)
- MIT 6.1810 (2024), lecture notes: Page faults
- Linux man-pages: getrusage(2)
- Linux kernel documentation: Multi-Gen LRU
Quiz Unlocks after the last step.