› Module 7: Virtual Memory › 7.1 Demand Paging
M O D U L E

Module 7: Virtual Memory

Demand paging, page replacement algorithms, and thrashing prevention

7.1 Demand Paging

Virtual Memory decouples the logical address space seen by user programs from physical RAM. In Demand Paging, pages are only loaded into memory when they are explicitly referenced during execution ("lazy swapper"), reducing memory pressure and start-up latencies.

Figure 7.1: The Complete Sequence of Events in Handling a Page Fault 1. Process references page 2. Invalid Bit → Trap to OS 3. Check validity & frame 4. Read Page from Disk into free physical frame 5. Update Page Table: Valid=1 6. Restart Faulting Instruction Pure Demand Paging: Process starts execution with 0 pages in memory; faults immediately on first instruction. Performance Critical: Disk I/O latency (~5ms) is 100,000× slower than RAM (~50ns)!

Figure 7.1: Complete sequence of events in handling a page fault, from the initial trap through disk read to instruction restart.

Effective Access Time (EAT)

Let $p$ be the page fault rate ($0 le p le 1$), $ma$ be memory access time (e.g. 100ns), and $PFT$ be page fault service time (e.g. 8ms = 8,000,000ns):

EAT = (1 − p) × ma + p × PFT

Even if $p$ is just 0.1% ($0.001$), memory access slows down by an enormous factor of 80×!

7.2 Page Replacement Algorithms

When a page fault occurs and no physical memory frame is free, the operating system must choose a resident page to evict to secondary storage — this is page replacement. The objective is to achieve the lowest page fault rate possible.

Figure 7.2: FIFO Page Replacement Traced on Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3 Ref: 7 0 1 2 0 3 0 4 2 3 F1 7 7 7 2 2 2 2 4 4 4 F2 0 0 0 0 3 3 3 2 2 F3 1 1 1 1 0 0 0 3 FIFO Page Faults: 9 / 10 Belady's Anomaly: In FIFO, allocating MORE physical frames can counterintuitively INCREASE page faults! Optimal Algorithm: 5 faults (Theoretical Best) LRU (Least Recently Used): Never suffers from Belady's Anomaly

Figure 7.2: FIFO replaces the oldest resident page; Optimal replaces the page unreferenced for the longest future duration; LRU approximates Optimal using the recent past.

Algorithm Comparisons

  • FIFO (First-In, First-Out): Evicts the page that has been in memory the longest. Susceptible to Belady's Anomaly.
  • Optimal (OPT / MIN): Replace the page that will not be used for the longest period of time in the future. Provably produces the lowest possible page fault count, but cannot be implemented in general-purpose OSs because future references cannot be known in advance.
  • LRU (Least Recently Used): Evicts the page that has not been referenced for the longest period of time. Uses the recent past as an approximation of the near future. Immune to Belady's Anomaly (it belongs to the class of stack algorithms).
  • Clock (Second-Chance) Algorithm: Practical hardware-assisted approximation of LRU. Operates on a circular list of frames using a 1-bit reference flag per frame.

7.3 Thrashing

If a process does not have enough physical memory frames to hold all pages in its active locality (its Working Set), it will trigger continuous page faults. Each fault evicts a page that is immediately needed again, causing the CPU to spend nearly 100% of its cycles servicing page-in and page-out I/O rather than executing instructions. This pathological condition is called Thrashing.

Figure 7.3: Thrashing Curve — CPU Utilization vs. Degree of Multiprogramming Degree of Multiprogramming (Number of Resident Processes) CPU Utilization Thrashing Point (Utilization collapses) More processes added → CPU work done Paging I/O dominates → CPU idle

Figure 7.3: As multiprogramming increases, CPU utilization rises until the available frames are exhausted. Beyond this threshold, thrashing sets in and utilization collapses.

Remedies for Thrashing

  • Working-Set Model: Proposed by Peter Denning. Tracks the set of pages referenced by a process in the most recent $Delta$ page references. A process is only admitted if all pages in its working set fit in available free frames.
  • Page-Fault Frequency (PFF): Directly measures each process's page fault rate. If the fault rate exceeds an upper threshold, allocate more frames to the process; if it drops below a lower threshold, reclaim unused frames.
MODULE COMPREHENSIVE QUIZ

Module 7 Final Quiz

Test your understanding across all 3 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 7 Final Quiz →
Yukti Mascot