4.2.20 · HinglishOperating Systems

Deadlock prevention — break each condition

1,805 words8 min readRead in English

4.2.20 · Coding › Operating Systems

KYA break kar rahe hain hum?

Deadlock Prevention = system ko aisa design karo ki IN MEIN SE EK BHI kabhi true na ho sake.

Figure — Deadlock prevention — break each condition

KAISE todein har condition ko

1. Mutual Exclusion todo

2. Hold and Wait todo

3. No Preemption todo

4. Circular Wait todo ⭐ (the 80/20 winner)



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.


Active Recall

Flashcards

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.

Connections

Concept Map

requires AND of all

requires AND of all

requires AND of all

requires AND of all

breaks at least one

breaks at least one

breaks at least one

breaks at least one

broken by

broken by

broken by

broken by

cost

justified by

Deadlock

Mutual Exclusion

Hold and Wait

No Preemption

Circular Wait

Deadlock Prevention

Spooling / shareable

All-or-nothing request

Forcible preemption

Resource ordering

Low utilization / starvation

Contrapositive breaks the AND