# Page Replacement (FIFO, LRU, Optimal), Unix Inodes & Disk Arm Scheduling
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Operating Systems
Canonical URL: https://gateandnet.com/exams/gate-cs/operating-systems/page-replacement-inodes-and-disk-scheduling

> FIFO Belady’s anomaly vs stack algorithms (LRU/Optimal), Unix inode maximum file size formula, and FCFS / SSTF / SCAN / C-SCAN / C-LOOK disk head movement numericals.

## 1. Page Replacement Algorithms & Stack Property

| Algorithm | Evicts Page... | Suffers Belady’s Anomaly? | Notes |
| :--- | :--- | :--- | :--- |
| **FIFO** | Oldest arrival in RAM | **YES!** (Classic string `1,2,3,4,1,2,5,1,2,3,4,5`: 9 faults on 3 frames vs **10 faults** on 4 frames!) | Simple queue |
| **Optimal (Belady MIN)** | Used farthest in the **future** | **No** (Stack algorithm) | Theoretical minimum page faults; unimplementable online |
| **LRU (Least Recently Used)** | Unused longest in the **past** | **No** (Stack algorithm: $S_m(t) \subseteq S_{m+1}(t)$) | Approximated in hardware via Reference Bit / Clock Second-Chance |

## 2. Disk Arm Scheduling Algorithms

Suppose cylinders are numbered $0 \dots 199$, head starts at **$53$** moving toward $0$, and queue = $\{98, 183, 37, 122, 14, 124, 65, 67\}$:
- **FCFS:** Services strictly in arrival order (high wild swings, fair, zero starvation).
- **SSTF (Shortest Seek Time First):** Always moves to closest pending cylinder from current head ($53 \to 65 \to 67 \to 37 \to 14 \to 98 \to 122 \to 124 \to 183$); **can cause starvation** of far cylinders!
- **SCAN (Elevator):** Moves all the way to physical boundary **$0$** (or $199$) even if no request sits at $0$, then reverses!
- **LOOK / C-LOOK:** Reverses or jumps right at the **last requested cylinder** without touching empty boundary $0$ / $199$.

> [!TRAP]
> **SCAN vs LOOK Boundary Trap:** In GATE NAT questions, **SCAN** MUST touch cylinder $0$ (or $M-1$) before reversing direction, adding extra cylinder distance $2 \times d_{\text{edge}}$, whereas **LOOK** reverses immediately at the extremal request!

## Solved Practice Questions & PYQs

### [GATE CS OS | MCQ]
Which of the following page replacement algorithms can suffer from **Belady’s Anomaly** (more page faults when allocated more physical page frames)?
**Correct Answer:** C
**Solution:** **FIFO** is not a stack algorithm ($S_m \not\subseteq S_{m+1}$) and exhibits Belady’s Anomaly on reference strings like `1,2,3,4,1,2,5,1,2,3,4,5` (9 faults with 3 frames vs 10 faults with 4 frames). Both **LRU** and **Optimal** satisfy the inclusion (stack) property and never suffer from Belady’s Anomaly.

### [GATE CS OS | MCQ]
A disk head is currently at cylinder $50$ and pending requests are at cylinders $\{82, 170, 43, 140, 24, 16\}$. Under **SSTF (Shortest Seek Time First)**, which cylinder is serviced **first** and **second**?
**Correct Answer:** B
**Solution:** From $50$, distances are $|50 - 43| = 7$ vs $|50 - 82| = 32$, so **$43$** is serviced first. From $43$, distances are $|43 - 24| = 19$ vs $|43 - 82| = 39$, so **$24$** is serviced second.
