Sub topic 3
Page Replacement Algorithms
This chapter assumes you know binary and hexadecimal numbers, basic computer organization (CPU, main memory, disk), and what a process is. When main memory is full, the operating system must evict a page to load a new one. The choice of victim page determines how many page faults occur.
Page Replacement Algorithms
A page fault happens when a process touches a page that is not in a frame. If no frame is free, the OS picks a resident page to evict. Three algorithms are standard for GATE problems.
- FIFO evicts the page loaded earliest.
- LRU (least recently used) evicts the page unused for the longest time.
- Optimal evicts the page whose next use is farthest in the future. It cannot be built in practice, but it gives a lower bound for comparison.
def fifo_faults(refs, frames):
memory, faults = [], 0
for p in refs:
if p not in memory:
faults += 1
if len(memory) == frames:
memory.pop(0) # evict the oldest loaded page
memory.append(p)
return faults
refs = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5]
print(fifo_faults(refs, 3), fifo_faults(refs, 4)) # 9 10On this reference string with 3 frames, FIFO gives 9 faults, LRU gives 10, and Optimal gives 7.
Belady's Anomaly and Stack Algorithms
Belady's anomaly is the case where adding frames increases the fault count. The code above shows it: FIFO faults 9 times with 3 frames but 10 times with 4.
A stack algorithm keeps the set of pages for m frames as a subset of the set for m+1 frames. LRU and Optimal are stack algorithms, so they never suffer this anomaly. FIFO is not, so it can.
Protection, Shared Pages and Copy-on-Write
Each page table entry has a valid bit and protection bits (read, write, execute). An access that violates them traps to the OS. Two processes can share a read-only page, such as program code, by pointing both page tables at the same frame.
Copy-on-write (CoW) extends sharing to writable pages. Both processes share the frame until one writes. That write triggers a fault, and the OS copies the page for the writer. Fork-style process creation relies on this.
Frame Allocation and Replacement Scope
Equal allocation gives each of n processes m/n frames. Proportional allocation gives process i about ai = (si / S) × m frames, where s_i is its size and S is the sum of all sizes. Local replacement evicts only pages of the faulting process. Global replacement lets it evict any process's page.
Thrashing and the Working-Set Model
Thrashing occurs when processes spend most of their time paging rather than running. The working-set model defines WS(Δ) as the pages referenced in the last Δ references. The OS should allocate at least the sum of working-set sizes, and suspend a process if the total exceeds the available frames.
GATE Problem-Solving Patterns
| Problem type | Method |
|---|---|
| Count faults for a string | Write memory state after each reference |
| Check Belady's anomaly | Compare FIFO faults at m and m+1 frames |
| Proportional allocation | Compute s_i / S × m, then round |
Exercise
With 3 frames and the reference string 1 2 3 4 1 2 5 1 2 3 4 5, how many faults does LRU produce?
Show answer
10 faults. The LRU code above returns 10 for 3 frames.