4.1.17Computer Architecture (Deep)

Working set model, thrashing

2,115 words10 min readdifficulty · medium3 backlinks

WHY does this concept even exist?

The problem: Physical RAM is finite, but virtual memory pretends it's infinite by paging stuff to disk. The OS decides which pages stay in RAM. If it guesses wrong, every memory access becomes a disk access (a page fault), and disk is ~10510^510610^6× slower than RAM.

The observation that saves us: programs don't touch all their memory uniformly. They obey the principle of locality:

  • Temporal locality — a page used now is likely used again soon (loops, hot variables).
  • Spatial locality — if you touch address xx, you'll soon touch x+1x+1 (arrays, instruction streams).

So at any instant a program only needs a handful of pages "live". The working set model quantifies that handful so the OS can allocate frames intelligently.


WHAT is the working set?

  • Δ\Delta = the working-set window size (measured in memory references, not seconds).
  • W(t,Δ)|W(t,\Delta)| = the working set size = number of frames the process needs right now.

HOW do we compute W|W|? — a worked trace

Reference string (each number = a page touched): 2 6 1 5 7 7 7 5 1 6 2 3 42\ 6\ 1\ 5\ 7\ 7\ 7\ 5\ 1\ 6\ 2\ 3\ 4 \dots Take Δ=5\Delta = 5 (look back at the last 5 references including current).

tt ref last-5 window W(t,5)W(t,5) W\lvert W\rvert
5 7 2 6 1 5 7 {2,6,1,5,7} 5
6 7 6 1 5 7 7 {6,1,5,7} 4
9 1 7 7 5 1 _ {7,5,1} 3
11 2 1 6 2 _ _ {1,6,2} 3

Why this step? When references repeat (the run of 7s), the count of distinct pages drops — locality is high, fewer frames needed. When new pages stream in (phase change at the end), W|W| climbs again.


The allocation rule (HOW the OS uses it)

Let D=iWiD = \sum_i |W_i| = total demand = sum of working-set sizes over all runnable processes. Let mm = number of physical frames available.


WHAT is thrashing, precisely?

Figure — Working set model, thrashing

Page-Fault-Frequency (PFF): the practical control

Instead of computing exact working sets (expensive), bound the fault rate with two thresholds:


Worked example 2 — does this set thrash?

Three processes, frames m=12m = 12. WA=5, WB=4, WC=6|W_A| = 5,\ |W_B| = 4,\ |W_C| = 6.

Step 1: D=5+4+6=15D = 5+4+6 = 15. Why? Sum the live demands. Step 2: Compare: 15>1215 > 12thrashing. Why? Demand exceeds supply. Step 3 (fix): Suspend CC. New D=912D = 9 \le 12. Why CC? Removing it frees the most and brings DD comfortably under mm, leaving slack for the others. System now runs A and B cleanly; C resumes when a working set fits.



Recall Feynman: explain it to a 12-year-old

Imagine you do homework at a tiny desk (RAM) but keep all your books on a shelf across the room (disk). You only need the few books for the chapter you're on right now — that's your working set. If you and your siblings crowd the desk so nobody's chapter-books fit, everyone keeps running back and forth to the shelf and no one actually writes anything. That endless running is thrashing. The smart move is for one kid to leave the desk so the rest can finally work.


Active Recall

What is the working set W(t,Δ)W(t,\Delta)?
The set of distinct pages a process referenced in the last Δ\Delta memory references ending at virtual time tt.
What does W(t,Δ)|W(t,\Delta)| represent?
The number of frames the process currently needs to run without heavy faulting.
In what units is the working-set window Δ\Delta measured?
Memory references (virtual time), not seconds.
State the thrashing condition.
If total demand D=iWiD=\sum_i|W_i| exceeds available frames mm, the system thrashes.
What principle makes the working set a good predictor?
Locality of reference (temporal + spatial): recently/nearby-used pages are likely used soon.
Define thrashing.
A state where a process spends more time paging (servicing faults) than executing, collapsing CPU utilization.
Why does naively adding processes worsen thrashing?
Low CPU use is caused by everyone faulting; more processes steal frames, raise fault rate, and lower CPU use further — positive feedback.
What is the OS fix for thrashing?
Suspend/swap out a whole process to bring DmD \le m.
What does the Page-Fault-Frequency (PFF) scheme do when f>Uf>U?
The process is faulting too much — give it more frames.
What does PFF do when f<Lf<L?
The process has surplus frames — reclaim one.
Why is Δ\Delta too large bad?
It keeps stale pages resident, over-estimating the working set and wasting frames, lowering multiprogramming.

Connections

  • Demand Paging — working set decides which pages to keep resident.
  • Page Replacement Algorithms — LRU/Clock approximate the working set.
  • Locality of Reference — the empirical law the whole model rests on.
  • Multiprogramming Degree — bounded by Wim\sum|W_i| \le m.
  • TLB and Caches — same locality principle, smaller scale.
  • Page Fault Handling — faults are the events PFF measures.

Concept Map

justifies

part of

part of

size equals

frames needed now

limits

if D greater than m

storm of causes

disk 10^5 x slower

forecasts future via recent past

prevents

Principle of locality

Working set W t,Delta

Temporal locality

Spatial locality

Working set size |W|

Total demand D = sum |Wi|

Finite physical RAM m frames

Thrashing

Page fault: access hits disk

CPU stalls, no compute

OS allocates |Wi| frames

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, idea simple hai: ek program apni saari memory ek saath use nahi karta. Kisi bhi pal sirf thodi si pages "active" hoti hain — usko hum working set kehte hain. Yeh kaise pata chalta hai? Hum last Δ\Delta memory references dekhte hain (window). Jo distinct pages us window mein touch hui, wahi working set. Iska size W|W| batata hai ki process ko abhi kitne frames chahiye RAM mein.

Ab thrashing kya hai? Maan lo bahut saare processes chal rahe hain. Har ek ka working set add karo: D=WiD = \sum |W_i|. Agar yeh total RAM ke frames mm se zyada ho gaya (D>mD > m), toh sabki active pages RAM mein fit hi nahi hongi. Phir har memory access pe page fault, disk se page lao, kisi aur ki page nikaalo, wo bhi turant fault... CPU bas swapping mein laga rehta hai, kaam kuch nahi hota. Yahi thrashing hai — disk RAM se lakhon guna slow hai, isliye system reng-reng ke chalta hai.

Sabse khatarnaak baat: jab thrashing hoti hai toh CPU idle dikhta hai (sab fault wait kar rahe). Naive scheduler sochta hai "CPU khaali hai, ek aur process daal do" — aur cheez aur kharaab ho jaati hai! Yeh positive feedback loop graph mein "cliff" banata hai. Sahi fix ulta hai: ek process ko suspend kar do, taaki DmD \le m ho jaaye, baaki tezi se chalein.

Exam/coding tip: Δ\Delta seconds mein nahi, references mein hota hai. Aur PFF (page-fault-frequency) scheme practical hai — fault rate >U> U ho toh frame do, <L< L ho toh frame waapas lo. Bas itna yaad rakho: "D > m matlab DOOM."

Go deeper — visual, from zero

Test yourself — Computer Architecture (Deep)

Connections