4.2.20 · Coding › Operating Systems
Deadlock bilkul four cars stuck at a 4-way intersection jaisa hai — har car apni right wali car ka wait kar rahi hai ki woh move kare. Koi jaa nahi sakta, koi peeche nahi hata. Prevention ki trick simple but powerful hai: deadlock ke liye charon conditions simultaneously hold karni padti hain (Coffman conditions). Agar aap system ko aisa design karo ki unme se ek bhi kabhi hold na kar sake , toh deadlock mathematically impossible ho jaata hai — "unlikely" nahi, impossible .
Definition The 4 Coffman Conditions (deadlock ke liye SABHII hold karni chahiye)
Mutual Exclusion — ek resource non-shareable mode mein hold hota hai (ek time pe sirf ek hi process use kar sakti hai).
Hold and Wait — ek process kam se kam ek resource hold karte hue doosron ka wait karti hai .
No Preemption — resource forcibly nahi chhina jaa sakta ; woh sirf voluntarily release hota hai.
Circular Wait — processes ka ek cycle hota hai, jahan har ek process us resource ka wait karti hai jo next wali process hold kar rahi hai.
Deadlock Prevention = system ko aisa design karo ki IN MEIN SE EK BHI kabhi true na ho sake.
Intuition "ek TODNO" kaafi kyun hai
Deadlock ke liye charon ka logical AND chahiye:
Deadlock ⟹ ( C 1 ∧ C 2 ∧ C 3 ∧ C 4 )
Contrapositive se, agar koi bhi C i false hai, toh conjunction false hai, isliye deadlock ho hi nahi sakta:
¬ C i ⟹ ¬ ( C 1 ∧ C 2 ∧ C 3 ∧ C 4 ) ⟹ ¬ Deadlock
Yahi prevention ka poora mathematical justification hai. Ek link toot gaya toh poori chain toot jaati hai.
Worked example Technique: resources ko shareable banao
Kaise: Spooling use karo (jaise printer spooler) ya read-only / lock-free data taaki kai processes resource ko "share" kar sakein.
Kyun kaam karta hai: agar kisi ko bhi exclusive hold ki zaroorat nahi, toh exclusivity pe waiting hi nahi hogi.
Kyun limited hai: Kuch resources intrinsically non-shareable hote hain (printer mid-page, write-lock). Inhe shareable nahi banaya jaa sakta, isliye yeh condition generally break karna impossible hota hai .
Worked example Technique: wait karte hue kabhi hold mat karo
Do designs hain:
All-or-nothing (sab kuch pehle hi maango): ek process ko start hone se pehle saare zaroorat wale resources request karne padte hain; ya toh atomically sab milenge, ya kuch nahi.
Release-before-request: ek process ko nayi resources maangne se pehle apne saare held resources release karne padte hain.
Kyun kaam karta hai: agar aap kabhi X hold karte hue Y ka wait nahi karte, toh condition 2 destroy ho jaati hai.
Yeh step (all-or-nothing) kyun? Kyunki agar aapke paas sab kuch pehle se hai, toh aap hold karte hue kabhi wait nahi karte — wait karne ke liye kuch bacha hi nahi.
Cost: low resource utilization (aap resources pehle hi grab kar lete ho aur unpe baithe rehte ho) aur starvation (bahut saare popular resources ki zaroorat wala process forever wait kar sakta hai).
Worked example Technique: forcible release allow karo
Kaise: Agar process P koi resource request kare jo usse nahi mil raha, toh P ke saare current resources preempt kar lo (ya kisi waiting process se preempt karo). P tabhi restart hota hai jab woh sab kuch wapas paa sake.
Kyun kaam karta hai: resources ab liye jaa sakte hain , isliye koi process indefinitely doosron ko block nahi kar sakta.
Kahan use hota hai: jo states save/restore karna aasaan ho — CPU registers, memory pages (swap them out). Printer ya half-edited file protect karne wale mutex ko easily preempt nahi kar sakte.
Common mistake Steel-man: "Prevention = Avoidance hi hai na?"
Yeh galat idea sahi kyun lagta hai: dono deadlocks ko hone se pehle rokते hain, dono proactive lagte hain.
Fix yeh hai: ye alag hain:
Prevention structurally ek Coffman condition ko impossible banata hai (static rule, koi run-time state nahi chahiye).
Avoidance (Banker's algorithm) charon conditions ko allow karta hai lekin har request ko run-time pe check karta hai safe state mein rehne ke liye. Ise maximum future claims jaanna zaroori hai.
Mnemonic: Prevention = "physics ka ek rule todo"; Avoidance = "har step dhyaan se drive karo".
Common mistake Steel-man: "Sab resources pehle maangne ka matlab koi starvation nahi."
Kyun sahi lagta hai: sab ek saath milte hain, fair lagta hai.
Fix yeh hai: Kai popular resources ki zaroorat wala process repeatedly deny ho sakta hai kyunki koi na koi piece hamesha taken rehta hai → starvation . Saath hi utilization bhi girta hai kyunki resources "reserved" rehte hue idle baithe hain.
Common mistake Steel-man: "Circular wait todne ke liye resources ko request
time se order karo."
Kyun sahi lagta hai: ordering ka matlab ordering hi hai.
Fix yeh hai: Ordering ek fixed global function on resource types f ( R ) honi chahiye, pehle se decide ki gayi — na ki request ka dynamic time. Dynamic time orderings mein abhi bhi cycles ho sakti hain.
Recall Feynman: ek 12-saal ke bachche ko samjhao
Socho 4 bacche crayons share kar rahe hain. Jam tabhi hoga jab sab kuch ek saath galat ho jaaye : har crayon sirf ek baccha use kar sakta ho (1), har baccha ek crayon pakde hue doosra maang ke rota ho (2), kisi ko crayons chheenne ki ijazat na ho (3), aur woh waiting ke ek circle mein phans gaye hon (4). Ensure karne ke liye ki woh KABHI jam nahi honge, teacher bas ek rule hataa deti hai — sabse aasaan: "crayons order mein lene hain: pehle red, phir blue, phir green." Ab circle ho hi nahi sakta, kyunki uska matlab hoga red, red se pehle aata hai — jo bakwaas hai. Ek simple rule, zero jams forever.
Coffman conditions → "Me Hungry, No Cookies"
M utual exclusion · H old and wait · N o preemption · C ircular wait.
Aur sabse sasta wala todne ke liye: "Number your locks, climb only upward."
Charon Coffman conditions batao.
Logically sirf ek todne se saare deadlocks kyun prevent ho jaate hain?
Prove karo ki resource-ordering rule circular wait ko destroy karta hai.
Kaun si condition generally todna impossible hoti hai, aur kyun?
Prevention aur avoidance mein kya difference hai?
Deadlock ke liye 4 Coffman conditions kya hain? Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait — charon ek saath hold karni chahiye.
EK Coffman condition todne se deadlock prevent kyun hota hai? Deadlock ke liye charon ka logical AND chahiye; agar koi bhi false hai, toh conjunction false hai, isliye deadlock impossible hai.
Mutual Exclusion kaise todein? Resources ko shareable banao (spooling, read-only/lock-free access). Limited hai kyunki kuch resources intrinsically non-shareable hote hain.
Hold and Wait kaise todein? Ya toh saare resources atomically pehle request karo, YA nayi request karne se pehle saare held resources release kar do.
Hold and Wait todne ki cost kya hai? Low resource utilization (pehle reserve kiye resources idle baithe rehte hain) aur possible starvation.
No Preemption kaise todein? Kisi process se resources forcibly le lo (preempt karo) jab usse kuch nahi mil raha; CPU/memory ke liye aasaan, printers/mutexes ke liye mushkil.
Circular Wait todne wala resource-ordering rule batao. Har resource type ko ek unique number f(R) do; ek process R_new tabhi request kar sakti hai jab f(R_new) > f(R) ho har us R ke liye jo woh currently hold karti hai.
Resource ordering circular wait ko kyun khatam karta hai, prove karo. Ek cycle se milega f(R0)<f(R1)<...<f(R0), yaani f(R0)<f(R0), jo ek contradiction hai; isliye koi cycle exist nahi kar sakti.
Kaun si Coffman condition todna usually impossible hota hai, aur kyun? Mutual Exclusion — kuch resources (printer mid-job, write-lock) inherently non-shareable hote hain.
Prevention vs Avoidance? Prevention structurally ek condition ko impossible banata hai (static, koi future info nahi chahiye). Avoidance sab conditions allow karta hai lekin har request ko run-time pe check karta hai safe state mein rehne ke liye (Banker's algorithm, max claims chahiye).
Practical 80/20 prevention technique kaun si hai aur kyun? Circular Wait todna, resources ki total ordering se — sasta, static, koi future prediction nahi; real kernels mein use hota hai.
Low utilization / starvation
Contrapositive breaks the AND