8.5 Frame Allocation Policies & Thrashing (Working Set Model)
💡 Core Intuition
🍳 The Everyday Analogy: The Overbooked Emergency Room
Imagine an emergency hospital ward with a fixed capacity of 20 hospital beds:
The Emergency Ward Capacity Pipeline
Contrasting balanced patient admission with catastrophic system saturation
Healthy Multipatient Care
10 doctors treat 15 patients across the 20 available beds.
The Administrator Overbooks
Seeing brief doctor idle periods, the administrator admits 60 patients into the ward.
Complete Hospital Paralysis
Doctors spend 99% of their time wheeling gurneys back and forth between hallways and beds.
- Thrashing: The catastrophic operating state where a system spends vastly more time moving pages between RAM and disk than executing user instructions.
- The Root Cause: Too many processes admitted into memory (high degree of multiprogramming), starving each individual process of its minimum required execution frames.
💻 Bridging to Computer Science
When designing virtual memory subsystems, the operating system must solve two intertwined architectural challenges:
- Frame Allocation: How many physical frames should be allocated to each competing process?
- Thrashing Prevention: How does the kernel detect and prevent hyperactive swapping that drives CPU utilization to zero?
The Vicious Feedback Loop of Thrashing Collapse
How scheduler misconceptions trigger total system throughput degradation
RAM Saturation
Excessive multiprogramming reduces allocated frames per process below working sets.
Fault Explosion
Processes encounter continuous page faults and queue up waiting on swap I/O.
CPU Starvation
Ready queue empties. CPU utilization drops precipitously toward 0%.
Fatal Overload
Scheduler admits new processes to boost CPU usage, triggering total freeze.
The Vicious Feedback Loop of Thrashing
- As the OS increases the Degree of Multiprogramming (DoM), more processes share memory.
- Individual processes receive fewer frames than their active working set requires.
- Page fault rates surge across all processes.
- Processes transition from
RUNNINGtoBLOCKED / WAITINGwhile stalled on disk I/O. - The CPU runs out of ready threads and becomes idle.
- The naive OS CPU scheduler observes low CPU utilization and concludes: "The system is underutilized; let's admit more processes!"
- New processes demand even more frames page fault rates explode Total System Collapse.
📚 Core Deep-Dive & Concepts
1. Frame Allocation Strategies
The total number of frames allocated to a process must respect hardware constraints:
- Minimum Frame Bound: Determined by the Instruction Set Architecture (ISA). A single instruction may reference multiple memory operands. For instance, in an indirect addressing architecture with a two-operand instruction (
ADD @A, @B), the machine requires at least frames simultaneously resident to execute the instruction without an infinite fault loop. - Maximum Frame Bound: Bounded by total physical DRAM installed.
Worked Numerical Example: Proportional Allocation
Consider a system with physical frames available for user processes. Three processes are active:
- size:
- size:
- size:
Step 1: Compute Total Virtual Demand ():
Step 2: Calculate Frame Allocations ():
Verification: frames allocated cleanly!
2. Local vs Global Page Replacement
When a process encounters a page fault, from which pool of frames does it select a victim?
3. Resolving Thrashing: Peter Denning's Working Set Model
To eliminate thrashing, an operating system must understand the Locality Model: as a process executes, it moves through a sequence of execution localities.
In 1968, Peter J. Denning formulated the Working Set Model:
| Parameter | Working Set Window at | Working Set Window at |
|---|---|---|
| Reference Sequence () | 2, 6, 1, 5, 7, 7, 7, 5, 1, 6 | 3, 4, 4, 4, 3, 4, 3, 4, 4, 4 |
| Working Set () | ||
| **Working Set Size ($ | WS(t) | $)** |
| Memory Allocation Action | Allocate physical frames to maintain zero-fault execution | Dynamic shrink: Kernel safely reclaims surplus frames |
Definitions & Formulations
- Working Set Window (): A fixed parameter representing the most recent page references.
- Working Set of Process (): The set of unique pages referenced by process during the most recent window .
- Working Set Size (): The cardinality , representing the exact number of frames process actively needs right now.
- Total System Frame Demand ():
Thrashing Prevention Criterion
Let be the total physical frames in the system:
- Safe State (): Total demand does not exceed physical capacity. Allocate each process its working set frames. The system runs smoothly with negligible page faulting!
- Thrashing Imminent (): The aggregate working set exceeds physical RAM!
- Action: The operating system MUST suspend (swap out) one or more entire processes.
- Evicting an entire process frees all its frames, redistributing them to remaining processes so their working sets can fit into RAM.
- When memory pressure subsides, the suspended process is swapped back in.
4. Page Fault Frequency (PFF) Strategy
While the Working Set Model is mathematically elegant, tracking the exact working set on every memory reference is computationally expensive. Operating systems approximate it using Page Fault Frequency (PFF):
🏭 In The Real World: Production Case Study
Linux Out-of-Memory (OOM) Killer & PSI (Pressure Stall Information)
In high-density cloud environments (Kubernetes, AWS EC2, Google Borg), thrashing causes severe economic damage by locking server nodes:
Linux Memory Protection Pipeline
Multi-tier telemetry and intervention hierarchy preventing thrashing in production
Pressure Stall Information (PSI)
Tracks exact percentage of CPU time spent stalled on memory and swap I/O via /proc/pressure/memory.
systemd-oomd Mitigation
Proactively terminates entire runaway cgroups and container slices before the kernel locks up.
Kernel OOM Killer
Emergency kernel routine computing oom_score to forcibly SIGKILL the largest offending process.
- PSI (Pressure Stall Information):
- Modern Linux kernels expose
/proc/pressure/memory. If threads spend of their wall-clock time stalled waiting on memory/swap I/O, the kernel flags a Memory Pressure Event.
- Modern Linux kernels expose
- The OOM Killer:
- If thrashing cannot be cured by swapping, the kernel calculates an
oom_scorefor every process: - The kernel abruptly terminates (
SIGKILL) the rogue process with the highest score (typically a runaway browser tab or memory-leaking Python container), instantly reclaiming gigabytes of frames and saving the host machine.
- If thrashing cannot be cured by swapping, the kernel calculates an
🎯 Exam & Interview Pitfall Check
Question 1: Define "Thrashing" in an operating system. What are its primary causes and primary symptoms? Answer:
- Definition: Thrashing is the condition where a process or the entire system spends more time paging (swapping pages in and out of disk) than executing actual program instructions.
- Primary Symptoms:
- Page fault rate spikes dramatically.
- Disk I/O queues become completely saturated.
- CPU utilization drops toward zero because almost all ready processes are blocked waiting for disk I/O to complete.
- Primary Causes:
- Overcommitting physical memory by admitting too many processes (excessive degree of multiprogramming).
- The sum of the working sets of all active processes exceeds the total number of physical frames available in RAM ().
Question 2: In the Working Set Model, how does the size of the window parameter affect system performance? Answer:
- If is chosen TOO SMALL:
- The window will not capture the entire locality of the process.
- Active pages needed by the current loop or function will be prematurely dropped from the working set, causing continuous page faults.
- If is chosen TOO LARGE:
- The window will encompass pages from prior localities that are no longer needed.
- Memory will be over-allocated to the process, artificially inflating and reducing the degree of multiprogramming.
- If :
- The working set becomes the set of all pages ever referenced throughout the entire execution history of the process!
- The "CPU is Busy During Thrashing" Fallacy: Many students believe the CPU is at 100% load during thrashing. In reality, CPU utilization drops toward 0%! The CPU is starved of executable instructions because all processes are suspended waiting for disk controllers to swap pages.
- Confusing Page Replacement with Process Suspension: Page replacement cannot solve thrashing if total memory demand exceeds physical RAM! To stop thrashing, the OS must suspend an entire process (swapping it to disk) to reduce the degree of multiprogramming.