Deadlock detection and recovery
1. What is a deadlock (recap)
The four Coffman conditions must all hold: mutual exclusion, hold-and-wait, no preemption, and circular wait. Detection assumes we let these happen and look for the consequence.
2. Detection — single instance per resource type
We build a Wait-For Graph (WFG): collapse the Resource-Allocation Graph by removing resource nodes.
- WHAT: node = process. Edge means is waiting for a resource currently held by .
- HOW to derive it: in the RAG, if (request) and (allocation), then in the WFG draw .

3. Detection — multiple instances per resource type
Data structures
- : free instances of each of resource types.
- : how much process currently holds.
- : what process is currently requesting (note: this is request, not the Banker's "max need").
WHY this works: if , process can get what it asks, run, and release everything it holds — adding back to the pool. We optimistically grant resources to whoever can run, snowballing the free pool. Any process we can never satisfy this way is genuinely stuck.
4. When to run detection?
5. Recovery
Once detected, break at least one Coffman condition. Three families:
Recall Feynman: explain to a 12-year-old
Imagine four kids each holding one half of two toys they want to swap, and each is waiting for the other to hand over their half first. Nobody lets go, so they freeze forever — that's a deadlock. The teacher (the OS) walks around with a checklist: "Can anyone finish with the toys lying free right now?" If yes, that kid plays, then drops their toys back into the box so others can grab them — and the teacher keeps going. If she reaches a point where no kid can ever play, those kids are stuck. To fix it she either makes one kid quit and give up their toys (termination) or snatches a toy away and hands it over (preemption) — choosing the kid who'd lose the least progress.
Flashcards
In a single-instance system, what graph condition is necessary AND sufficient for deadlock?
Why is a cycle NOT sufficient for deadlock with multiple resource instances?
What's the key difference between the deadlock detection algorithm and the Banker's algorithm?
In detection, why do processes with Allocation = 0 start as Finish = true?
How does the detection algorithm decide a process can finish?
After the detection loop ends, who is deadlocked?
Why allow deadlocks then detect, instead of preventing them?
Name the two main recovery strategies.
What three issues complicate recovery by preemption?
How do you prevent starvation when the same process is repeatedly chosen as victim?
What is the cost of cycle detection in a WFG with V processes?
Connections
- Resource Allocation Graph — the WFG is its process-only projection.
- Banker's Algorithm — same loop, but avoidance (Need) vs detection (Request).
- Coffman Conditions — recovery works by breaking one of these.
- Checkpointing and Rollback — the mechanism enabling preemption recovery.
- CPU Scheduling — victim selection reuses priority/aging ideas.
- Deadlock Prevention vs Avoidance — the alternative philosophies.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, deadlock detection ka idea simple hai: kuch OS jaan-boojh kar deadlock hone dete hain, kyunki har request pe prevention ya Banker's algorithm chalana mehenga padta hai. Agar deadlock kabhi-kabhi hi hota hai, toh hone do — baad mein detect karke clean kar lenge. Isme do kaam hain: pehle detect karo ki sach me deadlock hua hai ya nahi, phir recover karo (kisi process ko maaro ya resource cheen lo).
Single instance wale resources me detection bahut aasan hai — bas ek Wait-For Graph banao (Pi → Pj matlab Pi, Pj ke paas wala resource maang raha hai). Agar is graph me cycle ban gaya, deadlock pakka. Cycle = deadlock, sure shot. Lekin jab ek resource type ke multiple instances hain, tab sirf cycle dekhna kaafi nahi — kyunki koi doosra holder release karke waiting process ka kaam bana sakta hai. Tab hum simulate karte hain: se shuru karo, dekho kaun apna satisfy karta hai, use finish karwa do aur uska wapas pool me daal do. Jo process kabhi finish nahi ho sakta — wahi deadlocked hai.
Yahan ek bada confusion hota hai: log isko Banker's algorithm samajh lete hain. Farak yeh hai — Banker's me Need (Max − Allocation) use hota hai pehle se unsafe state rokne ke liye; detection me actual current Request use hota hai baad me deadlock dhoondhne ke liye. Recovery me ya toh process ko abort karo (sabko ya ek-ek karke), ya resource preempt karke checkpoint pe rollback karo. Bas ek baat dhyan rakho — har baar same process ko victim mat banao, warna woh kabhi finish hi nahi karega (starvation). Isliye rollback count ko cost me jod do.