Exercises — Page replacement — FIFO, LRU, Clock, Optimal
Throughout, = number of frames (slots in RAM), a reference string is the ordered list of page numbers the program touches, and a fault is a reference to a page not currently in a frame.
Level 1 — Recognition
Exercise 1.1
Three policies are described by one behaviour each. Name the policy.
- (a) "Throw out whichever page has sat in memory the longest, no matter how often it was used."
- (b) "Throw out whichever page has gone the longest since its last touch."
- (c) "Throw out the page whose next use lies furthest ahead."
Recall Solution
(a) FIFO — it uses arrival time only. (b) LRU — it uses last-access time. (c) Optimal (OPT) — it looks into the future; unrealizable, used as a benchmark. Why: the distinguishing word is arrival vs last-access vs future. That single word is the whole policy.
Exercise 1.2
Clock (Second-Chance) keeps one bit per frame. What is that bit called, what sets it to 1, and what does a hand-sweep do when it lands on a frame whose bit is 1?
Recall Solution
The bit is the reference bit R. Hardware sets on every access to that page. When the sweeping hand lands on a frame with , it does not evict; it gives a second chance by setting and advancing. It evicts only when it finds .
Exercise 1.3
True or false: "OPT always produces strictly fewer faults than LRU." Explain in one line.
Recall Solution
False. OPT guarantees faults(OPT) faults(any policy) — it is a lower bound, so it can tie. On 7 0 1 2 0 3 0 4 with 3 frames both score 6 (shown in the parent note).
Level 2 — Application
Exercise 2.1
Reference string 1 2 3 4 1 2 5 1 2 3 4 5, frames, FIFO. Count the faults.
Recall Solution
Track the queue oldest→newest. A * marks a fault.
| Ref | Frames (old→new) | Fault |
|---|---|---|
| 1 | [1] | * |
| 2 | [1,2] | * |
| 3 | [1,2,3] | * |
| 4 | [2,3,4] | * evict 1 |
| 1 | [3,4,1] | * evict 2 |
| 2 | [4,1,2] | * evict 3 |
| 5 | [1,2,5] | * evict 4 |
| 1 | [1,2,5] | hit |
| 2 | [1,2,5] | hit |
| 3 | [2,5,3] | * evict 1 |
| 4 | [5,3,4] | * evict 2 |
| 5 | [5,3,4] | hit |
Faults = 9. (This is the famous Belady string — remember 3 frames → 9.)
Exercise 2.2
Same string 1 2 3 4 1 2 5 1 2 3 4 5, frames, FIFO. Count the faults.
Recall Solution
| Ref | Frames (old→new) | Fault |
|---|---|---|
| 1 | [1] | * |
| 2 | [1,2] | * |
| 3 | [1,2,3] | * |
| 4 | [1,2,3,4] | * |
| 1 | [1,2,3,4] | hit |
| 2 | [1,2,3,4] | hit |
| 5 | [2,3,4,5] | * evict 1 |
| 1 | [3,4,5,1] | * evict 2 |
| 2 | [4,5,1,2] | * evict 3 |
| 3 | [5,1,2,3] | * evict 4 |
| 4 | [1,2,3,4] | * evict 5 |
| 5 | [2,3,4,5] | * evict 1 |
Faults = 10. More frames → more faults. That is Belady's Anomaly (Belady's Anomaly), and 2.1 vs 2.2 is its canonical demonstration.
Exercise 2.3
Reference string 2 3 2 1 5 2 4 5 3 2 5 2, , LRU. Count the faults.
Recall Solution
List frames LRU→MRU (leftmost is the eviction candidate). On a hit we move the page to the MRU (right) end.
| Ref | Frames (LRU→MRU) | Fault |
|---|---|---|
| 2 | [2] | * |
| 3 | [2,3] | * |
| 2 | [3,2] | hit (2→MRU) |
| 1 | [3,2,1] | * |
| 5 | [2,1,5] | * evict 3 |
| 2 | [1,5,2] | hit (2→MRU) |
| 4 | [5,2,4] | * evict 1 |
| 5 | [2,4,5] | hit (5→MRU) |
| 3 | [4,5,3] | * evict 2 |
| 2 | [5,3,2] | * evict 4 |
| 5 | [3,2,5] | hit (5→MRU) |
| 2 | [3,5,2] | hit (2→MRU) |
Faults = 7.
Exercise 2.4
Same string 2 3 2 1 5 2 4 5 3 2 5 2, , Clock. Reference bits start at 1 when a page is loaded or hit; the hand starts on the first frame. Count the faults.
Recall Solution
Frames are a 3-slot ring; hand ↓ marks the current position. (R) is the reference bit. On a hit we set that frame's . On a fault the hand sweeps: →clear to 0 and advance; →evict here, load new page with , advance hand one past.
| Ref | Action | Ring (hand ↓) | Fault |
|---|---|---|---|
| 2 | load slot0 | ↓2(1) _ _ | * |
| 3 | load slot1 | 2(1) ↓3(1) _ | * |
| 2 | hit slot0 | 2(1) 3(1) _ (hand at slot2) | hit |
| 1 | load slot2 | 2(1) 3(1) ↓1(1) | * |
| 5 | sweep: 2→0,3→0,1→0, then 2(0) evict | put 5(1) slot0 → 5(1) 3(0) 1(0), hand→slot1 | * |
| 2 | sweep: 3(0) evict slot1 | 5(1) 2(1) 1(0), hand→slot2 | * |
| 4 | sweep: 1(0) evict slot2 | 5(1) 2(1) 4(1), hand→slot0 | * |
| 5 | hit slot0, R=1 | (hand stays slot0) | hit |
| 3 | sweep: 5→0 adv, 2→0 adv, 4→0 adv, 5(0) evict slot0 | 3(1) 2(0) 4(0), hand→slot1 | * |
| 2 | hit slot1, R=1 | hit | |
| 5 | sweep: 2→0 (slot1), 4(0) evict slot2 | 3(1) 2(0) 5(1), hand→slot0 | * |
| 2 | hit slot1, R=1 | hit |
Faults = 8. Note Clock (8) did worse than LRU (7) here — it approximates recency, it does not reproduce it.
Level 3 — Analysis
Exercise 3.1
On 7 0 1 2 0 3 0 4 with 3 frames, FIFO faults 7 times but LRU faults 6. Identify the single reference where they diverge and explain why LRU wins there.
Recall Solution
They diverge at the second 0 (step 7 of the parent's tables). By step 6, both had to evict a page. FIFO's queue made 0 the oldest, so FIFO evicted 0 at step 6 and had to reload it (fault) at step 7. LRU had just used 0 at step 5, so 0 was Most-Recently-Used — LRU evicted 1 instead and still held 0 at step 7 → hit.
Why LRU wins: FIFO judges by age; but age is a bad proxy for future use when a page is old and still hot. LRU judges by recent use, which — under Locality of Reference — predicts near-future use.
Exercise 3.2
A colleague says "OPT is useless, we can't see the future." Give the one concrete use OPT still has, and state the inequality it certifies.
Recall Solution
OPT is the benchmark / lower bound. For any realizable policy on the same string and frame count: So OPT tells you the best possible fault count. If LRU is close to OPT on your workload, you know LRU is essentially as good as any policy could be — no point chasing a fancier algorithm. That is the whole value of an unachievable optimum: it bounds how much room for improvement exists.
Exercise 3.3
Explain why LRU and OPT never show Belady's Anomaly, but FIFO can. Reference the stack property.
Recall Solution
A policy is a stack algorithm if, for every prefix of the reference string, the set of pages it holds with frames is always a subset of the set it holds with frames: LRU and OPT satisfy this: with more frames you keep a superset of pages, so any page present with frames is present with — you can never fault more with more memory. Hence faults are monotonically non-increasing in ; no anomaly. FIFO is not a stack algorithm: its victim depends on insertion order, and a different queue length reorders evictions so . That broken inclusion is exactly what lets adding a frame increase faults (see 2.1 vs 2.2, Belady's Anomaly).
Level 4 — Synthesis
Exercise 4.1
Construct a reference string using pages {1,2,3,4} that, with FIFO and , forces a fault on a page immediately after that page was evicted (i.e. reproduce FIFO's blind-spot at least once). Show the trace.
Recall Solution
Use 1 2 3 4 1.
| Ref | Frames (old→new) | Fault |
|---|---|---|
| 1 | [1] | * |
| 2 | [1,2] | * |
| 3 | [1,2,3] | * |
| 4 | [2,3,4] | * evict 1 |
| 1 | [3,4,1] | * reload 1 ← the blind spot |
Faults = 5. FIFO evicted 1 (oldest) at ref 4, then the very next reference needed 1 again. LRU would also fault on 4 and 1 here, so this short string is the essence of the trap, not yet a win for LRU — but it isolates the mechanism: eviction by age ignores that 1 was about to be reused.
Exercise 4.2
Build a string where LRU strictly beats FIFO on the same frames (both count faults, LRU fewer). Use . Give both traces and both fault counts.
Recall Solution
Use 1 2 3 1 4 1 2.
FIFO (old→new):
| Ref | Frames | Fault |
|---|---|---|
| 1 | [1] | * |
| 2 | [1,2] | * |
| 3 | [1,2,3] | * |
| 1 | [1,2,3] | hit |
| 4 | [2,3,4] | * evict 1 |
| 1 | [3,4,1] | * evict 2 (reload 1) |
| 2 | [4,1,2] | * evict 3 |
FIFO faults = 6.
LRU (LRU→MRU):
| Ref | Frames | Fault |
|---|---|---|
| 1 | [1] | * |
| 2 | [1,2] | * |
| 3 | [1,2,3] | * |
| 1 | [2,3,1] | hit (1→MRU) |
| 4 | [3,1,4] | * evict 2 |
| 1 | [3,4,1] | hit (1→MRU) |
| 2 | [4,1,2] | * evict 3 |
LRU faults = 5.
Why LRU wins: at ref 4, FIFO's oldest frame is 1 (arrived first) so FIFO evicts 1 — but 1 was just used at ref 4-position-4 and is needed again next. LRU sees 1 as MRU and evicts 2 instead, keeping 1 for its reuse. Age vs recency, made to bite.
Exercise 4.3
Construct a string on {1,2,3} with where OPT ties LRU (equal faults) — proving OPT need not strictly win. Give both counts.
Recall Solution
Use 1 2 3 2 1.
OPT (): at ref 3 (frames [1,2]), look ahead — 2 used at position 4, 1 at position 5. 1 is used furthest, so evict 1.
| Ref | Frames | Fault | note |
|---|---|---|---|
| 1 | [1] | * | |
| 2 | [1,2] | * | |
| 3 | [2,3] | * | evict 1 (furthest) |
| 2 | [2,3] | hit | |
| 1 | [3,1] | * | evict 2 or 3 |
OPT faults = 4.
LRU ():
| Ref | Frames (LRU→MRU) | Fault |
|---|---|---|
| 1 | [1] | * |
| 2 | [1,2] | * |
| 3 | [2,3] | * evict 1 |
| 2 | [3,2] | hit |
| 1 | [2,1] or [3,1] | * evict 3 |
LRU faults = 4. They tie — confirming OPT is , not .
Level 5 — Mastery
Exercise 5.1
Prove: with frames, any demand-paging policy faults at least once on each distinct first appearance of a page, and therefore
Then apply it: 7 0 1 2 0 3 0 4 has how many distinct pages, and what does the bound say about the minimum faults?
Recall Solution
Proof. A frame can only contain a page that was previously loaded into it, and a page is loaded only on a fault (demand paging — nothing is prefetched). The first time a page is referenced, it has never been loaded, so it cannot be in any frame; that reference is necessarily a fault. Distinct pages have distinct first appearances, and no reference can be the first appearance of two pages. Hence there are at least (number of distinct pages) faults, giving the bound.
Application. Distinct pages in 7 0 1 2 0 3 0 4 are = 6. So every policy — even OPT — faults at least 6 times. Since OPT achieves exactly 6 (parent note), 6 is unavoidable and OPT is tight here. These 6 are the compulsory / cold-start faults (Thrashing & Working Set Model calls them cold misses).
Exercise 5.2
Prove that LRU is a stack algorithm: for a fixed reference string, after processing any prefix, the set of pages LRU holds with frames satisfies . (Sketch is fine; identify the invariant.)
Recall Solution
Invariant. After processing any prefix, LRU with frames holds exactly the most recently used distinct pages of that prefix (with fewer if fewer distinct pages have appeared).
Why the invariant holds: LRU only ever evicts the least recently used page and always keeps the just-referenced page as most-recently-used, so the retained set is precisely the top- by recency. (Induct on prefix length: the base is trivial; each new reference either is a hit — recency order updates, top- still the most recent — or a fault that inserts the new page as most-recent and drops the least-recent, again leaving the most recent.)
Conclusion: the " most recent distinct pages" is, by definition, a subset of the " most recent distinct pages." Therefore , LRU is a stack algorithm, and by Exercise 3.3's reasoning its fault count is monotone non-increasing in — no Belady's Anomaly.
Exercise 5.3
On 7 0 1 2 0 3 0 4, , you are told FIFO=7, LRU=6, OPT=6. Compute the fault rate for each and the relative overhead of FIFO over OPT as a percentage.
Recall Solution
There are references.
- FIFO: .
- LRU: .
- OPT: .
Relative overhead of FIFO over OPT: So FIFO does more faults than the theoretical best on this string, while LRU matches OPT exactly.