Quiz 2

Virtual Memory — Demand Paging, Page Replacement

878 words
4 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Virtual Memory — Demand Paging, Page Replacement ## 🎯 Learning Objectives - Explain demand paging and how page faults trigger loading - Trace FIFO, LRU, Optimal, and Clock page replacement algorithms - Identify Belady's Anomaly and explain why it occurs - Calculate effective access time with page faults - Underst...

Virtual Memory — Demand Paging, Page Replacement

🎯 Learning Objectives

  • Explain demand paging and how page faults trigger loading
  • Trace FIFO, LRU, Optimal, and Clock page replacement algorithms
  • Identify Belady's Anomaly and explain why it occurs
  • Calculate effective access time with page faults
  • Understand thrashing and the working set model

1. Demand Paging

1.1 Intuition

Demand paging loads pages only when they are needed, not before. Like a lazy student who only studies topics the night before an exam — you save time if some pages are never needed.

1.2 Page Fault Handling

(Diagram)

1.3 Effective Access Time (EAT) with Page Faults

\text{EAT} = (1-p) \times \text{memory_access} + p \times (\text{page_fault_time})
Where pp = page fault rate. Example: Memory access = 200ns, Page fault time = 8ms (8,000,000ns)
Page Fault RateEATPerformance Degradation
0 (none)200 ns1x (ideal)
0.001 (1 in 1000)200 + 0.001 × 8,000,000 = 8200 ns41x slower
0.01 (1 in 100)200 + 0.01 × 8,000,000 = 80,200 ns401x slower
Key Insight: Even 1 fault per 1000 accesses slows the system by 41×. Page replacement must be highly optimized.

2. Page Replacement Algorithms

2.1 Common Setup for All Examples

Page reference string7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
Frames3
GoalMinimize page faults

2.2 First-In, First-Out (FIFO)

Replace the page that has been in memory the longest. (Diagram) Tracing Table (3 frames):
Ref70120304230321201701
F077722224440000000777
F10000333222221111100
F2111100033333333331
Fault?
Page faults = 15 Belady's Anomaly: Adding more frames can increase page faults!
FramesFIFO Faults
315
416 (more faults with more memory!)

2.3 Optimal Page Replacement (OPT)

Replace the page that will not be used for the longest time in the future. Provably optimal — used as a benchmark. Tracing Table (3 frames):
Ref70120304230321201701
F077722222222227777777
F10000004440000000000
F2111333333331111111
Fault?
Page faults = 9 (optimal benchmark)

2.4 Least Recently Used (LRU)

Replace the page that has not been used for the longest time. Uses past history as a proxy for future use. Tracing Table (3 frames):
Ref70120304230321201701
F077722224440001111111
F10000000000000000000
F2111333233322222777
Fault?
Page faults = 12

2.5 Clock (Second-Chance) Algorithm

An approximation of LRU with lower overhead. Pages have a reference bit set by the MMU on each access. The Clock hand sweeps through frames, giving a "second chance" to pages with ref bit = 1. (Diagram) Tracing (3 frames):
StepFrames (ref bit)Hand PositionVictim
17(1), -, -0
27(1), 0(1), -1
37(1), 0(1), 1(1)2
4 (ref 2)7(0), 0(1), 1(1)07 (ref was 0)
52(1), 0(1), 1(1)1

3. Frame Allocation

3.1 Allocation Strategies

StrategyDescriptionProsCons
EqualEach process gets same numberFairWastes memory for small processes
ProportionalAllocate proportional to process sizeEfficientComplex
PriorityHigher priority → more framesPerformanceCan starve low-priority

3.2 Thrashing

Thrashing occurs when a process doesn't have enough frames, causing constant page faults. The CPU is busy handling page faults instead of executing instructions. Working Set Model: A process needs its working set (set of pages currently in use) in memory to avoid thrashing.
Working Set Size (WSS)=Pages accessed in last Δ references\text{Working Set Size (WSS)} = \text{Pages accessed in last } \Delta \text{ references}
If total WSS > physical memory → thrashing.

4. Algorithm Comparison

AlgorithmImplementation CostFault RateBelady's Anomaly?Practical?
FIFOVery lowHighYesRarely
OptimalImpossible (needs future)LowestNoBenchmark only
LRUHigh (full stack / counters)LowNoHardware-limited
ClockMedium (ref bit)Medium-LowNoMost common
LFUMedium (counter)MediumNoCan suffer from stale data

5. 📝 Practice Questions

Q1: For the reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 with 3 frames, compute FIFO page faults.
Answer: 9 faults. Steps: 1(✓),2(✓),3(✓),4(replaces 1,✓),1(replaces 2,✓),2(replaces 3,✓),5(replaces 4,✓),1(replaces 1,✓? Actually let me trace...)
Proper trace: Frames: [1][2][3]; t=4: [4][2][3] (victim 1); t=1: [4][1][3] (victim 2); t=2: [4][1][2] (victim 3); t=5: [5][1][2] (victim 4); t=1: [5][1][2] (hit); t=2: [5][1][2] (hit); t=3: [5][3][2] (victim 1 → 3); t=4: [5][3][4] (victim 2); t=5: [5][3][4] (hit). Total faults = 9. Q2: What is Belady's Anomaly?
Answer: Belady's Anomaly is the counterintuitive observation that for FIFO, increasing the number of frames can increase the page fault rate. For the reference string 1,2,3,4,1,2,5,1,2,3,4,5: 3 frames → 9 faults, 4 frames → 10 faults. OPT and LRU do not exhibit this anomaly. Q3: Why is LRU considered "good but expensive"?
Answer: LRU requires tracking the exact order of page accesses, which requires either a hardware stack (expensive to update on every access) or counters/registers for each page. The Clock algorithm approximates LRU with just a reference bit, which is much cheaper. Q4: Calculate EAT for page fault rate 0.0001, memory access 100ns, page fault service time 10ms.
Answer: EAT = (1-0.0001)×100 + 0.0001×10,000,000 = 99.99 + 1000 = 1100 ns. The page fault time dominates despite the low rate. Q5: What is thrashing and how does the working set model prevent it?
Answer: Thrashing occurs when a process's working set (pages actively used) exceeds its allocated frames, causing constant page faults. The working set model monitors the pages each process accesses within a window Δ, and ensures that each process is allocated enough frames to hold its working set.

6. 🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.