Counting LRU page faults — Aptitude Question
Counting LRU page faults: A process references the pages 7, 0, 1, 2, 0, 3, 0, 4. With three frames and LRU… Answer and worked solution on CodeKairo.
- Topic: OS, DBMS & Networks
- Section: Programming MCQs
- Difficulty: Medium
- Time target: 165 seconds
Question
A process references the pages 7, 0, 1, 2, 0, 3, 0, 4. With three frames and LRU replacement, how many page faults occur?
Options
- 4
- 5
- 6
- 7
Show the answer and the worked solution
Correct answer: C. 6
Worked solution
- 7, 0 and 1 fault into the empty frames, leaving [7, 0, 1] with 7 the least recently used.
- 2 faults and evicts 7, giving [0, 1, 2].
- The reference to 0 is a hit and makes 0 the most recently used, so 1 becomes the least recently used.
- 3 faults and evicts 1, giving [0, 2, 3]; the next reference to 0 is a hit, leaving 2 least recently used.
- 4 faults and evicts 2. The faults are on 7, 0, 1, 2, 3 and 4 — 6 in total.
Answer: 6.
The idea
LRU approximates the optimal policy by assuming the recent past predicts the near future. Its cost is the bookkeeping: real systems approximate it with reference bits or a second-chance clock.
Shortcut. Only the two re-references to 0 hit, because 0 is always recent when it returns, so 8 − 2 = 6 faults.
Common trap. 7 is the FIFO count: evicting the oldest-loaded page throws 0 out at the reference to 3, so the next 0 faults as well.