Deadlock — four necessary conditions (Coffman)
4.2.19· Coding › Operating Systems
KIYA hai deadlock?
Key word hai circular waiting on each other. Agar unme se ek bhi process ko set ke bahar se eventually apna resource mil sakta, toh woh true deadlock nahi hai — woh sirf ek delay hai.
KYUN chahiye chaar conditions?
1971 mein Edward Coffman ne pucha: kin circumstances mein deadlock possible bhi hota hai? Unhone four necessary conditions dhundhe. "Necessary" ka matlab hai:
Toh agar koi ek bhi condition false ho, deadlock ho hi nahi sakta. Yahi WHY hai har deadlock-prevention strategy ke peeche: ek condition todo aur deadlock impossible ho jaata hai.
Chaar Coffman Conditions
HOW har condition contribute karti hai (gridlock scratch se derive karo)
Deadlock ko step by step build karo aur dekho har condition kahan use hoti hai:
- Process resource grab karta hai. Yeh possible kyun hai? Kyunki non-sharable hai — Mutual Exclusion ek owner ko baaki sab ko lock out karne deta hai.
- ab chahta hai lekin hold kiye rakhta hai. Woh kyun rakhta hai? Hold and Wait — woh jo hold kar raha hai use release nahi karta jabki aur maangta hai.
- Meanwhile hold kiye baitha hai aur chahta hai. Koi doosre ki resource grab nahi kar sakta. OS se kyun nahi cheen sakta? No Preemption — resources sirf voluntarily release hote hain.
- Ab : wait ka ek closed loop. Yeh fatal kyun hai? Circular Wait — dependency graph mein ek cycle hai, isliye "main unke khatam hone ka wait karunga" wali reasoning apni hi tail ko chase karti rehti hai forever.
Koi ek bhi link hatao aur chain toot jaati hai:
- No mutual exclusion → dono share karein → koi wait nahi.
- No hold-and-wait → ko maangne se pehle drop karna hoga → hold-while-waiting nahi.
- Preemption allowed → OS se le leta hai, ko de deta hai → loop khatam.
- No circular wait (e.g. resources order karo, increasing order mein request karo) → koi cycle ban hi nahi sakti.

Worked Examples
Recall Feynman: 12-saal ke bachche ko samjhao
Socho ek square hallway mein 4 bacche hain. Har bachcha apne saamne wale darwaze ka knob pakad leta hai aur chodega nahi jab tak woh agla darwaza bhi nahi khol sakta — lekin us agle darwaze ka knob already aage wale bachche ne pakad rakha hai. Ab sab ek knob pakde baitha hai, aage wale ka wait kar raha hai, ek circle mein. Koi nahi chodega (no preemption), koi knob share nahi kar sakta (mutual exclusion), aur sab ek ring mein wait kar rahe hain (circular wait) jabki pakde bhi hain (hold-and-wait). Unke phansne ke liye charon silly rules ek saath true honi chahiye. Sirf ek rule badlo — jaise "agar dono darwaze ek saath nahi khul sakte toh chod do" — aur jam clear ho jaata hai.
Forecast-then-Verify checkpoint
Aage padhne se pehle predict karo: Agar hum saare resources preemptible bana dein, tab bhi kya deadlock ho sakta hai? → Verify karo: Nahi. No Preemption hatane se condition 3 false ho jaati hai, isliye necessary set incomplete hai ⇒ deadlock impossible (haalaanki livelock aa sakta hai).
Flashcards
"Deadlock ke liye necessary condition" ka formal matlab kya hota hai?
Chaar Coffman conditions list karo.
"Charon conditions present hain" deadlock guarantee kyun nahi karta?
Global lock ordering enforce karna kaun si condition todata hai?
"Saare resources ek saath request karo" kaun si condition todata hai, aur iska downside kya hai?
Circular wait precisely define karo.
Deadlock context mein mutual exclusion kya hota hai?
Deadlock aur livelock mein kya farq hai?
Dining philosophers problem mein ek philosopher ko right-fork-first uthwana kaise help karta hai?
Agar resources OS dwara preemptible hon, toh kaun si Coffman condition fail hoti hai?
Connections
- Resource-Allocation Graph — cycle detection circular wait ko visible banata hai.
- Deadlock Prevention — har strategy ek Coffman condition negate karti hai.
- Deadlock Avoidance — Banker's Algorithm — conditions forbid karne ki jagah system ko safe states mein rakhta hai.
- Deadlock Detection and Recovery — deadlock hone do, phir usse todo (aksar preemption ke zariye).
- Mutual Exclusion aur Semaphores and Mutexes — jahan condition 1 aati hai.
- Dining Philosophers Problem — canonical illustration.
- Starvation and Livelock — failure modes jin mein tum deadlock prevent karte waqt trade ho jaate ho.