Lecture #7: Deadlock
These topics are from Chapters 3 (Deadlock) in Advanced
Concepts in OS.
Types of Resources
- reusable
- fixed number of units
- neither created nor destroyed
- request --> use --> release --> repeat
- consumable
- produced by a producer process
- request, produce/acquire (implies consume)
What are examples of each type of resource?
When is a set of processes deadlocked?
- resource deadlock: each process requests resources
held by another process in the the set and it must receive all
the requested resources before it can become unblocked
- communication deadlock: each process is waiting for communication
from another process, and will not communicate until it receives
the communication for which it is waiting
What are the fundamental causes of deadlock?
How can we deal with deadlock?
Fundamental Causes of Deadlock
- exclusive access
- wait while holding
- no preemption
- circular wait
All these conditions are necessary for deadlock to occur.
Hence, by preventing any one of these we prevent deadlock.
Deadlock Handling Strategies
- prevention
- avoidance
- detection
What is the difference between prevention and avoidance?
What are examples of deadlock prevention?
Deadlock Avoidance
Process P1:
request(R1); ... request(R2); ... release(R2); release(R1);
Process P2:
request(R2); ... request(R1); ... release(R1); release(R2);
The Banker's Algorithm is an example of a well
known deadlock avoidance algorithm
Deadlock Prevention
- Ordered allocation
Process P1:
request(R1); ... request(R2); ... release(R2); ... release(R1);
Process P2:
request(R1); ... request(R2); ... release(R2); ... release(R1);
- Release before request
Process P1:
request(R1); ... release (R1); request(R2); ... release(R2);
Process P2:
request(R2); ... release (R2); request(R1); ... release(R1);
- Request all at once
Process P1:
request(R1, R2); ... release(R1, R2);
Process P2:
request(R1, R2); ... release(R1, R2);
Wait-For Graphs (WFG)
Nodes correspond to processes (only).
There is an edge from process P1 to process P2 iff P1
is blocked waiting for P2 to release some resource.
Single-Unit Resource Allocation Graphs
Nodes correspond to processes and resources.
There is a request edge from process P to resource
R iff P is blocked waiting for an allocation of R.
There is an assignment edge from resource R to
process P iff P is holding an allocation of R.
How do we model multi-unit resources?
Multiunit Resource Allocation Graphs
General Resource System
A system state consists of:
- set of processes P = {P1, P2, ¼Pn}
- set of resources G = {R1, R2, ¼Rm}
- for every reusable resource Ri, a nonegative integer
ti,
the total number of units present in the system
- for every consumable resource Ri,
a nonempty set of producers of Ri
General Resource Graph
Bipartite directed graph
- nodes are P and G
- (r1, r2, ¼rm) = available units vector
- (P,R) is a request edge
- (R,P) is an assignment edge if R is reusable
- (R,P) is a producer edge if R is consumable
What is a bipartite graph?
Give examples of graphs, operations on graphs, deadlock,
and safe/unsafe states.
Request Models
- single-unit
- AND request model
- OR request model
- AND-OR request model
- P-out-of-Q request model
What is a sufficient condition for deadlock in each
of these models?
Deadlock Conditions in WFG
Cycle is always a necessary condition for deadlock
- single-unit -- cycle is sufficient
- AND request model -- cycle is sufficient
- OR request model -- knot is sufficient
- AND-OR request model -- knot is sufficient
- P-out-of-Q request model -- knot is sufficient
A cycle is not a sufficient condition for deadlock in
multiunit models.
What is a Knot?
A strongly connected subgraph of a
directed graph, such that starting from any node in the subset it
is impossible to leave the knot by following the edges of the
graph.
A cycle with no deadlock, and no knot.
A knot is a sufficient condition for deadlock
A knot is not a necessary condition for deadlock
Conditions on General Resource Graph
- for every reusable resource Ri
- #(Ri,*) £ ti
- ri = ti - #(Ri,*)
- "Pj : Pj Î P :: #(Ri,Pj) + #(Pj,Ri) £ ti
- for every consumable resource Ri
- ri ³ 0
- $ edge (Ri, Pj) Û Pj is a producer of Ri
What is the relationship between a system state and a GRG?
Can you describe an algorithm for detecting a deadlock?
Operations on General Resource Graph
- request: add request edges
(requires P was not blocked)
- grant:
replace reusable request edges by assignment edges, and decrement ri
(requires the resource was available)
- produce: delete consumable request edges
(requires the producer was not blocked)
- release: delete assignment edges, and increment ri
(requires P was not blocked)
Graph Reduction
Models a most favorable possible (optimistic) continuation
of execution from the current system state.
while there is an unblocked process Pi:
- For each request edge (Pi, Rj), remove the
request edge and decrement the corresponding count rj.
This corresponds to satisfying the request.
- For each allocation edge (Rj, Pi), remove the
allocation edge and increment the corresponding count rj.
This corresponds to releasing the allocation.
- For each consumable resource production edge (Pi, Rj),
remove the producer edge from the graph,
and set rj to infinity. This corresponds to
assuming Pi produces as many units of Rj as may
be required by other processes.
There is deadlock iff the remaining graph is non-null
Practicality of GRG Reduction
- Useful for characterizing properties of deadlock systems
- Not practical for implementation
- Reducibility is dependent on the order of the reductions
- Worst time complexity is O(mn) where m is number of resources
and $n$ is number of processes
Expedient States
A state is expedient iff all processes having
outstanding requests are blocked, i.e. all grantable
requests have been granted.
Note: A system must eventually get to one of these states if it
is deadlocked.
Theorem In a GRG,
- A cycle is a necessary condition for deadlock
- If the graph is expedient, then a knot is a sufficient
condition for deadlock
This still leaves a gap. That is,
the existence of a cycle is necessary but not sufficient,
and the existence of a knot is sufficient but not necessary.