Skip to content

L-4: Deadlock

A deadlock in operating systems is a situation where :

  • a set of processes or threads are blocked because each one is waiting for a resource that another process is holding. None of the processes can proceed because they are all waiting for a condition that can never be true, leading to a “deadlock.”

Deadlock Example

R1
[.]
Allocated ⬋ ⬉ Request
(P1) (p2)
Request ⬊ ⬈ Allocated
[.]
R2
┌-------------------┐
(P1) P2 P3 | R1 -> P1
↓ ⬋ ↑ ⬋ ↑ | R1 is allocated to P1
R1 R2 R3 ←--┘ P2 requests for R1

Key Conditions for Deadlock (Coffman’s Conditions) ⭐

  1. Mutual Exclusion: Only one process can use the resource at any given time.
  2. Circular Hold & Wait
    • Hold and Wait: A process is holding at least one resource and is waiting for at least one resource resources.
    • Circular Wait: A set of processes are waiting in a circular chain
  3. No Preemption: Resources cannot be forcibly taken away from a process.

Resource Allocation Graph (RAG) in Deadlock.

Vertex
|
┌------+------┐
Process Resource
Vertex Vertex
(Pi) |
┌------+------┐
Single Multi
Instance Instance
[.] [...]
ex:CPU,Monitor ex: Registers
Edge
|
┌------+------┐
Assign Request
Edge Edge
(p) (p)
↑ ↓
[R] [R]

Waiting:

  1. Finite -> Starvation
  2. Infinite -> Deadlock
ScenarioDeadlock Status
Single Instance + No Circular WaitNo Deadlock
Single Instance + Circular WaitDeadlock
Multiple Instances + Circular WaitMay or May Not Deadlock

Examples 1

R1
[.]
Assign ⬋ ⬉ Requesting
(P1) (p2) (Circular Wait)
Requesting ⬊ ⬈ Assign
[.]
R2
(single Instance)
| Allocate | Request
----|----------|--------
| R1 R2 | R1 R2 Availability (0, 0) -> deadlock Method ✅
P1 | 1 0 | 0 1 R1 R2
P2 | 0 1 | 1 0

Methods for Handling Deadlocks

  1. Deadlock Prevention
    • Prevent the system from entering a deadlock state by ensuring that at least one of the necessary conditions for deadlock== (mutual exclusion, hold and wait, no preemption, and circular wait) ==never occurs.

  2. Deadlock Avoidance (Banker’s Algorithm)
    • Dynamically check resource allocation to ensure the system will never enter an unsafe state (i.e., a state that could lead to deadlock).

  3. Deadlock Detection & Recovery
    • Detection : Allow the system to enter a deadlock state but detect it through algorithms and then recover. Construct a wait-for graph, If a cycle exists, a deadlock has occurred.

    • Recovery: Once a deadlock is detected, recovery strategies include:
      1. Process Termination: Abort one or more processes to break the deadlock.

      2. Resource Preemption: Temporarily take resources away from some processes and allocate them to others to resolve the deadlock.

  4. Deadlock Ignorance (Ostrich Method)
    • The system deliberately ignores the possibility of deadlock, assumes that deadlock is rare and the cost of prevention, detection, or recovery outweighs the cost of occasionally restarting the system when a deadlock occur.

Difference between Deadlock Prevention & Deadlock Avoidance

Here is the table showing the clear difference: Here is the table showing the clear difference:

Here is the table with examples:

AspectDeadlock PreventionDeadlock Avoidance (Banker’s Algorithm)
StrategyEliminates one or more necessary conditions for deadlockDynamically checks if granting resource keeps system in safe state
ExamplePrevent Hold and Wait: Process must request all resources (printer, scanner) at onceSystem with 3 resources, 2 processes (Max=2, Alloc=1). Checks if granting keeps system safe before allowing
Decision TimeBefore execution (design-time)At runtime
System BehaviorConservative, may lead to low resource utilizationMore efficient, but requires complex checks
Unsafe StateNever enteredMay be close, but avoided through safe-state checking
Example:A process must acquire both printer and scanner together or wait without holding any.

If a process requests a resource, System checks:
- If granting keeps the system in a safe state (i.e., enough resources remain for other processes to complete) → Request granted
- If granting leads to an unsafe state (i.e., no process can finish) → Request denied (deadlock avoided)

Deadlock Detection Algorithm

  • Construct a wait-for graph, where nodes represent processes and directed edges represent dependencies between processes.
  • Detect cycles in this graph. If a cycle exists, a deadlock has occurred.

Real-World Example

  • Consider a system where two programs are trying to access two shared resources (like a printer and a scanner). If one program locks the printer and waits for the scanner while the other locks the scanner and waits for the printer, both will wait indefinitely—this is a deadlock situation.





  • Deadlock Avoidance
  • Also used for Deadlock Detection

Example:

There may be deadlock In future or not ?? Given the no. of resources allocated and required to each process.

3 Type of Resources = A, B, C (let CPU, Memory and Printer) 5 Processed = P1, P2, P3, P4, P5

Total Units of ResourcesA=10B=5C=7
ProcessResources Allocated to the ProcessResource Required/Need of process
A B CA B C
P10 1 07 5 3
P22 0 03 2 2
P33 0 29 0 2
P42 1 14 2 2
P50 0 25 3 3

Solution : We have To find whether

  • Safe (No Deadlock will Occur)
  • or Unsafe(Deadlock will occur) ??

Step 1: Calculate Remaining Need for Each Process by = (Required Need - Already Allocated)

ProcessAllocationMax NeedRemaining Need = Need - Allocated
A B CA B CA B C
P10 1 07 5 37 4 3
P22 0 03 2 21 2 2
P33 0 29 0 26 0 0
P42 1 14 2 22 1 1
P50 0 25 3 35 3 1
Total7 2 5
Step 2: Find Current Available Of Resources = (Total Resources in System - Current Available Resources)
ProcessAllocationMax NeedRemaining Need = Need - AllocatedCurrent Available = Total (10 5 7) - Total Allocated (7 2 5)
A B CA B CA B CA B C
P10 1 07 5 37 4 33 3 2
P22 0 03 2 21 2 2
P33 0 29 0 26 0 0
P42 1 14 2 22 1 1
P50 0 25 3 35 3 1
Total7 2 5
Step 3:
  1. From P1 to P5 , Find Process that’s ( Remaining Need for each resources type <= Current Availability of each Resource type).
  • If none of such found, than deadlock will occur.
  • P2 Requirement Could be full-filled P2✅ : (1 2 2) < (3 3 2)
  1. Terminate The P2 Resource, And free its allocated Resources Current Available Resource = P2 Allocated(2 0 0) + Current = (5 3 2)

Similarly Repeat the process. P2 -> P4 -> P5 -> P1 -> P3 Final Total Resource = Initial Total Available Resources = ( 10 5 7) Safe ✅

ProcessAllocationMax NeedRemaining Need = Need - AllocatedCurrent Available = Total (10 5 7) - Total Allocated (7 2 5)
A B CA B CA B CA B C
P10 1 07 5 37 4 33 3 2
P22 0 03 2 21 2 25 3 2 (+ P2 Free up)
P33 0 29 0 26 0 07 4 3 (+ P4 Free up)
P42 1 14 2 22 1 17 4 5 (+ P5 Free up)
P50 0 25 3 35 3 17 5 5 (+ P1 Free up)
Total7 2 510 5 7 (+ P3 Free up)
There could be multiple sequence
P2 -> P4 -> P3 -> P5 -> P1
P2 -> P4 -> P1 -> P5 -> P3
P2 -> P5 -> P3 -> P1 -> P4 etc.

Note: Banker’s Algorithm is not possible In real life, because Process Resource Requirement are Dynamic and at Run time. And this approach require predefined knowledge of Total, Allocated and Required Resources.


Ques: Find If it is Safe or Not??

ProcessAllocationMax NeedAvailable
E F GE F GE F G
P11 0 14 3 13 3 0
P21 1 22 1 4
P31 0 31 3 3
P42 0 05 4 1
Total7 2 5

Solution

Total Resource = Available + Allocated = (8 4 6)

ProcessAllocationMax NeedRemainingAvailable
E F GE F GE F GE F G
P11 0 14 3 13 3 03 3 0
P21 1 22 1 41 0 24 3 1 ( + P1 Free up)
P31 0 31 3 30 3 05 3 4 (+ P3 Free up)
P42 0 05 4 13 4 16 4 6 (+ P2 Free up)
Total5 1 68 4 6 (+ P4 Free up)

[L-4.7: Question Explaination on Deadlock | Operating System](deadlock avoidance in operating system)

Section titled “[L-4.7: Question Explaination on Deadlock | Operating System](deadlock avoidance in operating system)”

Ques. A System is having 3 process each require 2 units of resources R The Minimum no. of Units of R such that no deadlock will occur ?

Section titled “Ques. A System is having 3 process each require 2 units of resources R The Minimum no. of Units of R such that no deadlock will occur ?”

a) 3 b) 5 c) 6 d) 4

Solution

R = 6 ?

R = 3 Process x Each Require 2 Resouces.
P1 P2 P3
2 2 2
But Note, We have to find Minimum ❌

If R = 2 ?

P1 P2 P3
1 1 ❌ Deadlock
P1 P2 P3
1
1
P2 P3 (+ P1 Free)
1
1
P3 (+P2 Free)
1
1
All Free :)
But this not meen It is deadlock Free ❌
Because, After P1 Free, Its not Necessary that Resources both unit will be allocated to P2, And may stuck in Deadlock

R = 3?

P1 P2 P3
1 1 1 Deadlock ❌
P1 P2 P3
1 1
1
P2 P3 (+P1 free) P2 P3
1 1 or 1 1
1 1
P3 (+P2/P3 free) P2
1 or 1
1 1
All Free : )
But its not necessary 2 Resource Allocated to P1 in Starting, it is one of the suitable case. but we have to find minimum that satisfy all cases ❌

R = 4?

P1 P2 P3
1 1 1
1
P2 P3 (+P1 Free)
1 1
1 1
Deadlock will Never Occur✅

Answer: Minimum no. of Resources so no deadlock = 4 Max no. of Resources with Deadlock = Min Resources with No deadlock - 1 = 4-1 = 3

Ques: What is the Minimum Requirement of Resources To Prevent Deadlock. where each Processes P1, P2 and P3 with required 3 , 4 and 5 unit respectively

P1 P2 P3
3 4 5
2 3 4 (Max no. of Resources with Deadlock = ∑(Each Required - 1)
+1 (Min no. of Resources with No Deadlock) = ∑(Each Required - 1) +1
Minimum no. of Resources so no deadlock = 2 + 3 + 4 (+1) = 10
Max no. of Resources with Deadlock = 2+3 + 4 (+1) = 9

Formula ⭐

  • if each process P Required same amount of Resources R
  1. Maximum Resource Required (for Deadlock) = P * (R-1)
  2. Minimum Resource Required (No Deadlock ) = P * (R-1) + 1