4.2.21Operating Systems

Deadlock avoidance — Banker's algorithm

1,848 words8 min readdifficulty · medium2 backlinks

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 nn processes and mm 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.

  • Work=AvailableWork = Available (the free pool we can hand out as we simulate).
  • Finish[i]=falseFinish[i] = false for all ii (nobody has finished yet).

WHY WorkWork starts at AvailableAvailable: 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 ii with Finish[i]=falseFinish[i] = false and Need[i]WorkNeed[i] \le Work (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. Work=Work+Allocation[i],Finish[i]=trueWork = Work + Allocation[i], \qquad Finish[i] = true

WHY add Allocation[i]Allocation[i], not Max[i]Max[i]: when it finishes it releases everything it held, which is AllocationAllocation. Its NeedNeed was already covered from WorkWork in step 1.

Step 3 — repeat until no such process exists.

Step 4 — decide. If Finish[i]=trueFinish[i] = true for all ii → state is safe; the order we picked is a safe sequence. Otherwise unsafe.


HOW: the Resource-Request Algorithm

Process PiP_i makes request RequestiRequest_i.

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.

Figure — Deadlock avoidance — Banker's algorithm

Worked Example 1 — Is the state safe?

3 resource types A,B,CA, B, C with total =(10,5,7)= (10,5,7).

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 =TotalAllocation=(10,5,7)(7,2,5)=(3,3,2)= Total - \sum Allocation = (10,5,7) - (7,2,5) = (3,3,2). 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 Work=(3,3,2)Work=(3,3,2):

  • NeedP1=(1,2,2)(3,3,2)Need_{P1}=(1,2,2)\le(3,3,2) ✓ → finish P1, Work=(3,3,2)+(2,0,0)=(5,3,2)Work=(3,3,2)+(2,0,0)=(5,3,2). Why add Alloc P1? It releases its held resources.
  • NeedP3=(0,1,1)(5,3,2)Need_{P3}=(0,1,1)\le(5,3,2) ✓ → Work=(5,3,2)+(2,1,1)=(7,4,3)Work=(5,3,2)+(2,1,1)=(7,4,3).
  • NeedP0=(7,4,3)(7,4,3)Need_{P0}=(7,4,3)\le(7,4,3) ✓ → Work=(7,4,3)+(0,1,0)=(7,5,3)Work=(7,4,3)+(0,1,0)=(7,5,3).
  • NeedP2=(6,0,0)(7,5,3)Need_{P2}=(6,0,0)\le(7,5,3) ✓ → Work=(7,5,3)+(3,0,2)=(10,5,5)Work=(7,5,3)+(3,0,2)=(10,5,5).
  • NeedP4=(4,3,1)(10,5,5)Need_{P4}=(4,3,1)\le(10,5,5) ✓ → Work=(10,5,7)Work=(10,5,7). All finished.

Safe sequence: P1,P3,P0,P2,P4\langle P1, P3, P0, P2, P4\rangle. State is SAFE. ✅


Worked Example 2 — Should we grant a request?

From Example 1's state, P1P1 requests Request1=(1,0,2)Request_1=(1,0,2).

  1. (1,0,2)NeedP1(1,2,2)(1,0,2)\le Need_{P1}(1,2,2)? ✓
  2. (1,0,2)Available(3,3,2)(1,0,2)\le Available(3,3,2)? ✓
  3. Pretend-grant:
    • Available=(3,3,2)(1,0,2)=(2,3,0)Available = (3,3,2)-(1,0,2)=(2,3,0)
    • AllocP1=(2,0,0)+(1,0,2)=(3,0,2)Alloc_{P1}=(2,0,0)+(1,0,2)=(3,0,2)
    • NeedP1=(1,2,2)(1,0,2)=(0,2,0)Need_{P1}=(1,2,2)-(1,0,2)=(0,2,0)

Run Safety with Work=(2,3,0)Work=(2,3,0):

  • P1 Need(0,2,0)(2,3,0)Need(0,2,0)\le(2,3,0) ✓ → Work=(2,3,0)+(3,0,2)=(5,3,2)Work=(2,3,0)+(3,0,2)=(5,3,2)
  • P3 (0,1,1)(5,3,2)(0,1,1)\le(5,3,2) ✓ → Work=(7,4,3)Work=(7,4,3)
  • P0 (7,4,3)(7,4,3)(7,4,3)\le(7,4,3) ✓ → Work=(7,5,3)Work=(7,5,3) ... P2, P4 follow.

Safe sequence P1,P3,P0,P2,P4\langle P1,P3,P0,P2,P4\rangle 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 P4P4 requests (3,3,0)(3,3,0).

  • NeedP4(4,3,1)\le Need_{P4}(4,3,1)? ✓
  • Available(3,3,2)\le Available(3,3,2)? ✓ → pretend: Available=(0,0,2)Available=(0,0,2).

Run Safety with Work=(0,0,2)Work=(0,0,2): 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 proceedUNSAFErollback, P4 waits. ❌ Why refuse a request that "fits"? Fitting in AvailableAvailable 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?
A state where there exists at least one ordering (safe sequence) in which every process can obtain its remaining needs, finish, and release resources.
Formula for Need?
Need[i][j] = Max[i][j] − Allocation[i][j].
In the Safety Algorithm, what is Work initialized to?
Work = Available (the currently free resources).
When a process "finishes" in simulation, what is added to Work — Max or Allocation?
Allocation (what it actually held); its Need was already covered from Work.
Does unsafe imply deadlock?
No — unsafe means deadlock is possible, not that it has occurred.
The three checks for a resource request?
(1) Request ≤ Need, else error; (2) Request ≤ Available, else wait; (3) pretend-allocate then run Safety; grant only if still safe.
What extra knowledge does Banker's algorithm require vs. detection?
The maximum resource demand (Max) of every process declared in advance.
How do you compute Available from the table?
Available = Total resources − sum of all Allocation rows.
When P4 requests (3,3,0) and state becomes unsafe, what happens?
Rollback the pretend-allocation and make P4 wait.

Connections

Concept Map

one approach

classic algorithm

requires

grants request only if

defined by

tracks

invariant

tested by

starts

picks process with

then releases

all Finish true means

Deadlock strategies

Avoidance

Banker's algorithm

Max demand known in advance

Safe state

Safe sequence exists

Available Max Allocation Need

Need = Max - Allocation

Safety algorithm

Work = Available

Need <= Work

Work += Allocation

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.

Go deeper — visual, from zero

Test yourself — Operating Systems

Connections