Answer: Reference string: 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6; 4 frames.
LRU: frames after each reference: [1], [1,2], [1,2,3], [1,2,3,4], hit 2, hit 1, [4,2,1,5], [2,1,5,6], hit 2, hit 1, hit 2, [6,1,2,3], [1,2,3,7], [2,3,7,6], hit 3, hit 2, [6,3,2,1], hit 2, hit 3, hit 6. Faults at references 1,2,3,4,7,8,12,13,14,17 = 10 faults.
Optimal: keep pages used farthest in future: initial 4 faults, hits at 5,6, fault 7 (replace 4), fault 8 (replace 5), hits 9-12, fault 13 (replace 1), hits 14-16, fault 17 (replace 7), hits 18-20 = 8 faults.
Deciding which page to evict. When a page fault occurs and all frames are occupied, the operating system must choose a victim page to remove. Page replacement algorithms differ in which page they select and in how many faults they produce for a given reference string. The optimal algorithm, also called MIN, replaces the page whose next use is furthest in the future; it produces the fewest possible faults but cannot be implemented in general because future references are unknown, so it serves as a benchmark. First in first out replaces the oldest page, which is simple but ignores usage and suffers Belady's anomaly, where more frames can paradoxically cause more faults. Least recently used replaces the page that has not been used for the longest time, approximating the optimal choice using recent history; it performs well but needs hardware support such as a reference bit, a stack or counters to be efficient. Clock or second chance uses a circular list and a reference bit, giving a cheap approximation to least recently used. Counting based algorithms such as least frequently used keep a counter of accesses. For a reference string processed with three or four frames, the fault count is obtained by stepping through the string, marking hits, and on each miss selecting the victim according to the rule, counting every miss as a fault. A good answer explains the goal of minimising faults, describes optimal, first in first out and least recently used, traces the given reference string with a frame table, states the fault count and notes Belady's anomaly and the fact that optimal is unrealisable. Mentioning that bringing in a page may require writing a dirty victim back to disk adds realism to the cost discussion.
Result: LRU = 10 page faults; Optimal = 8 page faults (Optimal is lower, as expected).
Tip from Examiner: Explain the mechanism or algorithm step by step, support it with a diagram, table or worked calculation, and state the final result with clear units.