Deadlock
Deadlock
Content
• Principles of deadlock,
• Deadlock Prevention,
• Deadlock Avoidance,
• Deadlock Detection,
• Deadlock Recovery.
Potential Deadlock
I need quad
I need quad A and B
D and A
Actual Deadlock
HALT until B
HALT until A is free
is free
DEADLOCKS
■ Permanent blocking of a single or set of processes, competing
for system resources or may want to cooperate for
communication.
■ Formal definition :
A set of processes is deadlocked if each process in the set is
waiting for an event that only another process in the set can
cause.
■ Usually the event is release of a currently held resource.
■ Generally it is because of the conflicting needs of different
processes.
■ There is no general solution to solve it completely.
Resource Categories
– Dining Philosophers
– Request
– Lock
– Use
– Release
Resources
• Process must wait if request is denied
❑ Requesting process may be blocked
• Deadlocks
• utility program A
deadlock
❑ Copies a file from a tape to
disk
❑ Prints the file to a printer
• Resources
❑ Tape
❑ Disk
❑ Printer
System Model
❑ request
❑ use
❑ release
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
1. Mutual exclusion :Only single process is allowed to use the
resource.
2. Hold and wait :Process holding at least one resource and
waiting to acquire additional resources currently held by other
processes.
3. No preemption :No resource can be removed forcibly from a
process.
4. Circular wait: A closed chain of processes exists, such that
each process holds at least one resource needed by the next
process in the chain
▪ First three are necessary but not sufficient conditions for a
deadlock to exist
Resource-Allocation Graph
Resource-Allocation Graph
• Process node
Pi
• Resource node Rj
• Assignment edge
• Request edge
Example of a Resource allocation graph
And this?
Yes deadlock
… and this?
■ Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources
that Pi can still request can be satisfied by currently available
resources + resources held by all the Pj, with j<i.
❑ If Pi resource needs are not immediately available, then Pi
can wait until all Pj have finished.
❑ When Pj is finished, Pi can obtain needed resources,
execute, return allocated resources, and terminate.
❑ When Pi terminates, Pi+1 can obtain its needed resources,
and so on.
Safe, Unsafe , Deadlock State
• Snapshot at t0
A B C A B C 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
Example
• 5 processes P0 through P4
• 3 resource types: A (10 instances), B (5 instances),
and C (7 instances)
• Snapshot of system at t0 : is it safe?
Proc Allocation Max Need
ess
A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Safe State?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 3 3 2
P0 0 1 0 7 4 3 F
P1 2 0 0 1 2 2 F
P2 3 0 2 6 0 0 F
P3 2 1 1 0 1 1 F
P4 0 0 2 4 3 1 F
Safe State?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 3 3 2
P0 0 1 0 7 4 3 F F 5 3 2
P1 2 0 0 1 2 2 F T
P2 3 0 2 6 0 0 F F
P3 2 1 1 0 1 1 F F
P4 0 0 2 4 3 1 F F
Safe State?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 3 3 2
P0 0 1 0 7 4 3 F F F 5 3 2
7 4 3
P1 2 0 0 1 2 2 F T T
P2 3 0 2 6 0 0 F F F
P3 2 1 1 0 1 1 F F T
P4 0 0 2 4 3 1 F F F
Safe State?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 3 3 2
P0 0 1 0 7 4 3 F F F T 5 3 2
7 4 3
P1 2 0 0 1 2 2 F T T T
7 5 3
P2 3 0 2 6 0 0 F F F F
P3 2 1 1 0 1 1 F F T T
P4 0 0 2 4 3 1 F F F F
Safe State?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 3 3 2
P0 0 1 0 7 4 3 F F F T T 5 3 2
7 4 3
P1 2 0 0 1 2 2 F T T T T
7 5 3
P2 3 0 2 6 0 0 F F F F T
10 5 5
P3 2 1 1 0 1 1 F F T T T
P4 0 0 2 4 3 1 F F F F F
Safe State?
Pr Allocation Need Fi Fi Fi Fi Fi Fi
oc ni ni ni ni ni ni
es s s sh sh sh sh Work
s h h
3 3 2
A B C A B C
5 3 2
P0 0 1 0 7 4 3 F F F T T T
7 4 3
P1 2 0 0 1 2 2 F T T T T T
7 5 3
P2 3 0 2 6 0 0 F F F F T T 10 5 5
P3 2 1 1 0 1 1 F F T T T T 10 5 7
P4 0 0 2 4 3 1 F F F F F T
A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Is the state safe?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 3 0
P0 0 1 0 7 4 3 F
P1 3 0 2 0 2 0 F
P2 3 0 2 6 0 0 F
P3 2 1 1 0 1 1 F
P4 0 0 2 4 3 1 F
Is the state safe?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 3 0
P0 0 1 0 7 4 3 F F 5 3 2
P1 3 0 2 0 2 0 F T
P2 3 0 2 6 0 0 F F
P3 2 1 1 0 1 1 F F
P4 0 0 2 4 3 1 F F
Is the state safe?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 3 0
P0 0 1 0 7 4 3 F F F 5 3 2
7 4 3
P1 3 0 2 0 2 0 F T T
P2 3 0 2 6 0 0 F F F
P3 2 1 1 0 1 1 F F T
P4 0 0 2 4 3 1 F F F
Is the state safe?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 3 0
P0 0 1 0 7 4 3 F F F T 5 3 2
7 4 3
P1 3 0 2 0 2 0 F T T T
7 5 3
P2 3 0 2 6 0 0 F F F F
P3 2 1 1 0 1 1 F F T T
P4 0 0 2 4 3 1 F F F F
Is the state safe?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 3 0
P0 0 1 0 7 4 3 F F F T T 5 3 2
7 4 3
P1 3 0 2 0 2 0 F T T T T
7 5 3
P2 3 0 2 6 0 0 F F F F T
10 5 5
P3 2 1 1 0 1 1 F F T T T
P4 0 0 2 4 3 1 F F F F F
Is the state safe?
Pr Allocation Need Fi Fi Fi Fi Fi Fi
oc ni ni ni ni ni ni
es s s sh sh sh sh Work
s h h
2 3 0
A B C A B C
5 3 2
P0 0 1 0 7 4 3 F F F T T T
7 4 3
P1 3 0 2 0 2 0 F T T T T T
7 5 3
P2 3 0 2 6 0 0 F F F F T T 10 5 5
P3 2 1 1 0 1 1 F F T T T T 10 5 7
P4 0 0 2 4 3 1 F F F F F T
A B C A B C A B C
P0 0 3 0 7 5 3 7 2 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Is the state safe?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 1 0
P0 0 3 0 7 2 3 F
P1 3 0 2 0 2 0 F
P2 3 0 2 6 0 0 F
P3 2 1 1 0 1 1 F
P4 0 0 2 4 3 1 F
Is the state safe?
Pr Allocation Need Fi Fi Fi Fi Fi
oc ni ni ni ni ni
es s s sh sh sh Work
s h h
A B C A B C 2 1 0
P0 0 3 0 7 2 3 F
P1 3 0 2 0 2 0 F
P2 3 0 2 6 0 0 F
P3 2 1 1 0 1 1 F
P4 0 0 2 4 3 1 F
Example : Is the below system in safe state?
Pr Allocation Max
oc
es
s
A B C D A B C D Available
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Deadlock Detection
– How long the process has computed and how much longer the
process will compute before completing its designated task?
– How many and what type of resources the process has used
(e.g., preemptable, non-preemptable)?