VIRTUAL MEMORY

When pages compete, the disk always wins

L216 MINSYSTEMS & LOW-LEVEL
PREDICT

A process touches pages in the order A B C A D A B C A E, repeated. Page A is the hottest page: 4 of every 10 accesses. Memory holds 4 pages at a time, and on a miss the OS evicts one. FIFO evicts the page loaded longest ago; LRU evicts the page used longest ago. Which policy evicts A?

Every address a process uses is virtual. The MMU translates the page number through a small cache (the TLB) and, on a miss, through the page table. If the page is not in a physical frame, the access faults and the OS loads it from disk, evicting another page if no frame is free.

The first touch of a page always faults. The faults that matter are re-faults: loading a page you evicted a moment ago. The replacement policy decides which page to evict. When the pages a process uses in a short window (its working set) exceed its frames, no policy can stop re-faults. That is thrashing.

▶ SIMULATION
MMU · TLB (2 entries)
TLB: empty
hits 0 · misses 0
FIFO replacement
PHYSICAL FRAMES (4 of 4 usable)
f0: free
f1: free
f2: free
f3: free
PAGE TABLE
A:disk B:disk C:disk D:disk E:disk
evicted so far: —
reference stringA B C A D A B C A E A B C A D A B C A E
faults0 = 0 cold + 0 re-faults (budget 2) after 0 accesses
last access—
✓ re-faults ≤ 10% of the 20 references (budget 2)✓ every TLB entry points at a frame that holds that page
SCHEDULE
01[OS]policy: evict the page loaded longest ago (FIFO)
02[PROC]access page A (#1)
03[PROC]access page B (#2)
04[PROC]access page C (#3)
05[PROC]access page A (#4)
06[PROC]access page D (#5)
07[PROC]access page A (#6)
08[PROC]access page B (#7)
09[PROC]access page C (#8)
10[PROC]access page A (#9)
11[PROC]access page E (#10)
12[PROC]access page A (#11)
13[PROC]access page B (#12)
14[PROC]access page C (#13)
15[PROC]access page A (#14)
16[PROC]access page D (#15)
17[PROC]access page A (#16)
18[PROC]access page B (#17)
19[PROC]access page C (#18)
20[PROC]access page A (#19)
21[PROC]access page E (#20)
22[OS]report fault counts
Start of fifo scenario. 22 steps.
Address translation

  virtual address = [ page number | offset ]
  1. TLB lookup        hit  -> frame, done (~1 cycle)
  2. page-table walk   present -> frame, fill TLB
  3. PAGE FAULT        not present -> trap to the OS:
       find a free frame, or pick a victim
       victim dirty? write it to disk
       clear victim PTE, shoot down its TLB entry
       read the page from disk, set PTE present
       restart the faulting instruction

Replacement
  FIFO  evict the page loaded longest ago (ignores use)
  LRU   evict the page used longest ago
        real kernels approximate it with reference bits
        (clock / second chance, active and inactive lists)

Thrashing
  working set > frames -> every load evicts a page needed soon
  fix: more frames, fewer processes, smaller working set

Watch the re-fault counter and the invariant strip. FIFO breaks the budget at access 13, starting with the hot page A. LRU, SQUEEZED is LRU with only 2 frames: 12 re-faults, because the working set does not fit. FIX: LRU with all 4 frames stays at 2.

TUNE IT NOT YET

FIFO thrashes at 4 frames, where LRU holds. Drag PHYSICAL FRAMES to the fewest frames at which FIFO stays within the re-fault budget on this reference string.

GOAL: FIFO within budget, and one frame fewer is not

FIX IT NOT YET

Implement LRU victim selection. frames lists the resident pages; history lists every page referenced so far, oldest first (it does not include the page faulting now). Return the resident page whose most recent use is oldest. The grader checks hand-made cases, then runs the reference string at 4 frames with your function.

Mental model:

physical memory is a cache for the address space, and a major page fault is a miss that costs a disk read instead of a few nanoseconds. A good policy keeps the pages you use most, but only frames ≥ working set stops thrashing. Watch the re-fault rate, not the total fault count.

IN PRODUCTION

Linux major faults and swap (vmstat si/so, sar -B, /proc/pressure/memory), containers killed or crawling at their memory limit, JVMs with heaps larger than RAM, and databases whose buffer pool no longer fits next to a new neighbour.

IN INTERVIEWS

“What happens on a page fault?” and “What is thrashing and how do you fix it?” Walk the TLB, page table and fault path; compare FIFO, LRU and the clock approximation; and say that thrashing is fixed by more memory or less multiprogramming, not by a cleverer policy.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: TCP HANDSHAKE →