Deadlock avoidance — Banker's algorithm
WHY does this algorithm exist?
WHY "safe" matters: A state is safe if there exists at least one ordering of processes (a safe sequence) such that each process can get its remaining needs, run to completion, and release everything. Unsafe ≠ deadlocked — it just means deadlock might become unavoidable. The banker refuses to ever enter an unsafe state, so deadlock can never occur.
WHAT are the data structures?
For processes and resource types:
HOW: the Safety Algorithm (derived from scratch)
We want to answer: "Given the current allocation, can everyone finish?"
Step 0 — set up bookkeeping.
- (the free pool we can hand out as we simulate).
- for all (nobody has finished yet).
WHY starts at : before anyone finishes, the only resources we can lend are the ones currently free.
Step 1 — find a process that can finish right now. Look for with and (componentwise).
WHY: if a process's remaining need fits in the free pool, we can imagine giving it everything, letting it run, and getting it all back.
Step 2 — pretend it finishes and returns its resources.
WHY add , not : when it finishes it releases everything it held, which is . Its was already covered from in step 1.
Step 3 — repeat until no such process exists.
Step 4 — decide. If for all → state is safe; the order we picked is a safe sequence. Otherwise unsafe.
HOW: the Resource-Request Algorithm
Process makes request .
WHY pretend-then-check: the only way to know a grant is safe is to look at the resulting state and verify a safe sequence still exists.

Worked Example 1 — Is the state safe?
3 resource types with total .
| Process | Allocation (A B C) | Max (A B C) |
|---|---|---|
| P0 | 0 1 0 | 7 5 3 |
| P1 | 2 0 0 | 3 2 2 |
| P2 | 3 0 2 | 9 0 2 |
| P3 | 2 1 1 | 2 2 2 |
| P4 | 0 0 2 | 4 3 3 |
Available . Why this step? Free = total minus everything held.
Need = Max − Allocation: P0 (7 4 3), P1 (1 2 2), P2 (6 0 0), P3 (0 1 1), P4 (4 3 1).
Now run Safety with :
- ✓ → finish P1, . Why add Alloc P1? It releases its held resources.
- ✓ → .
- ✓ → .
- ✓ → .
- ✓ → . All finished.
Safe sequence: . State is SAFE. ✅
Worked Example 2 — Should we grant a request?
From Example 1's state, requests .
- ? ✓
- ? ✓
- Pretend-grant:
Run Safety with :
- P1 ✓ →
- P3 ✓ →
- P0 ✓ → ... P2, P4 follow.
Safe sequence exists → GRANT. ✅ Why we still verify after pretending? Because availability dropped; we must confirm everyone can still finish.
Worked Example 3 — A request that is refused
From Example 1, suppose requests .
- ? ✓
- ? ✓ → pretend: .
Run Safety with : scan needs P0(7 4 3)✗, P1(1 2 2)✗, P2(6 0 0)✗, P3(0 1 1)✗ (B short), P4(1 0 1)✗. No process can proceed → UNSAFE → rollback, P4 waits. ❌ Why refuse a request that "fits"? Fitting in isn't enough — the resulting state has no safe sequence.
Common mistakes
Recall Feynman: explain to a 12-year-old
Imagine a toy-lending library. Each kid tells the librarian the most toys they'll ever need at once. Before lending more toys, the librarian thinks: "If I give these now, can I still make sure some kid gets all their toys, plays, and gives them back, then the next kid, and so on, until everyone's happy?" If yes, she lends. If even one arrangement is impossible, she says "wait." Because she always keeps an escape plan, no kid ever gets stuck forever.
Flashcards
What is a safe state?
Formula for Need?
In the Safety Algorithm, what is Work initialized to?
When a process "finishes" in simulation, what is added to Work — Max or Allocation?
Does unsafe imply deadlock?
The three checks for a resource request?
What extra knowledge does Banker's algorithm require vs. detection?
How do you compute Available from the table?
When P4 requests (3,3,0) and state becomes unsafe, what happens?
Connections
- Deadlock — Coffman conditions
- Deadlock detection and recovery
- Deadlock prevention
- Resource Allocation Graph
- Process scheduling
- Semaphores and mutual exclusion
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Socho OS ek bank manager hai. Har process (customer) pehle hi bata deta hai ki use maximum kitne resources chahiye honge — yeh hai Max. Abhi uske paas jo hai woh Allocation, aur jo aur maang sakta hai woh Need = Max − Allocation. Bank ke paas jo free padaa hai woh Available. Banker's algorithm ka simple funda: koi bhi request tabhi grant karo jab grant karne ke baad bhi system safe rahe.
Safe ka matlab kya? Ek aisa order milna chahiye jisme har process apni baaki Need pe paa kar, kaam khatam kar ke, apne saare resources wapas de de. Hum simulate karte hain: Work = Available se start karo, koi aisa process dhoondo jiska Need <= Work ho, use complete maan lo aur uska Allocation (jo usne hold kiya tha) Work me wapas add kar do. Repeat karte raho — agar saare process finish ho gaye, toh state SAFE. Yaad rakho — finish hone par Allocation add hota hai, Max nahi.
Request aaye toh teen check: (1) Request <= Need? nahi toh error; (2) Request <= Available? nahi toh wait; (3) maan lo de diya, phir Safety chalao — agar safe rahe toh sach me de do, warna rollback karke wait karwao. Example 3 me P4 ka request "fit" toh ho raha tha Available me, lekin baad me koi process aage badh hi nahi pa raha tha — isliye UNSAFE, request refuse. Important baat: unsafe ka matlab deadlock ho gaya nahi hai, balki ho sakta hai — banker thoda extra careful hota hai taaki deadlock kabhi ho hi na.