Deadlock
Deadlock
Process deadlock
deadlock prevention deadlock avoidance deadlock detection recovery from deadlock
Process deadlock
in general, can partition system resources into equivalence types or classes
e.g, CPUs, RAM, files, printers, tape drives,
when a process requests a resource from a particular resource class, any available resource in the class is allocated to the process
generally, resources go through the sequence: 1. request 2. use 3. release
deadlock occurs when a set of processes is waiting for resources that are held by other processes in the set
2
Deadlock examples
real-world example: single-lane bridge
each section of a bridge can be viewed as a resource if a deadlock occurs, it can be resolved if one car backs up (preempt & rollback) several cars may have to be backed up if a deadlock occurs starvation is possible
P1 and P2 each hold one tape drive and each needs another one
Deadlock characterization
deadlock can arise if four conditions hold simultaneously:
Mutual Exclusion: only one process at a time can use a resource. Hold and Wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No Preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular Wait: there exists a set {P0, P1, , Pn} of waiting processes such that: P0 is waiting for a resource held by P1 P1 is waiting for a resource held by P2 Pn is waiting for a resource held by P0
in this example: P1 has R2, has requested R1 P2 has R1 & R2, has requested R3 P3 has R3
cycle = deadlock?
If graph contains no cycles If graph contains a cycle no deadlock
if only one instance per resource type, then deadlock. if several instances per resource type, deadlock is possible
no deadlock
deadlock
no deadlock
allow deadlocks to occur system implements an algorithm for deadlock detection, if so then recover
Ignorance:
assume/pretend deadlocks never occur in the system used by most operating systems, including UNIX & Windows
7
Deadlock prevention
to prevent deadlock, must eliminate one of the four necessary conditions
Mutual Exclusion: can't eliminate since some resources are not shareable Hold and Wait: options exist, but wasteful and may lead to starvation 1. grant all resources when process starts, or 2. allocate and deallocate resources in complete groups No Preemption: options exist, but taking away resources can be tricky Circular Wait: most likely candidate impose an ordering on resources [R0, R1, , Rn], and require that each process requests resources in increasing order e.g., P0 wants R0, R1, and R2 P1 wants R1, R2, and R3
Deadlock avoidance
if have some knowledge of future resource demands (e.g., max number of resources needed per process), can avoid deadlock situations
safe state: a state is safe if the system can allocate resources to each process (up to its maximum need) in some sequence and still avoid a deadlock safe sequence: a sequence of processes {P0, P1, , Pn} is safe for the current allocation state if, for each Pi, the resources that Pi can still request are either free or else held by {P0, P1, , Pi-1} a safe sequence implies that each process need only wait for lower numbered processes to finish before it can finish
10
11
Banker's algorithm
the banker's algorithm can avoid deadlocks even with multiple instances
looks at each request for resources and tests if the request moves the system into an unsafe state if the system is still safe, then the request is granted if the system would become unsafe, then the request is denied
13
Max A B C P 2 1 0 Q 3 1 1 R 5 1 2
Allocation A B C 0 1 0 2 0 0 2 0 1
Available A B C 2 1 1
Need A B C 2 0 0 1 1 1 3 1 1
P0 P1 P2 P3 P4
the system is in a safe state since the sequence P1, P3, P4, P2, P0 satisfies the safety criteria
16
P1 requests [1 0 2]
1. Request[i] Need[i]: [1 0 2] [1 2 2], so no ERROR 2. Request[i] Available[i]: [1 0 2] [3 3 2], so resources are available 3. pretend to allocate the resources Allocation A B C 0 1 0 3 2 2 3 0 2 2 1 1 0 0 2 Available A B C 2 1 0 Need A B C 7 4 3 0 0 0 6 0 0 0 1 1 4 3 1
P0 P1 P2 P3 P4
Max A B C 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3
the sequence P1, P3, P4, P0, P2 satisfies the safety 17 requirement
P0 P1 P2 P3 P4
P4 requests [3 3 0] ? P0 requests [0 2 0] ?
18
Deadlock detection
if there is only a single instance of each resource, can construct a wait-for graph out of the resource allocation graph
remove all resource nodes and collapse the edges
wait-for graph
a deadlock has occurred if and only if there is a cycle in the wait-for graph
to detect deadlock, need to periodically check for cycles can be accomplished in O(n2) operations, where n is number of processes
19
not deadlocked P0, P2, P3, P1, P4 results in Finish[i] == true for all i
20
deadlock! can reclaim P0's resources, but not enough to meet requests of other processes
21
testing at every resource request would greatly slow down all requests
if deadlocks are rare or only affect a small number of processes, then may resort to testing periodically (e.g., once a day, when CPU use < 40%)
note that if testing is sporadic, there may be many cycles in the resource graph will not be able to tell which of the deadlocked processes caused the deadlock
22
Deadlock recovery
to recover from deadlock, must eliminate one of the necessary conditions
this involves preempting a resource or aborting a deadlocked process
preempting a resource
How do you select a victim? When preempt a resource, how do you roll back the process? How do you prevent starvation?
aborting a process
Do you abort all deadlocked processes or one at a time?
allowing the use of the optimal approach for each class of resources
23