Title: Deadlocks
1Deadlocks
2Chapter 8 Deadlocks
- System Model
- Deadlock Characterization
- Methods for Handling Deadlocks
- Deadlock Prevention
- Deadlock Avoidance
- Deadlock Detection
- Deadlock Recovery
- Combined Approach
3Process Resource Trajectory
u
B
Printer
I8 I7 I6 I5
t
r
Plotter
s
A
p
q
I1 I2 I3 I4
Printer
Plotter
4Process Resource Trajectory
- Every point in the diagram represents a joint
state of the two processes - With a single processor, all progress is vertical
or horizontal, never diagonal - At I1, task A got the printer
- when B reaches q, it requests the plotter.
- Should it be given the plotter?
- Box I1-I2, I5-I6 is unsafe if the process
arrives at I2-I6 it will deadlock. - At point p, the only safe route is to run Process
A until it gets to I4.
5Safe State
- When a process requests an available resource,
system must decide if immediate allocation leaves
the system in a safe state. - System is in safe state if it is not deadlocked,
and there is some scheduling order in which every
process can run to completion even if all of them
suddenly request their maximum number of
resources immediately. - Sequence ltP1, P2, , Pngt 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 lt 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
6Basic Facts
- If a system is in safe state ? no deadlocks.
- If a system is in unsafe state ? possibility of
deadlock. - Avoidance ? ensure that a system will never enter
an unsafe state.
7Safe State Example
Ten instances of the one resource exist
(a)
The state of (a) is safe because there exists a
sequence of allocations that allows all processes
to complete.
8Unsafe State Example
Ten instances of the resource exist
Dead lock
(b)
The state of (b) is unsafe because there is no
sequence that guarantees completion.
9Bankers Algorithm Single Resource
Ten instances of the credit resource
exist Customers have maximum credit totaling 22
Dead lock ?
(b)
(c)
The state of (b) is safe because with two units
left, the banker grant Cs request, delaying
others until C pays back the loan. (c) is unsafe
not enough for any.
10Bankers Algorithm Single Resource
- Consider each request as it occurs, and see if
granting it leads to a safe state. - If safe, the request is granted.
- If unsafe, the request is postponed.
- To see if a state is safe, the banker checks to
see if he has enough resources to satisfy some
customer. - If so, the loan is assumed to be repaid, and the
customer now closest to the limit is checked, - If all loans can eventually be repaid, the state
is safe and the initial request can be granted.
11Bankers Algorithm Multiple Resources
Multiple instances of multiple resources exist.
Tape Drives
CD Drives
Plotters
Scanners
E ( 6 4 4 2) P ( 5 4 2 2) A ( 1 0 2 0 )
E total P possessed A available
C R
12Bankers Algorithm Multiple Resources
Multiple instances of multiple resources exist.
Tape Drives
CD Drives
Plotters
Scanners
E ( 6 4 4 2) P ( 5 4 3 2) A ( 1 0 1 0 )
E total P possessed A available
C R
- 1. Satisfy Process D, as its needs fit in
Available A
13Bankers Algorithm Multiple Resources
Multiple instances of multiple resources exist.
Tape Drives
CD Drives
Plotters
Scanners
E ( 6 4 4 2) P ( 4 2 2 1) A ( 2 2 2 1 )
E total P possessed A available
C R
- 2. Assume D completed, add to Available A
14Bankers Algorithm Multiple Resources
Multiple instances of multiple resources exist.
Tape Drives
CD Drives
Plotters
Scanners
E ( 6 4 4 2) P ( 6 3 3 1) A ( 0 1 1 1 )
E total P possessed A available
C R
- 3. Satisfy Process E, as its needs fit in
Available A
15Bankers Algorithm Multiple Resources
Multiple instances of multiple resources exist.
Tape Drives
CD Drives
Plotters
Scanners
E ( 6 4 4 2) P ( 4 2 2 1) A ( 2 2 2 1 )
E total P possessed A available
C R
- 4. Assume E completed, add to Available A
16Bankers Safety Algorithm
- Look for a row, R, whose unmet resource needs are
all smaller than or equal to A. - If no such row exists, the system will deadlock
since no process can run to completion. - If such a row does exist, assume the process
requests its maximum number of resources and
finishes. Mark that process as terminated and
add all its resources to A. - Repeat Steps 1 and 2 until either all processes
are marked terminated (in which case the initial
state was safe), or until a deadlock occurs (in
which case it was unsafe).
17Bankers Resource-Request Algorithm
- Check that request R lt maximum request M
- If true, proceed if false, flag error and exit.
- Check that request R ? available A.
- If true, proceed if false, wait for resources.
- Pretend to allocate resources by adding them to
correct row in Assignment Matrix C, and removing
them from available Vector A - Check safety of the resulting system
- If safe, allocate resources
- If unsafe, refuse request and wait for resources.
18Example of Bankers Algorithm
- Suppose E (10, 5, 7), with five processes
- Snapshot at time T0
- Allocation Max Available Need
- A B C A B C A B C ABC
- P0 0 1 0 7 5 3 3 3 2 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
- The system is in a safe state since the sequence
- lt P1, P3, P4, P2, P0gt satisfies safety
criteria.
19Example of Bankers Algorithm
- Suppose P1 requests (1,0,2)
- Check that Request ? Available
- that is, (1,0,2) ? (3,3,2) ? true.
- If granted, then
- Allocation Max Available Need
- A B C A B C A B C A B C
- P0 0 1 0 7 5 3 2 3 0 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 1 4 3 1
- Executing safety algorithm shows that sequence
- ltP1, P3, P4, P0, P2gt satisfies safety
requirement. - ? The request can be granted.
20Example of Bankers Algorithm
- Can request for (3,3,0) by P4 be granted?
- Can request for (0,2,0) by P0 be granted?
- Starting from
- Allocation Max Available Need
- A B C A B C A B C A B C P0 0 1 0 7 5 3
2 3 0 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
21Bankers Algorithm Practicalities
- In theory, Bankers Algorithm works well.
- However,
- In practice, processes rarely know in advance
what their maximum resource needs will be - The number of processes is not fixed, but
dynamically varying as new users log in and out. - Resources that were thought to be available can
suddenly vanish (tape drives can break). - Thus, in practice, few existing systems use the
bankers algorithm for avoiding deadlocks.
22Resource-Allocation Graph Algorithm
- Processes indicate all needed resources before
they begin executing. - Claim edge Pi ? Rj indicates that process Pj may
at some time request resource Rj - represented by a dashed line
- Claim edge converts to request edge
- when a process requests a resource.
- When a resource is released by a process,
assignment edge reconverts to a claim edge.
23Resource-Allocation Graph for Deadlock Avoidance
Safe Unsafe
Good for single instances of multiple resources.
24Resource-Allocation Graph Algorithm
- Suppose that Process Pi requests Resource Rj.
- The request can be granted only if converting the
request edge Pi ? Rj does not result in the
formation of a cycle in the resource-allocation
graph. - If no cycle exists, then the allocation of the
resource will leave the system in a safe state. - If a cycle is found, then the allocation will put
the system in an unsafe state. - (Process Pi will have to wait.)
253. Deadlock Detection and Recovery
- Allow system to enter deadlock state
- Detection algorithm
- Recovery scheme
26Resource-Allocation Graph and Wait-for Graph
Resource-Allocation Graph
Corresponding wait-for graph
Good for single instances of multiple resources.
27Wait-for Graph
- Applicable to single instance of each resource
type - To detect deadlocks, maintain wait-for graph
- Nodes are processes.
- Pi ? Pj if Pi is waiting for Pj.
- Periodically invoke an algorithm that searches
for a cycle in the graph. - An algorithm to detect a cycle in a graph
requires an order of n2 operations, where n is
the number of vertices in the graph.
28Deadlock Detection
Multiple instances of each resource
Resources in existence
Resources available
(E1 , E2, E3 , Em)
(A1 , A2, A3 , Am)
Request matrix
Current allocation matrix
R11 R12 R13 R1m R21 R22 R23 R2m
.. .. .. .. .. Rn1 Rn2 Rn3 Rnm
- C11 C12 C13 C1m
- C21 C22 C23 C2m
- .. .. .. .. ..
- Cn1 Cn2 Cn3 Cnm
Row n is current allocation to process n
Row n is what process n needs.
29Resource Allocation Example
Tape Drives
Scanners
Tape Drives
Scanners
CD Drives
Plotters
CD Drives
Plotters
E ( 4 2 3 1 )
A ( 2 1 0 0 )
Resources in existence
Resources available to be allocated
C
Request matrix
Current allocation matrix
30Deadlock Detection
Multiple instances of each resource
- Define A ? B to mean that each element of vector
A is less than or equal to each element of vector
B. - Look for an unmarked process Pi for which the
i-th row of R is less than or equal to A - If such a process is found, add the i-th row of C
to A, mark the process and go back to Step 1 - If no such process exists, the algorithm
terminates. - On termination, all unmarked processes are
deadlocked.
31Several Instances of a Resource Type
- Available A vector of length m indicates the
number of available resources of each type. - Allocation An n ? m matrix defines the number
of resources of each type currently allocated to
each process. - Request An n ? m matrix indicates the current
request of each process. If Request ij k,
then process Pi is requesting k more instances of
resource type. Rj.
32Detection Algorithm
- 1. Let Work and Finish be vectors of length m and
n, respectively. Initialize - (a) Work Available
- (b) For i 1,2, , n, if Allocationi ? 0, then
Finishi falseotherwise, Finishi true. - 2. Find an index i such that both
- (a) Finishi false
- (b) Requesti ? Work
- If no such i exists, go to Step 4.
33Detection Algorithm
- 3. Work Work AllocationiFinishi trueGo
to step 2. - If Finishi false, for some i, 1 ? i ? n,
then the system is in deadlock state. - Moreover, if Finishi false, then Pi is
deadlocked. -
Reclaim resources, assume its finished
Algorithm requires an order of O(m x n2)
operations to detect whether the system is in
deadlocked state.
34Example of Detection Algorithm
- Five processes P0 through P4 three resource
types A (7 instances), B (2 instances), and C (6
instances). - Snapshot at time T0
- Allocation Request Available
- A B C A B C A B C
- P0 0 1 0 0 0 0 0 0 0
- P1 2 0 0 2 0 2
- P2 3 0 3 0 0 0
- P3 2 1 1 1 0 0
- P4 0 0 2 0 0 2
- Sequence ltP0, P2, P3, P1, P4gt will result in
Finishi true for all i. This state is not
deadlocked.
35Example of Detection Algorithm
- Suppose P2 requests an additional instance of
type C. - Then state is
- Allocation Request Available
- A B C A B C A B C
- P0 0 1 0 0 0 0 0 0 0
- P1 2 0 0 2 0 2
- P2 3 0 3 0 0 1
- P3 2 1 1 1 0 0
- P4 0 0 2 0 0 2
- State of system?
- Can reclaim resources held by process P0, but
insufficient resources to fulfill requests of
other processes. - Deadlock exists, consisting of processes P1, P2,
P3, and P4.
36Detection-Algorithm Usage
- 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)
- If detection algorithm is invoked arbitrarily,
there may be many cycles in the resource graph
and so we would not be able to tell which of the
many deadlocked processes caused the deadlock.
37Recovery from Deadlock Process Termination
- Abort all deadlocked processes. ? lose their
results - Abort one process at a time until the deadlock
cycle is eliminated, invoking deadlock detection
each time. - In which order should we choose to abort?
- Priority of the process.
- How long process has computed, and how much
longer to completion. - Resources the process has used. (e.g. Simple to
preempt?) - Resources process needs to complete.
- How many processes will need to be terminated.
- Is process interactive or batch?
38Recovery from Deadlock Resource Preemption
- Successively preempt some resources and give them
to other processes until deadlock is broken. - Issues
- Selecting a victim minimize cost
- (eg. Take into account time spent so far).
- Rollback return to some safe state, restart
process from that state. - Starvation same process may always be picked
as victim, so include number of rollbacks in cost
factor.
39Combined Approach to Deadlock Handling
- Combine the three basic approaches
- prevention
- avoidance
- detection
- allowing the use of the optimal approach for
each resource in the system. - Partition resources into hierarchically ordered
classes. - Use most appropriate technique for handling
deadlocks within each class.
40Traffic Deadlock
41Traffic Deadlock
- Show that the four necessary conditions for
deadlock indeed hold in this example. - Mutual exclusion
- Hold and wait
- No preemption
- Circular wait
- State a simple rule that will avoid deadlocks in
this system.