Chapter 7: Deadlocks
Chapter 7: Deadlocks
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Combined Approach to Deadlock Handling
P0 P1
wait (A); wait(B)
wait (B); wait(A)
1
Bridge Crossing Example
System Model
2
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Resource-Allocation Graph
3
Resource-Allocation Graph (Cont.)
Process
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
4
Resource Allocation Graph With A Deadlock
5
Basic Facts
6
Deadlock Prevention
No Preemption –
) If a process that is holding some resources requests
another resource that cannot be immediately allocated to it,
then all resources currently being held are released.
) Preempted resources are added to the list of resources for
which the process is waiting.
) Process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting.
7
Deadlock Avoidance
Safe State
When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state.
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.
8
Basic Facts
9
Resource-Allocation Graph Algorithm
10
Unsafe State In Resource-Allocation Graph
Banker’s Algorithm
Multiple instances.
11
Data Structures for the Banker’s Algorithm
Safety Algorithm
12
Resource-Request Algorithm for Process Pi
13
Example (Cont.)
14
Deadlock Detection
Detection algorithm
Recovery scheme
15
Resource-Allocation Graph and Wait-for Graph
16
Detection Algorithm
17
Example of Detection Algorithm
Example (Cont.)
18
Detection-Algorithm Usage
19
Recovery from Deadlock: Resource Preemption
20
Traffic Deadlock
21