Deadlock
Concurrency Issues
Past lectures:
¾ Problem: Safely coordinate access to shared resource
¾ Solutions:
Use semaphores, monitors, locks, condition variables
Coordinate access within shared objects
What about coordinated access across multiple objects?
¾ If you are not careful, it can lead to deadlock
Today’s
Today s lecture:
¾ What is deadlock?
¾ How can we address deadlock?
2
Deadlocks
Motivating Examples
Two producer processes share a buffer but use a different
protocol for accessing the buffers
Producer1() { Producer2(){
P(emptyBuffer
B ff ) d M L k)
P(producerMutexLock
P(producerMutexLock) P(emptyBuffer)
: :
} }
A postscript interpreter and a visualization program compete for
memory frames
PS_Interpreter()
P () { Visualize()
l () {
request(memory_frames, 10) request(frame_buffer, 1)
<process file> <display data>
request(frame_buffer, 1) request(memory_frames, 20)
<draw file on screen> <update display>
} }
The TENEX Case
If a process requests all systems buffers, operator
console tries to print an error message
To do so
¾ lock the console
¾ request a buffer
DUH!
4
Deadlock
Definition
Ready Running
Head
Tail
ready queue Waiting
Head semaphore/
Tail condition queues
A set of processes is deadlocked when every process in the set is
g for an event that can only
waiting y be g y some p
generated by process in
the set
Starvation vs. deadlock
¾ Starvation: threads wait indefinitely (e.g., because some other thread is
using a resource)
¾ Deadlock: circular waiting for resources
¾ Deadlock Î starvation, but not the other way
5
A Graph Theoretic Model of Deadlock
The resource allocation graph (RAG)
Basic components of any resource allocation problem
¾ Processes and resources
Model the state of a computer system as a directed graph
¾ G = (V,
(V E)
¾ V = the set of vertices = {P1, ..., Pn} ∪ {R1, ..., Rm}
Pi Rj
¾ E = the set of edges =
g from
{edges f m a resource to a process
p }∪
{edges from a process to a resource}
request allocation
Pi edge edge Pk
Rj
6
Resource Allocation Graphs
Examples
A PostScript interpreter that is waiting for the frame buffer lock
and a visualization process that is waiting for memory
V = {PS interpret, visualization} ∪ {memory frames, frame buffer lock}
Visualization Memory Frames
Process PostScript
Interpreter
Frame Buffer
7
A Graph Theoretic Model of Deadlock
Resource allocation graphs & deadlock
Theorem: If a resource allocation graph does not contain a cycle then
no processes are deadlocked
A cycle in a RAG is a necessary condition for deadlock
Is the existence of a cycle a sufficient condition?
Game
Memory Frames
Visualization PostScript
Process Interpreter
Frame Buffer 8
A Graph Theoretic Model of Deadlock
Resource allocation graphs & deadlock
Theorem: If there is only a single unit of all resources then a set of
processes are deadlocked iff there is a cycle in the resource
allocation graph
Memory Frames
Visualization PostScript
Process Interpreter
Frame Buffer
9
Using the Theory
An operational definition of deadlock
Visualization
P
Process Memory Frames P S i
PostScript
Interpreter
Frame Buffer
A set of processes are deadlocked iff the following conditions hold
simultaneously
1. Mutual exclusion is required for resource usage (serially useable)
2. A process is in a “hold-and-wait” state
3. Preemption of resource usage is not allowed
4. Circular waiting exists (a cycle exists in the RAG)
10
Dealing With Deadlock
Deadlock prevention & avoidance
Adopt some resource allocation protocol that ensures
deadlock can never occur
¾ Deadlock
D dl k prevention/avoidance
ti / id
Guarantee that deadlock will never occur
Generally breaks one of the following conditions:
Mutex
Hold-and-wait
No preemption
Circular wait *This is usually the weak link*
¾ Deadlock detection and recoveryy
Admit the possibility of deadlock occurring and periodically check for it
On detecting deadlock, abort
Breaks the no-preemption condition
What does the RAG for a lock look like?
11
Deadlock Avoidance
Resource Ordering
Recall this situation. How can we avoid it?
Producer1() { Producer2(){
P(emptyBuffer) P(producerMutexLock)
P(producerMutexLock
d M t L k) t B ff )
P(emptyBuffer
: :
} }
Eliminate circular waiting by ordering all locks (or
semaphores, or resoruces). All code grabs locks in a
predefined order. Problems?
¾ Maintaining global order is difficult, especially in a large project.
¾ Global order can fforce a client to g
grab a lock earlier than it
would like, tying up a resource for too long.
¾ Deadlock is a global property, but lock manipulation is local.
12
Deadlock Detection & Recovery
Recovering from deadlock
R1 R2 R3 R4
P1 P2 P3 P4 P5
Abort all deadlocked processes & reclaim their resources
Abort one process at a time until all cycles in the RAG
are eliminated
Where to start?
¾ Select low priority process
¾ Processes with most allocation of resources
Caveat: ensure that system is in consistent state (e.g., transactions)
Optimization:
¾ Checkpoint processes periodically; rollback processes to checkpointed state
13
Dealing With Deadlock
Deadlock avoidance
Examine each resource request and determine whether or not
granting the request can lead to deadlock
Define a set of vectors and matrices that characterize the
current state of all resources and processes
¾ resource allocation state matrix
Allocij = the number of units of R1 R2 R3 ... Rr
resource j held by process i P1 n1,1 n1,2 n1,3 ... n1,r
¾ maximum claim matrix
P2 n2,1 n2,2
Maxij = the maximum number of units P3 n3,1
...
31
of resource j that the process i will
...
...
ever require simultaneously
Pp np,1 ... np,r
¾ available vector
Availj = the number of units of
resource j that are unallocated <n1, n2, n3, ..., nr>
14
Dealing With Deadlock
Deadlock detection & recovery
What are some problems with the banker’s algorithm?
¾ Very slow O(n2m)
¾ Too slow to run on every allocation. What else can we do?
Deadlock prevention and avoidance:
¾ Develop and use resource allocation mechanisms and protocols that
prohibit deadlock
Deadlock detection and recovery:
y
¾ Let the system deadlock and then deal with it
Detect that a set of processes are deadlocked
Recover from the deadlock
15