Numerical: count page faults for FIFO, LRU and Optimal with 3 frames.
HardConceptOS Numericals
Answer
Problem: reference string 2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2 with 3 frames, initially empty. Count the page faults for FIFO, LRU and Optimal. Answer: FIFO 9, LRU 7, Optimal 6.
- The first reference to each page always faults, so at least 5 faults (pages 1 to 5) are unavoidable.
- FIFO evicts the page loaded earliest, LRU the page used least recently, and Optimal the page whose next use is furthest away or never comes.
- Write the frames after every reference and mark each fault; hit ratio = hits / references.
Example (worked solution): frame contents after each fault (- marks a hit):
Ref: 2 3 2 1 5 2 4 5 3 2 5 2
FIFO: 2 23 - 231 531 521 524 - 324 - 354 352 9 faults
LRU: 2 23 - 231 251 - 254 - 354 352 - - 7 faults
Optimal: 2 23 - 231 235 - 435 - - 235 - - 6 faults
Key steps: in FIFO, the 3 at reference 9 evicts 5 (the oldest), although 5 is needed two references later. In LRU, the 4 at reference 7 evicts 1, last used at reference 4. In Optimal, the 4 evicts 2, whose next use (reference 10) is furthest away. Hit ratios: FIFO 3/12 = 25%, LRU 5/12 ≈ 42%, Optimal 6/12 = 50%.