8.2 Page Replacement Algorithms: First-In, First-Out (FIFO) & Belady's Anomaly
💡 Core Intuition
🍳 The Everyday Analogy: The Bakery Display Case
Imagine a boutique bakery with a counter display case that holds only three dessert trays:
The Bakery Tray Replacement Pipeline
Contrasting arrival-time eviction with customer demand patterns
Strict Chronological Queue
When a customer requests a chocolate croissant not on the counter, the baker throws away the tray that arrived earliest in the morning.
Adding a Fourth Shelf
The bakery expands the display case from 3 shelves to 4 shelves to satisfy more customers.
Lack of Stack Property
FIFO does not guarantee that the set of items held in N shelves is a subset of N+1 shelves.
- First-In, First-Out (FIFO): The simplest page replacement policy. When a frame is needed, the page that has resided in physical memory the longest is evicted.
- Belady's Anomaly: The counter-intuitive phenomenon where increasing the number of physical memory frames causes the total number of page faults to increase rather than decrease!
💻 Bridging to Computer Science
When physical memory is fully saturated and a new page is referenced, the operating system must choose a victim page to evict.
The ideal eviction goal is simple: Evict the page that is least likely to be needed in the near future.
However, because the operating system cannot predict the future, it must use heuristics. The most elementary heuristic is First-In, First-Out (FIFO):
- Track the arrival timestamp of each page in memory.
- When a replacement is required, evict the oldest page.
- While trivial to implement with a FIFO circular queue, FIFO frequently discards heavily used core utility pages simply because they were loaded early during process startup.
📚 Core Deep-Dive & Concepts
1. The FIFO Page Replacement Algorithm
Algorithm Mechanics
- Queue Maintenance: The OS maintains a FIFO queue of all physical frames currently resident in RAM.
- On Page Fault:
- If free frames exist: Load the incoming page into a free frame and push the frame index to the tail of the queue.
- If memory is full: Pop the frame index from the head of the queue (the oldest page). Evict that page, load the incoming page into that frame, and append the frame index to the tail.
- On Page Hit:
- The page already resides in memory. The FIFO queue is completely unaffected! Arrival timestamps do not change on read or write references.
2. Comprehensive 20-Reference Trace
Let us trace the classic 20-reference workload with allocated physical frames:
| Step | Page Ref | Frame 1 | Frame 2 | Frame 3 | Event | Victim Evicted | Oldest Page in Queue |
|---|---|---|---|---|---|---|---|
| 1 | 7 | 7 | — | — | Fault | None (Cold) | 7 |
| 2 | 0 | 7 | 0 | — | Fault | None (Cold) | 7 |
| 3 | 1 | 7 | 0 | 1 | Fault | None (Cold) | 7 |
| 4 | 2 | 2 | 0 | 1 | Fault | 7 | 0 |
| 5 | 0 | 2 | 0 | 1 | Hit | None | 0 |
| 6 | 3 | 2 | 3 | 1 | Fault | 0 | 1 |
| 7 | 0 | 2 | 3 | 0 | Fault | 1 | 2 |
| 8 | 4 | 4 | 3 | 0 | Fault | 2 | 3 |
| 9 | 2 | 4 | 2 | 0 | Fault | 3 | 0 |
| 10 | 3 | 4 | 2 | 3 | Fault | 0 | 4 |
| 11 | 0 | 0 | 2 | 3 | Fault | 4 | 2 |
| 12 | 3 | 0 | 2 | 3 | Hit | None | 2 |
| 13 | 2 | 0 | 2 | 3 | Hit | None | 2 |
| 14 | 1 | 0 | 1 | 3 | Fault | 2 | 3 |
| 15 | 2 | 0 | 1 | 2 | Fault | 3 | 0 |
| 16 | 0 | 0 | 1 | 2 | Hit | None | 0 |
| 17 | 1 | 0 | 1 | 2 | Hit | None | 0 |
| 18 | 7 | 7 | 1 | 2 | Fault | 0 | 1 |
| 19 | 0 | 7 | 0 | 2 | Fault | 1 | 2 |
| 20 | 1 | 7 | 0 | 1 | Fault | 2 | 7 |
Performance Summary for 3 Frames:
- Total References:
- Total Page Faults:
- Total Page Hits:
- Hit Ratio:
- Fault Ratio:
3. Belady's Anomaly: The Memory Paradox
Intuition dictates that giving a process more physical RAM frames should either decrease the number of page faults or, in the worst case, leave it unchanged:
In 1969, László Bélády proved that for certain replacement algorithms, this assumption is false!
Definition of Belady's Anomaly:
For certain page replacement algorithms (such as FIFO), the page fault rate may increase as the number of allocated physical frames increases for the exact same memory reference string.
4. Mathematical Demonstration of Belady's Anomaly
Consider the canonical 12-reference sequence:
Case A: FIFO with 3 Physical Frames
| Ref | 1 | 2 | 3 | 4 | 1 | 2 | 5 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| F1 | 1 | 1 | 1 | 4 | 4 | 4 | 5 | 5 | 5 | 5 | 5 | 5 |
| F2 | — | 2 | 2 | 2 | 1 | 1 | 1 | 1 | 1 | 3 | 3 | 3 |
| F3 | — | — | 3 | 3 | 3 | 2 | 2 | 2 | 2 | 2 | 4 | 4 |
| Hit/Fault | F | F | F | F | F | F | F | Hit | Hit | F | F | Hit |
- Total Page Faults with 3 Frames: (3 Hits: at references
1,2, and5).
Case B: FIFO with 4 Physical Frames (More Memory Allocated!)
| Ref | 1 | 2 | 3 | 4 | 1 | 2 | 5 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| F1 | 1 | 1 | 1 | 1 | 1 | 1 | 5 | 5 | 5 | 5 | 4 | 4 |
| F2 | — | 2 | 2 | 2 | 2 | 2 | 2 | 1 | 1 | 1 | 1 | 5 |
| F3 | — | — | 3 | 3 | 3 | 3 | 3 | 3 | 2 | 2 | 2 | 2 |
| F4 | — | — | — | 4 | 4 | 4 | 4 | 4 | 4 | 3 | 3 | 3 |
| Hit/Fault | F | F | F | F | Hit | Hit | F | F | F | F | F | F |
- Total Page Faults with 4 Frames: (Only 2 Hits: at references
1and2).
| Allocated Physical Frames | Total Page Faults | Page Hits | Performance Outcome |
|---|---|---|---|
| Baseline reference for string | |||
| Faults (Worse performance despite physical frames!) |
By adding an extra physical frame, the page fault count increased from 9 to 10!
5. Why Does Belady's Anomaly Occur? The Stack Property
To understand why FIFO fails, computer scientists classify page replacement algorithms into two categories:
🏭 In The Real World: Production Case Study
Second-Chance (Clock) Algorithm in Linux & BSD
Pure FIFO is rarely used in production kernels because of its terrible performance and Belady vulnerability. Instead, operating systems deploy the Second-Chance (Clock) Algorithm:
Second-Chance (Clock) Circular Pointer Traversal
Circular sweep evaluating hardware reference bits (R) before selecting eviction victim
Frame 0 (R = 1)
Recently accessed. Hand clears bit to R = 0 (Second Chance granted).
Frame 1 (R = 0)
Not accessed since last sweep. Selected as eviction victim!
Frame 2 (R = 1)
Active working-set page. Kept resident; reference bit cleared on next pass.
Frame 3 (R = 0)
Cold page awaiting next scan cycle if no immediate replacement occurs.
- How Clock Operates:
- Arranges physical frames in a circular circular buffer (a clock face).
- A hand points to the oldest frame.
- When eviction is required, the hand inspects the Reference Bit () set by hardware:
- If : Clear , advance hand to next frame (give it a "second chance").
- If : Evict this frame immediately!
- Production Balance:
- Preserves the queue simplicity of FIFO while approximating the hit performance of LRU and dramatically suppressing Belady-style pathologies.
🎯 Exam & Interview Pitfall Check
Question 1: Which of the following page replacement algorithms can suffer from Belady's Anomaly?
- Optimal (OPT)
- Least Recently Used (LRU)
- First-In, First-Out (FIFO)
- Most Frequently Used (MFU)
Answer:
- FIFO (and algorithms that do not satisfy the stack property, such as pure FIFO and Random replacement) suffers from Belady's Anomaly.
- OPT and LRU are certified Stack Algorithms and can never suffer from Belady's Anomaly under any circumstance.
Question 2: Explain the formal definition of a "Stack Algorithm" in Operating Systems memory management. Answer: A page replacement algorithm is defined as a Stack Algorithm if for any given reference string and for all time , the set of pages resident in physical memory with frames, denoted , is a strict subset of the set of pages resident in physical memory with frames, denoted :
Because any page present in the -frame system is guaranteed to also be present in the -frame system, adding frames can never convert a page hit into a page fault. Hence, stack algorithms are provably immune to Belady's Anomaly.
- The "More Memory Always Means Fewer Faults" Trap: When asked if adding RAM always reduces page faults, the answer is NO. Under FIFO replacement, Belady's Anomaly can cause page faults to increase.
- The Hit-Update Misconception in FIFO: In FIFO, when a page reference results in a Hit, DO NOT change its queue position! Its arrival timestamp remains the original time it was first loaded. Moving it to the tail converts the algorithm into LRU!