Presentation 5233 Content Document 20250304084937AM
Presentation 5233 Content Document 20250304084937AM
SEIT2230
S1_Module_4
Deadlocks
Syllabus
What is Deadlock?
Deadlock
• A set of blocked processes each holding a resource and
waiting to acquire a resource held by another process in
the set is known as a Deadlock.
• A process is deadlocked if it is waiting for an event that will
never occur.
Process Process P
Hold Request
1
Request Hold
R1 DEADLOCK R2
P Hold
Request 2
Resource Resource
Example - Bridge Crossing
1. Mutual exclusion
– Each resource is either currently assigned to exactly one process or is
available.
2. Hold and wait
– Process currently holding resources granted earlier can request more
resources.
3. No preemption
– Previously granted resources cannot be forcibly taken away from
process.
4. Circular wait
– There must be a circular chain of 2 or more processes. Each process
is waiting for resource that is held by next member of the chain.
• All four of these conditions must be present for a deadlock to occur.
Resource Allocation Graph
• A visual way to determine if a deadlock has, or may occur.
• Process is a circle; Resource type is square; dots represent number of
instances of resource in type.
A 3 9 A 3 9 A 3 9
4
B 2 4 2 B 4 4 B 0 -
C 2 7 C 2 7 C 2 7
Free : 3 Free : 1 Free : 5
A 3 9 A 4 9 A 4 9
1
B 2 4 2 B 4 4
B 2 4
C 2 7 C 2 7 C 2 7
Free : 3 Free : 2 Free : 0
A 1 6 A 1 6 A 1 6
B 1 5 B 1 5 B 1 5
C 2 4 C 4 4 C 0 0
D 4 7 D 4 7 D 4 7
Free : 2 Free : 0 Free : 4
A 1 6 A 1 6 A 1 6
B 1 5 B 1 5 B 5 5
C 0 - C 0 - C 0 -
D 7 7 D 0 - D 0 -
Free : 1 Free : 8 Free : 4
Banker’s algorithm for
single resource
A 1 6 A 6 6 A 0 -
B 0 - B 0 - B 0 -
C 0 - C 0 - C 0 -
D 0 - D 0 - D 0 -
Free : 9 Free : 4 Free : 10
A 1 6
B 2 5
C 2 4
D 4 7
Free : 1
Tape
Process
total
Drive
P5
P4
P3
P2
P1
Plotters
resource
Tape
0
1
1
0
3
Drive
Scanners
Plotters 6 no 3of each4
0
1
1
1
0
CD Roms
2
Scanners
0
0
1
0
1
CD Roms
0
1
0
0
1
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
2
0
3
0
1
Drive
Plotters
1
0
1
1
1
Tape
1
multiple resource
Drive
Scanners
1
1
0
1
0
Plotters
0
resources
Banker’s algorithm for
CD Roms
0
0
0
2
0
Scanners
Available (free)
2
6
Process
Drive
P5
P4
P3
P2
P1
Plotters
resource
3
Tape
0
1
1
0
3
Drive
total no of each
Scanners
4
Plotters
0
1
1
1
0
CD Roms
2
Scanners
0
1
1
0
1
CD Roms
0
1
0
0
1
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
2
0
3
0
1
Drive
1 Plotters
0
1
1
1
Tape
1
multiple resource
Drive
Scanners
1
0
0
1
0
Plotters
0
Banker’s algorithm for
resources
CD Roms
0
0
0
2
0
Scanners
Available (free)
1
6
Process
Drive
P5
P4
P3
P2
P1
Plotters
resource
3
Tape
0
1
0
3
Drive
total no of each
Scanners
4
Plotters
0
1
1
0
CD Roms
2
Scanners
0
1
0
1
CD Roms
0
0
0
1
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
2
0
3
0
1
Drive
1 Plotters
0
1
1
1
Tape
2
multiple resource
Drive
Scanners
1
0
0
1
0
Plotters
1
Banker’s algorithm for
resources
CD Roms
0
0
0
2
0
Scanners
Available (free)
2
6
Process
Drive
P5
P4
P3
P2
P1
Plotters
resource
3
Tape
0
1
0
4
Drive
total no of each
Scanners
4
Plotters
0
1
1
1
2 CD Roms
Scanners
0
1
0
1
CD Roms
0
0
0
1
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
2
0
3
0
0
Drive
Plotters
1
0
1
1
0
Tape
1
multiple resource
Drive
Scanners
1
0
0
1
0
Plotters
0
resources
Banker’s algorithm for
CD Roms
0
0
0
2
0
Scanners
Available (free)
2
6
Process
Drive
P5
P4
P3
P2
P1
Plotters
resource
3
Tape
-
-
0
1
0
Drive
total no of each
Scanners
4
Plotters
-
-
0
1
1
CD Roms
2
Scanners
-
-
0
1
0
CD Roms
-
-
0
0
0
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
2
0
3
0
0
Drive
1 Plotters
0
1
1
0
Tape
5
multiple resource
Drive
Scanners
1
0
0
1
0
Plotters
1
resources
Banker’s algorithm for
CD Roms
0
0
0
2
0
Scanners
Available (free)
3
6
Process
Drive
P5
P4
P3
P2
P1
Plotters
resource
3
Tape
-
-
0
1
0
Drive
total no of each
Scanners
Plotters 4
-
-
0
1
2
CD Roms
2
Scanners
-
-
0
1
1
CD Roms
-
-
0
0
2
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
2
0
3
0
0
Drive
Plotters
1
0
1
0
0
Tape
5
multiple resource
Drive
Scanners
1
0
0
0
0
Plotters
0
resources
Banker’s algorithm for
CD Roms
0
0
0
0
0
Scanners
Available (free)
2
6
Process
Drive
P5
P4
P3
P2
P1
Plotters
resource
3
Tape
-
-
-
0
1
Drive
total no of each
Scanners
4
Plotters
-
-
-
0
1
CD Roms
2
Scanners
-
-
-
0
1
CD Roms
-
-
-
0
0
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
2
0
3
0
0
Drive
Plotters
1
0
1
0
0
Tape
5
multiple resource
Drive
Scanners
1
0
0
0
0
Plotters
2
resources
Banker’s algorithm for
CD Roms
0
0
0
0
0
Scanners
Available (free)
3
6
Process
Drive
P5
P4
P3
P2
P1
Plotters
resource
3
Tape
-
-
-
0
4
Drive
total no of each
Scanners
4
Plotters
-
-
-
0
2
CD Roms
2
Scanners
-
-
-
0
1
CD Roms
-
-
-
0
0
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
2
0
0
0
0
Drive
Plotters
1
0
0
0
0
Tape
2
multiple resource
Drive
Scanners
1
0
0
0
0
Plotters
1
resources
Banker’s algorithm for
CD Roms
0
0
0
0
0
Scanners
Available (free)
3
6
Process
Drive
P5
P4
P3
P2
P1
Plotters
resource
3
Tape
-
-
-
-
0
Drive
total no of each
Scanners
4
Plotters
-
-
-
-
0
CD Roms
2
Scanners
-
-
-
-
0
CD Roms
-
-
-
-
0
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
2
0
0
0
0
Drive
Plotters
1
0
0
0
0
Tape
6
multiple resource
Drive
Scanners
1
0
0
0
0
Plotters
3
resources
Banker’s algorithm for
CD Roms
0
0
0
0
0
Scanners
Available (free)
4
6
Process
Drive
P5
P4
P3
P2
P1
Plotters
resource
3
Tape
-
-
-
-
2
Drive
total no of each
Scanners
Plotters 4
-
-
-
-
1
CD Roms
2
Scanners
-
-
-
-
1
CD Roms
-
-
-
-
0
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
0
0
0
0
0
Drive
Plotters
0
0
0
0
0
Tape
4
multiple resource
Drive
Scanners
0
0
0
0
0
Plotters
2
resources
Banker’s algorithm for
CD Roms
0
0
0
0
0
Scanners
Available (free)
3
6
Process
Drive
P5
P4
P3
P2
P1
Plotters
resource
3
Tape
-
-
-
-
-
Drive
total no of each
Scanners
Plotters 4
-
-
-
-
-
CD Roms
2
Scanners
-
-
-
-
-
CD Roms
-
-
-
-
-
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
0
0
0
0
0
Drive
Plotters
0
0
0
0
0
Tape
6
multiple resource
Drive
Scanners
0
0
0
0
0
Plotters
3
resources
Banker’s algorithm for
CD Roms
0
0
0
0
0
Scanners
Available (free)
4
6
Process
Drive
P5
P4
P3
P2
P1
Plotters
resource
3
Tape
0
1
1
0
3
Drive
total no of each
Scanners
4
Plotters
0
1
1
1
0
CD Roms
2
Scanners
0
0
1
0
1
CD Roms
0
1
0
0
1
Tape
5
Scanners
2
Process
P5
P4
P3
P2
P1
CD Roms
2
Tape
2
0
3
0
1
Drive
Plotters
1
0
1
1
1
Tape
1
multiple resource
Drive
Scanners
1
1
0
1
0
Plotters
0
resources
Banker’s algorithm for
CD Roms
0
1
0
2
0
Scanners
Available (free)
2
• Data Structures
Available: Vector of length m. If Available[j] = k, there are k
instances of resource type Rj available.
Allocation: n m matrix. If Allocation[i,j] = k, then process
Pi is currently allocated k instances of resource type Rj.
Request: An n m matrix indicates the current request of
each process. If Request [i,j] = k, then process Pi is
requesting k more instances of resource type Rj .
Deadlock Detection Algorithm
• Step 1: Let Work and Finish be vectors of length m and n,
respectively. Initialize
– Work := Available
– For i = 1,2,…,n, if Allocation(i) 0, then Finish[i] := false,
otherwise Finish[i] := true.
• Step 2: Find an index i such that both:
– Finish[i] = false
– Request (i) Work
– If no such i exists, go to step 4.
Deadlock Detection Algorithm
• Step 3: Work := Work + Allocation(i)
– Finish[i] := true
– go to step 2
• Step 4: If Finish[i] = false for some i, 1 i n, then the
system is in a deadlock state. Moreover, if Finish[i] = false,
then Pi is deadlocked.
• Algorithm requires an order of m (n^2) operations to
detect whether the system is in a deadlocked state.
Deadlock detection for
multiple resource
Scanners
Scanners
CD Roms
CD Roms
Plotters
Plotters
Drive
Drive
Tape
Tape
4 2 3 1 2 1 0 0
total no of each no of resources that
E= A=
resource are available (free)
Scanners
Scanners
CD Roms
CD Roms
Plotters
Plotters
Process
Process
Drive
Drive
Tape
Tape
P1 0 0 1 0 P1 2 0 0 1
P2 2 0 0 1 P2 1 0 1 1
P3 0 1 2 0 P3 2 1 0 0
no of resources held by each no of resources still needed
C= process
R= by each process to proceed
Deadlock detection for
multiple resource
Scanners
Scanners
CD Roms
CD Roms
Plotters
Plotters
Drive
Drive
Tape
Tape
4 2 3 1 0 0 0 0
E= total no of each no of resources that
resource A= are available (free)
Scanners
Scanners
CD Roms
CD Roms
Plotters
Plotters
Process
Process
Drive
Drive
Tape
Tape
P1 0 0 1 0 P1 2 0 0 1
P2 2 0 0 1 P2 1 0 1 1
P3 2 2 2 0 P3 0 0 0 0
no of resources held by each no of resources still needed
C= process
R = by each process to proceed
Deadlock detection for
multiple resource
Scanners
Scanners
CD Roms
CD Roms
Plotters
Plotters
Drive
Drive
Tape
Tape
4 2 3 1 2 2 2 0
total no of each no of resources that
E= resource A = are available (free)
Scanners
Scanners
CD Roms
CD Roms
Plotters
Plotters
Process
Process
Drive
Drive
Tape
Tape
P1 0 0 1 0 DEADLOCK P1 2 0 0 1
P2 2 0 0 1 P2 1 0 1 1
P3 0 0 0 0 P3 0 0 0 0
no of resources held by each no of resources still needed
C= process
R= by each process to proceed
Detection-Algorithm Use
•When, and how often to invoke depends on:
– How often a deadlock is likely to occur?
– How many processes will need to be rolled back?
• One for each disjoint cycle
•How often --
– Every time a request for allocation cannot be granted immediately
• Allows us to detect set of deadlocked processes and process that
“caused” deadlock. Extra overhead.
• Every hour or whenever CPU utilization drops.
– With arbitrary invocation there may be many cycles in the resource
graph and we would not be able to tell which of the many
deadlocked processes “caused” the deadlock.
Deadlock recovery
1. Recovery through pre-emption
– In this method resources are temporarily taken away
from its current owner and give it to another process.
– The ability to take a resource away from a process, have
another process use it, and then give it back without the
process noticing it is highly dependent on the nature of
the resource.
– Recovering this way is frequently difficult or impossible.
Request
P
Hold
1 Hold
R1 R2
P Hold
2
Deadlock recovery
2. Recovery through rollback
– PCB (Process Control Block) and resource state are periodically
saved at “checkpoint”.
– When deadlock is detected, rollback the preempted process up
to the previous safe state before it acquired that resource.
– Discard the resource manipulation that occurred after that
checkpoint.
– Start the process after it is determined it can run again.
A A A
F1 F2
R R
Second B
Checkpoint
First
Checkpoint
Deadlock recovery
3. Recovery through killing processes
– The simplest way to break a deadlock is to kill one or more
processes.
• Kill all the process involved in deadlock
• Kill process one by one.
– After killing each process check for deadlock
» If deadlock recovered then stop killing more process
» Otherwise kill another process
Thank you🙂