4.2.14 · HinglishOperating Systems
Critical section — mutual exclusion, progress, bounded waiting
4.2.14· Coding › Operating Systems
WHAT is a critical section?
WHY do we need it? Kyunki concurrent unsynchronized access se race condition create hoti hai — final result instructions ke unlucky interleaving par depend karta hai.
The THREE requirements (WHY each exists)
DERIVING a correct lock from scratch
Hum step-by-step build karte hain, har tute hue attempt ko fix karte hue (Steel-man your mistakes).
Attempt 1 — single turn variable (strict alternation)
turn = 0
// Process i (i = 0 or 1)
while (turn != i) ; // entry: busy-wait
CS
turn = 1 - i // exit: hand over
- ✅ Mutual exclusion:
turnexactly ek value hold karta hai. - ❌ Progress fails: agar P0 ki turn hai lekin P0 apne remainder mein busy hai (CS nahi chahta), toh P1 block ho jaata hai chahe CS free ho. Forced alternation = no progress.
Attempt 2 — flag[] (intent flags)
flag[i] = true // "I want in"
while (flag[j]) ; // wait while other wants in
CS
flag[i] = false
- ✅ Mutual exclusion hold karta hai.
- ❌ Deadlock: dono simultaneously
flag = trueset kar lete hain, phir dono forever spin karte hain. Progress fail.
Attempt 3 — Peterson's solution (combine intent + courtesy)
Peterson's correctness ka sketch proof:
- Mutual exclusion: Dono ke andar hone ke liye, dono
whilese pass hue hote. P_i pass hua matlabflag[j]==falseORturn==i. Kyunkiflag[i]=flag[j]=true(dono chahte hain), yeh reduce hota haiturn==iP_i ke liye aurturn==jP_j ke liye — impossible,turnek value hai. ⇒ dono enter nahi kar sakte. - Progress: Ek waiting P_i sirf tab stuck hai jab
flag[j] && turn==j. Agar P_j CS nahi chahta,flag[j]=false→ P_i enter karta hai. Toh ek free CS kabhi blocked nahi hoti. ✅ - Bounded waiting: Jab P_j exit karke re-enter karta hai, woh
turn=iset karta hai, toh P_i (jiska abturn==ihai) next mein guaranteed andar jaata hai. Bound = 1. ✅

Hardware help — atomic instructions
Software (Peterson) assume karta hai ki loads/stores atomic aur ordered hain. Real CPUs atomic read-modify-write primitives dete hain taki hume yeh manually na karna pade.
Worked example: which property fails?
Flashcards
What are the three requirements of a critical-section solution?
Mutual exclusion, progress, bounded waiting.
Define mutual exclusion.
Agar ek process apni CS mein hai, toh koi doosri process us waqt apni CS mein nahi ho sakti.
Define progress.
Jab koi process CS mein nahi hoti, to yeh decision ki agli kaun enter kare indefinitely postpone nahi ho sakta, aur sirf contending (non-remainder) processes participate karti hain.
Define bounded waiting.
Ek finite bound hota hai ki kitni baar doosri processes apni CS enter kar sakti hain jab ek process entry request kar chuki ho lekin usse grant abhi nahi hua.
What is a race condition?
Jab outcome shared data ke concurrent accesses ke order/timing (interleaving) par depend kare.
Why does strict alternation (turn only) fail?
Progress violate hoti hai — ek process ko tab bhi apni turn ka wait karna padta hai jab doosre ko CS nahi chahiye.
Why does the two-flag attempt deadlock?
Dono apna flag true set kar lete hain, phir har ek doosre ka flag clear hone ka forever wait karta hai.
The key trick in Peterson's algorithm?
turn = other set karo (yield karo), taki turn ka last writer wait kare — ties deterministically break hoti hain.Peterson's wait condition?
while (flag[j] && turn == j);What does test_and_set return and do?
Atomically lock ki old value return karta hai aur use true set kar deta hai.
Why does plain test_and_set lack bounded waiting?
Ek process ko baar baar overtake kiya ja sakta hai; bound karne ke liye fair queue add karo.
Why is while(lock); lock=true; unsafe?
Check aur set atomic nahi hain — dono processes CS mein saath enter kar sakti hain (ME fail).
Bounded-waiting bound for n processes with FCFS?
n − 1.
Recall Feynman: explain to a 12-year-old
Ek toy hai aur do bacche. Rules taaki koi na roye:
- Sirf ek baccha ek waqt mein toy se khelega (mutual exclusion).
- Agar koi baccha toy chahta hai aur koi use nahi kar raha, toh use mil jaaye — yeh silly "wait, it's not your turn" wali baat nahi jab turn-baccha room se chala gaya (progress).
- Kisi ko forever wait nahi karna padega — kuch rounds ke baad apni turn pakki hai (bounded waiting). Peterson ka trick polite bacche ka rule hai: "Main chahta hoon, lekin tum pehle jao." Kyunki dono kehte hain "tum pehle," jo last mein kehta hai woh actually wait karta hai, aur doosra khelta hai — aur woh clean turns lete hain.
Connections
- Race conditions
- Peterson's algorithm
- Semaphores — in guarantees par built higher-level synchronization
- Mutex locks aur Spinlocks
- Test-and-Set and Compare-and-Swap — atomic hardware primitives
- Deadlock — jo progress entry par rokta hai; broader resource version alag jagah
- Starvation and Fairness — jo bounded waiting rokta hai
- Producer-Consumer problem / Dining Philosophers — applications