Deadlocks
Deadlocks
1
08/11/2021
2
08/11/2021
3
08/11/2021
P1 P2
4
08/11/2021
P1 P1 P1
R2a
R2b
P1 P1 P1
R2a
R2b
5
08/11/2021
Basic Facts
If graph contains no cycles no deadlock.
If graph contains a cycle
if only one instance per resource type, then deadlock.
if several instances per resource type, possibility of
deadlock.
6
08/11/2021
Ignoring deadlocks:
The Ostrich Algorithm
Sounds stupid, but...
Consider:
How often will a deadlock happen?
How severe will it be if it does happen?
How hard would it be to avoid/prevent/detect?
Deadlock detection/recovery
Detection:
Monitor resource allocation using (e.g.) a digraph
If detect a cycle ➔ deadlock has occurred
Recovery:
Kill one of the processes
If that doesn’t work: kill another, etc.
Another alternative: just look for processes that have
been idle for a long time and kill them
May be okay when aborting and restarting is okay
(e.g., batch jobs)
7
08/11/2021
Deadlock prevention
Set things up so that deadlocks cannot occur at all
Done by attacking one of the deadlock conditions
Attacking mutual exclusion condition:
Don’t let non-sharable resources be assigned to anyone
E.g., spooling
Deadlock prevention
Attacking hold-and-wait condition:
Process can’t request a resource if holding any
One way: processes request all resources up front
Problem: may not know ahead of time what you need!
Problem: hold resources too long in general
Another approach: release all you’re holding
momentarily to request another
Attacking no preemption condition: not realistic
8
08/11/2021
Deadlock prevention
Attacking the circular wait condition:
Stupid way: processes can only hold a single resource at
a time
Better way:
Number the resources
Process can request whatever it wants, whenever it wants...as
long as the requests are in numerical order
No cycles – why not?
Deadlock prevention
Attacking the circular wait condition (cont’d):
Resource allocation graph can’t have cycles in this
scheme – why not?
Consider the case where process A holds resource i and B
holds j – deadlock only possible if A requests j and B requests i
If i > j, then A can’t request j
If j > i, then B can’t request i
Problem – may not be able to find an ordering that
satisfies everyone!
9
08/11/2021
Deadlock avoidance
Idea: predict when some action → deadlock, avoid it
Dijkstra’s Banker’s Algorithm (single resource version)
Modeled on the way a banker might deal with lines of credit
to customers
Deadlock if there is no way to guarantee that all customers
can borrow up to their maximum resource limit
Bank: let Bmax be maximum amount of money bank has to
loan
Each customer: let Ci be maximum amount he/she can
max
borrow
Banker can’t let C Bmax
imax
10
08/11/2021
11
08/11/2021
12
08/11/2021
13
08/11/2021
14
08/11/2021
15
08/11/2021
A 3 7 A 3 7
B 2 3 B 3 3
B wants 1
C 0 2 C 0 2
D 2 4 D 2 4
Remaining: 1 Remaining: 0
16
08/11/2021
A 3 7 A 3 7
B 2 3 B 3 3
B wants 1
C 0 2 C 0 2
D 2 4 D 2 4
Remaining: 1 Remaining: 0
A 6 7 A 7 7
B 0 3 B 0 3
A wants 1
C 0 2 C 0 2
D 1 4 D 1 4
Remaining: 1 Remaining: 0
17
08/11/2021
A 6 7 A 7 7
B 0 3 B 0 3
A wants 1
C 0 2 C 0 2
D 1 4 D 1 4
Remaining: 1 Remaining: 0
A 4 7 A 4 7
B 1 3 B 1 3
D wants 1
C 0 2 C 0 2
D 1 4 D 2 4
Remaining: 2 Remaining: 1
18
08/11/2021
A 4 7 A 4 7
B 1 3 B 1 3
D wants 1
C 0 2 C 0 2
D 1 4 D 2 4
Remaining: 2 Remaining: 1
19
08/11/2021
Summary
Handling deadlocks is difficult
No best general solution
How you choose to handle it depends on your
situation: trade-offs
20