Operating System Slides 📂 Introduction · 13 of 22 37 min read

Deadlock Prevention & Avoidance — Banker's Algorithm

Master deadlock prevention and avoidance: break the four Coffman conditions by design, or grant only safe requests at runtime. Includes the Banker's Algorithm — safety and resource-request procedures — with fully worked Allocation/Max/Need matrices and safe sequences, plus animated diagrams.

🏦

Deadlock Prevention & Avoidance — Banker's Algorithm

Two ways to keep deadlock from ever happening: break a Coffman condition by design (prevention), or grant only requests that keep the system in a safe state (avoidance). With fully worked Banker's Algorithm numericals.
Prevention Avoidance Banker's Algorithm Safe Sequence

Press Next → or use ← → arrow keys

Section 01

The Story That Explains Both

A bank keeps ₹10 lakh in reserve. Four customers hold credit lines totalling ₹24 lakh (₹7L, ₹5L, ₹3L, ₹9L) — far more than the cash on hand. A customer who already owes ₹2L asks for ₹3L more.

Before handing it over, the manager asks: "If I grant this, can I still eventually repay every customer in full, in some order?" If yes, grant it. If no, make them wait — even though the cash is physically sitting in the vault. That caution is exactly the Banker's Algorithm.
⚖️
Prevention vs Avoidance

Prevention is pessimistic — it forbids patterns that could deadlock, even when they wouldn't. Avoidance is optimistic — it grants a request whenever the result is provably safe, postponing only the truly risky ones.

Sections 03–06

Prevention — Break One Coffman Condition

Break…HowProblem
🔒 Mutual ExclusionMake resources sharable (read-only files, spooling)Impossible for intrinsically non-sharable things like locks
✋ Hold and WaitProtocol A: request everything up front · Protocol B: release all before requesting moreLow utilisation; possible starvation
🚫 No PreemptionLet the OS forcibly take resources backOnly works for state-saveable resources (CPU, memory)
🔄 Circular WaitNumber resource types; always request in increasing orderMost practical — used in real kernels
🔢
Ordering Wins

Give every resource type a number and require processes to lock in strictly increasing order. A cycle needs someone to wait "backwards" — which the ordering forbids. Cheap, simple, and the technique real kernels and databases actually use.

Section 06 · Diagram

Ordered Locking — No Cycle, No Deadlock

R1 · order = 1lock this first R2 · order = 2lock this second P1 P2 ✅ both lock R1→R2same order ⇒ no cycle possible
➡️
Everyone Goes the Same Way

Both P1 and P2 grab R1 before R2. Since no process ever holds a higher-numbered resource while waiting for a lower-numbered one, the wait-for edges can never form a loop. Circular wait is structurally impossible.

Section 07

Avoidance — Stay in the Safe State

State Space UNSAFE DEADLOCK SAFE
🧭
Safe State & Safe Sequence

A state is safe if there is an order ⟨P₁, P₂, …⟩ — a safe sequence — in which each process's remaining Need can be met by the current Available plus the resources held by processes earlier in the sequence: Needᵢ ≤ Available + Σ Allocationⱼ (j < i). Avoidance simply refuses any request that would leave the safe zone.

Section 08

Banker's Algorithm — The Data Structures

💰
Available
length m
How many instances of each resource type are currently free.
📈
Max
n × m
The most each process could ever need of each resource — declared up front.
📦
Allocation
n × m
How much each process currently holds.
Need = Max − Allocation

The fourth matrix, Need, is derived: what a process might still request. The algorithm has two parts — the Safety Algorithm (is the current state safe?) and the Resource-Request Algorithm (can I grant this specific request and stay safe?). Cost is O(m·n²) per request.

Sections 09–10

The Two Algorithms

# SAFETY ALGORITHM — is the state safe?
Work = Available;  Finish[i] = false for all i
while ∃ i : Finish[i]==false and Need[i] <= Work:
    Work = Work + Allocation[i];  Finish[i] = true
return "SAFE" if all Finish else "UNSAFE"
# RESOURCE-REQUEST ALGORITHM — can I grant Request from Pi?
if Request > Need[i]:   error   # exceeds declared Max
if Request > Available: return "WAIT"
# pretend-grant, then test
Available -= Request; Allocation[i] += Request; Need[i] -= Request
if is_safe(): return "GRANTED"
else: rollback(); return "DENIED — unsafe"
🎭
Pretend, Test, Commit or Roll Back

The request algorithm tentatively grants the request, runs the safety check, and only keeps the change if the result is safe — otherwise it rolls back and makes the process wait. It never risks an unsafe state.

Numerical · Galvin

Classic 5-Process Example — The Matrices

5 processes (P0–P4), 3 resources A/B/C. Total A=10, B=5, C=7; allocated A=7, B=2, C=5 → Available = [3, 3, 2].

Allocation
ABC
P0010
P1200
P2302
P3211
P4002
Max
ABC
P0753
P1322
P2902
P3222
P4433
Need = Max − Alloc
ABC
P0743
P1122
P2600
P3011
P4431
🔎
Now Run the Safety Check

Start with Work = Available = [3,3,2] and find any process whose Need ≤ Work. Grant it hypothetically, add its allocation back to Work, and repeat. The next slide traces it.

Numerical · Galvin

The Safety Trace — Building a Safe Sequence

Work [3,3,2] P1 P3 P4 P0 P2 [6,4,3] [8,5,4] [8,5,6] [8,6,6] [10,5,7] ✅ SAFE · ⟨P1, P3, P4, P0, P2⟩
PickNeed ≤ Work?Work beforeWork after (+Alloc)
P1[1,2,2] ≤ [3,3,2] ✓[3,3,2][6,4,3]
P3[0,1,1] ≤ [6,4,3] ✓[6,4,3][8,5,4]
P4[4,3,1] ≤ [8,5,4] ✓[8,5,4][8,5,6]
P0[7,4,3] ≤ [8,5,6] ✓[8,5,6][8,6,6]
P2[6,0,0] ≤ [8,6,6] ✓[8,6,6][10,5,7] — all free
Numerical 1

Worked Example — 4 Processes, Compute Available

4 processes (P0–P3), 3 resources A/B/C. Total A=8, B=5, C=6. Allocated = [7, 2, 4] → Available = [1, 3, 2].

Allocation
ABC
P0102
P1311
P2210
P3101
Max
ABC
P0433
P1423
P2522
P3533
Need = Max − Alloc
ABC
P0331
P1112
P2312
P3432
Safe Sequence ⟨P1, P0, P2, P3⟩

P0 fails first ([3,3,1] > [1,3,2] on A). But P1 fits → Work becomes [4,4,3]; now P0 fits → [5,4,5]; then P2 → [7,5,5]; then P3 → [8,5,6]. All finish, so the state is SAFE. Lesson: if the first process fails, try the next — don't stop.

Numerical 2

Practice — 4 Processes, Resources X/Y/Z

4 processes (P0–P3), 3 resources X/Y/Z. Total X=9, Y=3, Z=6. Available = [1, 1, 2].

Allocation
XYZ
P0211
P1101
P2412
P3100
Need
XYZ
P0302
P1111
P2311
P3111
Your turn — trace it
1
Work = [1,1,2]. Which Need fits?
2
Add that process's Allocation to Work.
3
Repeat until all finish — or none can.
✍️
Work It Through

Starting from [1,1,2], P1 (need [1,1,1]) and P3 (need [1,1,1]) both fit — pick one, add its allocation, and keep going. If you can order all four so every Need is met in turn, the state is safe. Try it before checking your answer against the safety algorithm.

Sections 13–14

Prevention vs Avoidance

PreventionAvoidance (Banker's)
PhilosophyPessimistic — forbid risky patternsOptimistic — allow if provably safe
When decidedDesign time (structural)Runtime (per request)
Needs max claims?NoYes — Max must be known in advance
Resource utilisationLower (over-restrictive)Higher
OverheadLowO(m·n²) safety check per request
Best forSimple, safety-critical systemsKnown, bounded workloads
Avoidance Pros
No deadlock, higher concurrency than prevention, no rollback needed.
Avoidance Cons
Needs advance Max claims, fixed process/resource counts, and a safety check on every request.
🌍
In Practice
Real kernels favour cheap ordering; Banker's is rare live but vital for understanding safety.
Section 16

Seven Rules for Prevention & Avoidance

🏦 PREVENTION & AVOIDANCE · CHECKLIST
1
Prevention breaks a Coffman condition structurally; avoidance grants only safe requests at runtime.
2
Resource ordering (acquire in increasing order) is the cheapest, most practical prevention.
3
A state is safe if a safe sequence exists — remember unsafe ≠ deadlock.
4
Always compute Need = Max − Allocation first, and Available = Total − ΣAllocation.
5
In the safety algorithm, if the first process fails, try the next — only report UNSAFE if none fit.
6
A resource request is granted only if it keeps the state safe — otherwise pretend-grant, test, roll back.
7
Banker's needs Max known in advance and fixed counts — great in theory, restrictive in practice.
FINAL

Keeping Deadlock From Ever Happening

4Conditions to break
Need= Max − Alloc
⟨P1,P3,P4,P0,P2⟩Galvin safe sequence
O(m·n²)Per request
SAFEThe only state to grant into
🎯
You Can Now Run the Banker's Algorithm

From breaking Coffman conditions to computing Need, running the safety algorithm, finding a safe sequence, and deciding whether to grant a resource request — you can solve any prevention or avoidance problem, by hand or in code.

📚
Where To Go Next

You've completed the deadlock arc. Next in most courses come memory management — contiguous allocation, paging and segmentation — followed by virtual memory and page-replacement algorithms.

🏦 End of tutorial · Press to review, or click Restart