Operating System (CS2701)
Deadlock Prevention, Avoidance & Recover
Dr. Rourab Paul
Computer Science Department, SNU University, Chennai
Operating System
Introduction
Operating System
2
A deadlock occurs when two or more processes are waiting indefinitely for resources held by each other.
Example:
Process P1 holds Resource R1, waits for R2
Process P2 holds Resource R2, waits for R1
→ Both are stuck forever!
Conditions for Deadlock
Operating System
3
Deadlock occurs only if all four hold simultaneously:
We emphasize that all four conditions must hold for a deadlock to occur.
Deadlock Prevention
Operating System
4
Strategy: Deny one of the four necessary conditions.
Condition | How to Prevent |
Mutual Exclusion | Make resources shareable if possible (Not Realistic) |
Hold and Wait | Require all resources requested at once (Not Realistic) |
No Preemption | Take away resource if new one can’t be allocated (Partially realistic — used only for certain resources (like CPU/memory)) |
Circular Wait | Impose resource ordering (F: R → N) |
The most realistic and widely used deadlock prevention method is breaking the Circular Wait condition — by imposing an ordering on resources and requiring threads to request resources in that fixed order
Example — Circular Wait Prevention
Operating System
5
We define a global order on resources:
F(first_mutex) = 1
F(second_mutex) = 5
Rule:� A thread can only lock resources in increasing order of F.
Allowed: first_mutex → second_mutex�Not Allowed: second_mutex → first_mutex
Prevents circular waiting, hence no deadlock.
Example — Circular Wait Prevention
Operating System
6
pthread_mutex_lock(&first_mutex);
pthread_mutex_lock(&second_mutex);
// critical section
pthread_mutex_unlock(&second_mutex);
pthread_mutex_unlock(&first_mutex);
All threads follow the same lock order → deadlock-free
Deadlock Avoidance
Operating System
7
Don’t prevent conditions — analyze requests dynamically to avoid unsafe states.
Concept:� Allow resource allocation only if it keeps the system in a safe state.
Uses Banker’s Algorithm (Dijkstra):
Banker’s Algorithm for Deadlock Avoidance
Operating System
8
Proposed by Edsger Dijkstra.�
Used to avoid deadlock by ensuring the system never enters an unsafe state.�
Called “Banker” because it’s like a bank lending money:��� The bank only lends if it can satisfy all customers eventually, ensuring nobody is left waiting indefinitely.
Banker’s Algorithm for Deadlock Avoidance
Operating System
9
Safe State: A state is safe if the system can allocate resources to all processes in some order and every process can complete.
Unsafe State: A state is unsafe if there exists at least one sequence where some process may never finish.
Unsafe ≠ deadlock, but unsafe can lead to deadlock.
Process Resource Info:
Max Need: Maximum resources a process may request.
Allocation: Resources currently held by the process.
Need: Remaining resources needed = Max Need − Allocation
Available: Free resources in the system.
Banker’s Algorithm Steps
Operating System
10
Banker’s Algorithm Example
Operating System
11
System:
Process | Max Need | Allocation |
P0 | 7 | 0 |
P1 | 5 | 1 |
P2 | 3 | 2 |
Banker’s Algorithm Example Step 2
Operating System
12
System:
Process | Max Need | Allocation | Need = Max - Allocation |
P0 | 7 | 0 | 7 |
P1 | 5 | 1 | 4 |
P2 | 3 | 2 | 1 |
Available = Total - sum(Allocation) = 10 - (0+1+2) = 7
Banker’s Algorithm Example Step 3.1
Operating System
13
Process | Max Need | Allocation | Need = Max - Allocation |
P0 | 7 | 0 | 7 |
P1 | 5 | 1 | 4 |
P2 | 3 | 2 | 1 |
Banker’s Algorithm Example Step 3.2
Operating System
14
Allocate resources to P2 (Need = 1)
P2 completes its execution
Release resources held by P2: Allocation = 2
Update Available: Available = old Available + Allocation of P2 = 7 + 2 = 9
P2 is marked finished.�
Process | Max Need | Allocation | Need = Max - Allocation |
P0 | 7 | 0 | 7 |
P1 | 5 | 1 | 4 |
P2 | 3 | 2 | 1 |
Banker’s Algorithm Example Step 3.3
Operating System
15
Now, Available = 9
Process | Max Need | Allocation | Need = Max - Allocation |
P0 | 7 | 0 | 7 |
P1 | 5 | 1 | 4 |
P2 | 3 | 2 | 1 |
Banker’s Algorithm Example Step 3.4
Operating System
16
Process | Max Need | Allocation | Need = Max - Allocation |
P0 | 7 | 0 | 7 |
P1 | 5 | 1 | 4 |
P2 | 3 | 2 | 1 |
Prevention VS Avoidance
Operating System
17
Aspect | Deadlock Prevention | Deadlock Avoidance |
Approach | Structural restriction | Dynamic check |
Overhead | Low | High |
Resource Utilization | Poor | Better |
Knowledge of Max Need | Not required | Required |
Example | Lock ordering | Banker’s Algorithm |
Recover
Operating System
18
To eliminate deadlocks by aborting a process or thread, we use one of two methods. In both methods, the system reclaims all resources allocated to the terminated processes.
Thank You
Operating System
19