Deadlocks
processes are blocked, waiting on events that could never happen
![]() |
![]() |
Examples:
Four necessary conditions for deadlock:
P0 waits for resources held P1
P1 waits for resources held P2
......
Pn-1 waits for resources held Pn
Pn waits for resources held P0
Resource-allocation graphs
the process is holding the resource |
the process is requesting the resource |
cycle ~ deadlock
Deadlock
if graph contains no cycles => no process is in deadlock
if graph contains cycles => deadlock may exist
Reduction of Resource Graph
If a process's resource requests can be granted, then we say that
the graph can be reduced by that process
( i.e. the process is allowed to complete its execution and return
all its resources )


A deadlock example in Java:
a) The Ostrich Algorithm
pretend there's no problem ( Windows, UNIX )
undetected deadlock may result in deterioration of the system
performance; eventually, the system will stop functioning
and will need to be restarted manually
b) Detection and Recovery
check resource graph to see if cycle exists; kill a process if necessary
or if a process has been blocked for, say one hour, kill it
c) Deadlock Prevention
before a process can request any additional resources, it must release
temporarily all the resources that it currently holds, if
successful, it can get teh original resources back
may have starvation
order resource types
process requests resources in an increasing order
Let R = { R1, R2, ..., Rm } be the set of resource types
e.g.
Suppose a process first requests Ri, it can request Rj only if F(Rj) > F(Ri)
Under this constraint, circular-wait condition cannot hold
Proof
by contradiction
Suppose circular wait holds for
{ P0, P1, ..., Pn }
Pi is waiting for Ri which is held by Pi+1
Because Pi+1 is holding Ri while requesting Ri+1, we have
d) Deadlock Avoidance
In prevention strategy, we restrain how request can be made.
Here, we want to develop an algorithm to avoid deadlock by making the right choice all the time
Dijkstra's Banker's Algorithm is an approach to trying to give processes as much as is possible, while guaranteeing no deadlock.
safe state -- a state is safe if the system can allocate resources to each process in some order and still avoid a deadlock.
bank:
| credits available = 10 units | ||
| Process | Using ( allocated ) | Maximum needs |
|---|---|---|
| P0 | 0 | 6 |
| P1 | 0 | 5 |
| P2 | 0 | 4 |
| P3 | 0 | 7 |
At time t0, the system is safe.
At time t1:
| credits available = 2 units | |||
| Process | Using ( allocated ) | Maximum needs | Maximum requests |
|---|---|---|---|
| P0 | 1 | 6 | 5 |
| P1 | 1 | 5 | 4 |
| P2 | 2 | 4 | 2 |
| P3 | 4 | 7 | 3 |
credits left = 2 ( i.e. available = 2 ), state is still safe
delay any requests from P0, P1, P3
can grant further requests to P2, why?
| credits available = 1 unit | |||
| Process | Using ( allocated ) | Maximum needs | Maximum requests |
|---|---|---|---|
| P0 | 1 | 6 | 5 |
| P1 | 2 | 5 | 3 |
| P2 | 2 | 4 | 2 |
| P3 | 4 | 7 | 3 |
This is an unsafe state; with only one credit left, if all processes request their maximum units, all need to be blocked and wait → deadlock
Of course, unsafe state may not lead to deadlock but the bank cannot count on this.
The banker's algorithm is to consider each request as it occurs,
if granting the request would result in a safe state, request is granted
otherwise, it is postponed
A deadlock path
An avoidance path
Let
Example
| a b c d | |
| EXISTING = | ( 6 3 4 2 ) |
| POSSESSED = | ( 5 3 2 2 ) |
| AVAILABLE = | ( 1 0 2 0 ) |
Notation
e.g. A = ( 1, 7, 3, 2 ), and B = ( 0, 3, 1, 1 ) then B ≤ A
X < Y if X ≤ Y and X ≠ Y
i.e. at least one element of X is strictly smaller than the corresponding
element of Y
Safe Algorithm
TERMINATE = ( ... ) of length n
TERMINATE[i] = false for i = 1, 2, .. n ( i.e. no process is terminated )
if no such i exists, ( i.e. either all terminated or may lead to deadlock ), go to step 5
In our example, is the state safe ?
Now suppose P5 wants 2 c ( e.g. printers )
Suppose we grant the 2 c to P5, will the state be safe? Let's check:
| ALLOCATED= | ![]() |
| ![]() | NEED = | ![]() |
| ![]() |
| Available = ( | 1 | 0 | 0 | 0 | ) |
Then the remaining resources ( AVAILABLE ) are not sufficient to satisfy the max need of any process to run to the end. Thus the state is not safe. Therefore the request of P5 must not be granted at this moment.
Note that an unsafe state does not necesarily lead to deadlock.
Weaknesses of the Banker's Algorithm