What are page replacement algorithms?

Answer

When a page fault occurs and there are no free frames, the OS must evict (replace) a page to make room. The page replacement algorithm determines which page to evict. Optimal (OPT/Belady): replace the page that won't be used for the longest time in the future. Theoretically optimal (fewest page faults) but impossible to implement (requires knowing the future). Used as benchmark. FIFO (First In First Out): replace the oldest page in memory. Simple (just a queue). Problem: Belady's Anomaly — adding more frames can INCREASE page faults! Counter-intuitive. Example: with 3 frames, access pattern 1,2,3,4,1,2,5,1,2,3,4,5: 9 faults. With 4 frames: 10 faults! LRU (Least Recently Used): replace the page not used for the longest time. Uses past as predictor of future. No Belady's anomaly. Good approximation of OPT. Implementation: timestamp each page access, choose minimum timestamp for eviction. Expensive: hardware counter or stack per page access. Approximations used in practice. LRU approximations: Clock algorithm (second chance): FIFO but give each page a second chance using a reference bit. If bit=1 when page would be evicted, clear bit and skip to next page. Clock hand sweeps through frames. LFU (Least Frequently Used): replace page with lowest access count. Recent behavior not captured well. Aging can help. NFU (Not Frequently Used) / NRU (Not Recently Used): use reference and dirty bits. Prefer clean over dirty pages (saving dirty requires disk write). Linux uses: variant of clock algorithm with multiple lists (active, inactive).