4.1.17 · HinglishComputer Architecture (Deep)

Working set model, thrashing

1,945 words9 min readRead in English

4.1.17 · Coding › Computer Architecture (Deep)


YEH concept exist kyon karta hai?

Problem: Physical RAM finite hai, lekin virtual memory paging ki wajah se pretend karti hai ki woh infinite hai. OS decide karta hai ki kaun se pages RAM mein rehte hain. Agar woh galat guess kare, toh har memory access ek disk access ban jaati hai (ek page fault), aur disk RAM se ~× slower hoti hai.

Woh observation jo hume bachata hai: Programs apni saari memory uniformly nahi touch karte. Woh principle of locality follow karte hain:

  • Temporal locality — abhi use hua page jald hi dobara use hone ki sambhavna hai (loops, hot variables).
  • Spatial locality — agar aap address touch karte ho, toh jaldi hi touch karoge (arrays, instruction streams).

Toh kisi bhi instant mein ek program ko sirf ek mutthi bhar pages "live" chahiye hote hain. Working set model us mutthi ko quantify karta hai taaki OS frames intelligently allocate kar sake.


Working set KYA hai?

  • = working-set window size (memory references mein measure hoti hai, seconds mein nahi).
  • = working set size = frames ki number jo process ko abhi chahiye.

compute kaise karte hain? — ek worked trace

Reference string (har number = ek touched page): lo (last 5 references dekhna including current).

ref last-5 window
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

Yeh step kyon? Jab references repeat hote hain (7s ka run), distinct pages ki count girती है — locality high hai, kam frames chahiye. Jab naye pages stream in hote hain (end mein phase change), phir se badhta hai.


Allocation rule (OS isko KAISE use karta hai)

Maano = total demand = saare runnable processes ke working-set sizes ka sum. Maano = available physical frames ki number.


Thrashing precisely KYA hai?

Figure — Working set model, thrashing

Page-Fault-Frequency (PFF): practical control

Exact working sets compute karne ki jagah (expensive), do thresholds se fault rate bound karo:


Worked example 2 — kya yeh set thrash karega?

Teen processes, frames . .

Step 1: . Kyon? Live demands sum karo. Step 2: Compare karo: thrashing. Kyon? Demand supply se zyada hai. Step 3 (fix): ko suspend karo. Naya . kyon? Ise remove karna sabse zyada free karta hai aur ko se comfortably neeche laata hai, dusron ke liye slack chhod ke. System ab A aur B cleanly run karta hai; C tab resume karta hai jab ek working set fit ho.



Recall Feynman: 12-saal ke bacche ko explain karo

Socho tum ek tiny desk (RAM) pe homework karte ho lekin apni saari books dusre kamre mein shelf (disk) pe rakhte ho. Tumhe sirf woh kuch books chahiye jo tumhare abhi ke chapter ke liye hain — woh tumhara working set hai. Agar tum aur tumhare siblings desk pe itna crowd karo ki kisi ke chapter-books fit nahi ho, toh sab shelf ke paas baar-baar daudne lagte hain aur koi actually kuch likhta nahi. Woh endless daudna thrashing hai. Smart move hai ki ek baccha desk chhode taaki baaki finally kaam kar sakein.


Active Recall

What is the working set ?
Virtual time pe khatam hone wale last memory references mein ek process ne jo distinct pages reference kiye, unka set.
What does represent?
Frames ki number jo process ko abhi heavy faulting ke bina run karne ke liye chahiye.
In what units is the working-set window measured?
Memory references (virtual time) mein, seconds mein nahi.
State the thrashing condition.
Agar total demand available frames se zyada ho, toh system thrash karta hai.
What principle makes the working set a good predictor?
Locality of reference (temporal + spatial): recently/nearby-use kiye gaye pages jald hi use hone ki sambhavna hoti hai.
Define thrashing.
Ek state jahan ek process executing se zyada time paging (faults service karne) mein bitata hai, CPU utilization collapse kar deta hai.
Why does naively adding processes worsen thrashing?
Low CPU use isliye hoti hai ki sab fault kar rahe hain; zyada processes frames chheente hain, fault rate badhate hain, aur CPU use aur girte hain — positive feedback.
What is the OS fix for thrashing?
Ek puri process ko suspend/swap out karo taaki ho jaye.
What does the Page-Fault-Frequency (PFF) scheme do when ?
Process bahut zyada fault kar raha hai — use aur frames do.
What does PFF do when ?
Process ke paas surplus frames hain — ek reclaim karo.
Why is too large bad?
Yeh stale pages ko resident rakhta hai, working set over-estimate karta hai aur frames waste karta hai, multiprogramming ghataata hai.

Connections

  • Demand Paging — working set decide karta hai kaun se pages resident rakhne hain.
  • Page Replacement Algorithms — LRU/Clock working set approximate karte hain.
  • Locality of Reference — woh empirical law jis par poora model depend karta hai.
  • Multiprogramming Degree se bounded hai.
  • TLB and Caches — same locality principle, chhote scale par.
  • Page Fault Handling — faults woh events hain jo PFF measure karta hai.

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