Deadlock
Tanenbaum Ch 6
Silberschatz Ch 7
The Deadlock Problem
• A Computing example: record scanned
document
• System has 1 scanner and 1 CD Recorder
• 2 processes each need both devices.
P0 acquires scanner and waits for CD
P1 acquires CD and waits for scanner
cs431-cotter
2
Preemptable and Nonpreemptable
Resources
Sequence of events required to use a
resource:
1. Request the resource.
2. Use the resource.
3. Release the resource.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
3
Resource Acquisition (1)
Figure 6-1. Using a semaphore to protect
resources.
(a) One resource. (b) Two resources.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
4
Resource Acquisition (2)
Figure 6-2. (a)
Deadlock-free
code.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
5
Resource Acquisition (3)
Figure 6-2. (b) Code
with a potential
deadlock.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
6
Introduction To Deadlocks
Deadlock can be defined formally
as follows:
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.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
7
Deadlock Characterization:
Necessary Conditions
• Mutual Exclusion:
– At least 1 resource must be held in a non-sharable
mode.
• Hold and wait:
– There must be at least 1 process that is holding
one resource and is waiting for another
• No preemption:
– Resources cannot be preempted
• Circular wait:
– There must be a cycle of dependency among a set
of processes and resources.
cs431-cotter
8
Deadlock Strategies
Strategies for dealing with deadlocks:
1. Just ignore the problem.
2. Detection and recovery. Let deadlocks
occur, detect them, take action.
3. Dynamic avoidance by careful resource
allocation.
4. Prevention, by structurally negating one
of the four required conditions.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
9
Ignore the Problem
• May not occur very often
• May not have serious consequences if it
does happen
• May be difficult / expensive to detect.
cs431-cotter
10
Resource Allocation Graphs
Process
A
Resource
R
Request a Resource
A
R
Hold a Resource
R
A
A
R
S
Deadlock
B
cs431-cotter
11
Resource Allocation Graph
Multiple Resources with No Cycles
P1
cs431-cotter
P2
P3
12
Graph with Deadlock
P1
cs431-cotter
P2
P3
13
Graph with Deadlock
P1
cs431-cotter
P2
P3
14
Graph with Cycle but no
Deadlock
P2
P1
P3
P4
cs431-cotter
15
Deadlock Detection with Multiple
Resources of Each Type
Figure 6-6. The four data structures needed
by the deadlock detection algorithm.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
16
Deadlock Detection with Multiple
Resources of Each Type (2)
Deadlock detection algorithm:
1. Look for an unmarked process, Pi , for which
the i-th row of R is less than or equal to A.
2. If such a process is found, add the i-th row of C
to A, mark the process, and go back to step 1.
3. If no such process exists, the algorithm
terminates.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
17
Deadlock Detection with Multiple
Resources of Each Type (3)
Figure 6-7. An example for the deadlock detection
algorithm.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
18
Deadlock Avoidance
• The previous approach can be used to avoid a
deadlock (rather than just detect one when it
occurs).
– Declare the maximum number of resources of each
type that will be required before the process starts.
– Before any resource allocation is made, ensure that
the allocation will not leave the system in a state
where a deadlock can occur.
– This will ensure that a process can always get access
to its needed maximums.
cs431-cotter
19
Safe State
• A system is in a safe state if there exists a
sequence of resource assignments
such that each process can be allocated
its maximum request, and then release the
resources, and then allow remaining
processes to do the same...
cs431-cotter
20
Safe State
• A system is in a safe state if there exists a sequence of
resource assignments such that each process can be
allocated its maximum request, and then release the
resources, and then allow remaining processes to do the
same...
Process Max Needs
P0
10
P1
4
P2
9
cs431-cotter
Current Needs
5
2
2
Total
12
Avail
3
21
Safe State
• A system is in a safe state if there exists a sequence of
resource assignments such that each process can be
allocated its maximum request, and then release the
resources, and then allow remaining processes to do the
same...
Process Max Needs
P0
10
P1
4
P2
9
cs431-cotter
Current Needs
5
24
2
Total
12
Avail
31
22
Safe State
• A system is in a safe state if there exists a sequence of
resource assignments such that each process can be
allocated its maximum request, and then release the
resources, and then allow remaining processes to do the
same...
Process Max Needs
P0
10
P1
0
P2
9
cs431-cotter
Current Needs
5
40
2
Total
12
Avail
15
23
Safe State
• A system is in a safe state if there exists a sequence of
resource assignments such that each process can be
allocated its maximum request, and then release the
resources, and then allow remaining processes to do the
same...
Process Max Needs
P0
10
P1
0
P2
9
cs431-cotter
Current Needs
510
0
2
Total
12
Avail
50
24
Banker's Algorithm
• Use vectors and matrices to identify resource
requirements
– Available[j] = k; //resource j has k members available
– Max[i,j] = k; //Process i may need k members of
resource j
– Allocation [i,j] = k; //Process i is currently allocated k
members of resource j
– Need[i,j] = k; // Process i may need as many as k
more of resource j.
cs431-cotter
25
Banker's Example
Request: P0 wants 0 2 0
P0
P1
P2
P3
P4
Allocate
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Max
A B
7 5
3 2
9 0
2 2
4 3
C
3
2
2
2
3
Avail.
A B C
3 3 2
Safe order: ???
cs431-cotter
26
Banker's Example (change 1)
P1 requests (1 0 2)
Allocate
Max
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
cs431-cotter
Avail.
A B C
3 3 2
27
Banker's Example (change 1)
P1 requests (1 0 2)
Allocate
Max
A B C
A B C
P0 0 1 0
7 5 3
P1 3 0 2
3 2 2
P2 3 0 2
9 0 2
P3 2 1 1
2 2 2
P4 0 0 2
4 3 3
cs431-cotter
Avail.
A B C
3 3 2
2 3 0
28
Banker's Example (change 1)
P1 requests (1 0 2)
Allocate
Max
A B C
A B
P0 0 1 0
7 5
P1 3 2 2
3 2
P2 3 0 2
9 0
P3 2 1 1
2 2
P4 0 0 2
4 3
Safe Order: P1,
cs431-cotter
C
3
2
2
2
3
Avail.
A B C
3 3 2
2 3 0
2 1 0
29
Banker's Example (change 1)
P1 requests (1 0 2)
Allocate
Max
A B C
A B
P0 0 1 0
7 5
P1 0 0 0
3 2
P2 3 0 2
9 0
P3 2 1 1
2 2
P4 0 0 2
4 3
Safe Order: P1,
cs431-cotter
C
3
2
2
2
3
Avail.
A B C
3 3 2
2 3 0
2 1 0
5 3 2
30
Banker's Example (change 1)
P1 requests (1 0 2)
Allocate
Max
A B C
A B
P0 0 1 0
7 5
P1 0 0 0
3 2
P2 3 0 2
9 0
P3 2 2 2
2 2
P4 0 0 2
4 3
Safe Order: P1, P3,
cs431-cotter
C
3
2
2
2
3
Avail.
A B C
3 3 2
2 3 0
2 1 0
5 3 2
5 2 1
31
Banker's Example (change 1)
P1 requests (1 0 2)
Allocate
Max
A B C
A B
P0 0 1 0
7 5
P1 0 0 0
3 2
P2 3 0 2
9 0
P3 0 0 0
2 2
P4 0 0 2
4 3
Safe Order: P1, P3,
cs431-cotter
C
3
2
2
2
3
Avail.
A B C
3 3 2
2 3 0
2 1 0
5 3 2
5 2 1
7 4 3
32
Banker's Example (change 1)
P1 requests (1 0 2)
Allocate
Max
A B C
A B C
P0 0 1 0
7 5 3
P1 0 0 0
3 2 2
P2 3 0 2
9 0 2
P3 0 0 0
2 2 2
P4 4 3 3
4 3 3
Safe Order: P1, P3, P4,
cs431-cotter
Avail.
A B C
3 3 2
2 3 0
2 1 0
5 3 2
5 2 1
7 4 3
3 1 2
33
Banker's Example (change 1)
P1 requests (1 0 2)
Allocate
Max
A B C
A B C
P0 0 1 0
7 5 3
P1 0 0 0
3 2 2
P2 3 0 2
9 0 2
P3 0 0 0
2 2 2
P4 0 0 0
4 3 3
Safe Order: P1, P3, P4,
cs431-cotter
Avail.
A B C
3 3 2
2 3 0
2 1 0
5 3 2
5 2 1
7 4 3
3 1 2
7 4 5
34
Banker's Example (change 1)
P1 requests (1 0 2)
Allocate
Max
Avail.
A B C
A B C
A B C
P0 7 5 3
7 5 3
3 3 2
P1 0 0 0
3 2 2
2 3 0
P2 3 0 2
9 0 2
2 1 0
P3 0 0 0
2 2 2
5 3 2
P4 0 0 0
4 3 3
5 2 1
Safe Order: P1, P3, P4, P0,
cs431-cotter
7
3
7
0
4
1
4
0
3
2
5
2
35
Banker's Example (change 1)
P1 requests (1 0 2)
Allocate
Max
Avail.
A B C
A B C
A B C
P0 0 0 0
7 5 3
3 3 2
P1 0 0 0
3 2 2
2 3 0
P2 3 0 2
9 0 2
2 1 0
P3 0 0 0
2 2 2
5 3 2
P4 0 0 0
4 3 3
5 2 1
Safe Order: P1, P3, P4, P0, P2 !!! OK
cs431-cotter
7
3
7
0
7
4
1
4
0
5
3
2
5
2
5
36
Banker's Example (change 2)
P4 requests (3 3 0)
Allocate
Max
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
cs431-cotter
Avail.
A B C
3 3 2
37
Banker's Example (change 2)
P4 requests (3 3 0)
Allocate
Max
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 3 3 2
4 3 3
cs431-cotter
Avail.
A B C
3 3 2
0 0 2
38
Banker's Example (change 2)
P4 requests (3 3 0)
Allocate
Max
Avail.
A B C
A B C
A B C
P0 0 1 0
7 5 3
3 3 2
P1 2 0 0
3 2 2
0 0 2
P2 3 0 2
9 0 2
P3 2 1 1
2 2 2
P4 0 0 2
4 3 3
No job can be completed. No Safe Order exists!
cs431-cotter
39
Recovery from Deadlock
• Recovery through preemption
• Recovery through rollback
• Recovery through killing
processes
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
40
Deadlock Prevention
• Attacking the mutual exclusion
condition
• Attacking the hold and wait condition
• Attacking the no preemption condition
• Attacking the circular wait condition
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
41
Attacking the
Circular Wait Condition
Figure 6-13. (a) Numerically ordered resources.
(b) A resource graph.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
42
Approaches to Deadlock
Prevention
Figure 6-14. Summary of approaches to deadlock
prevention.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
43
Other Issues
• Two-phase locking
• Livelock
• Starvation
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
44
Locking Protocols
• Shared Locks (read but not write)
• Exclusive Locks (read or write)
• A transaction may require a number of locks that
must be applied consistently (same sequence..)
• 2 Phase locking protocol
– Growing Phase (obtain but not release locks)
– Shrinking Phase (release but not obtain locks)
cs431-cotter
45
Livelock
Figure 6-16. Busy waiting that can lead to livelock.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
46
Starvation
• Occurs when there is a priority based selection
process.
• If there are many high priority tasks (in a
continuous stream) and only one (or a few) low
priority tasks, the low priority tasks will never be
selected
• Starvation avoidance
– First-come, First-served
– Priority enhancement
cs431-cotter
47
Summary
•
Deadlock Characteristics
–
–
–
–
•
Mutual Exclusion:
Hold and wait:
No preemption:
Circular wait:
Deadlock Strategies
1. Just ignore the problem.
2. Detection and recovery. Let deadlocks occur, detect them,
take action.
3. Dynamic avoidance by careful resource allocation.
4. Prevention, by structurally negating one of the four required
conditions.
•
•
Deadlock Tools and Techniques
Other Issues
cs431-cotter
48
Questions
• In the context of Operating System processes, what is a deadlock?
What conditions are required for a deadlock to exist?
• How does a resource allocation graph help identify opportunities for
deadlock? Please show an example of a resource allocation graph
that includes a deadlock.
• Discuss how the Banker's algorithm ensures that each process that
tries to get access to a critical section will eventually be guaranteed
of getting in.
• The Banker’s algorithm only requires that there be at least 1 safe
order to make a transaction safe, however there may be many
different orders possible. Using the example shown on slide 34,
how many safe orders exist given the request by P1?
• How might we handle deadlocks? Give at least 3 of the methods
discussed in the book and provide examples of each.
cs431-cotter
49