Deadlocks
Deadlocks
P0 P1
wait (A); wait(B)
wait (B); wait(A)
Bridge Crossing Example
● Pi requests instance of Rj
P
i
R
● Pi is holding an instance of Rj j
P
i R
j
Resource Allocation Graph With A Deadlock
Before P3 requested an After P 3 requested an
instance of R2 instance of R2
Graph With A Cycle But No Deadlock
● Avoidance
● Ensure that the system will never enter an unsafe state
● Detection
● Allow the system to enter a deadlock state and then recover
● Do Nothing
● Ignore the problem and let the user or system administrator respond to the
problem; used by most operating systems, including Windows and UNIX
Deadlock Prevention
To prevent deadlock, we can restrain the ways that a request can be
made
● Mutual Exclusion – The mutual-exclusion condition must
hold for non-sharable resources
F(tape drive) = 1
F(disk drive) = 5
F(printer) = 12
Deadlock Avoidance
Requires that the system has some additional a priori
information
available.
● Simplest and most useful model requires that each process
declare the maximum number of resources of each type that it
may need
Assignme Reque
nt st
edge edge
Claim
Clai edge
m
edge
Unsafe State In Resource-Allocation Graph
Assignme Reque
nt st
edge edge
Assignme
Clai nt
m edge
edge
Resource-Allocation Graph Algorithm
● When a process gets all its resources, it must return them in a finite
amount of time
Data Structures for the Banker’s Algorithm
Required data
structures:
● When, and how often, to invoke the detection algorithm depends on:
● How often is a deadlock likely to occur?
● How many processes will be affected by deadlock when it happens?
● Two Approaches
● Process termination
● Resource preemption
Recovery from Deadlock: Process Termination
● Abort all deadlocked processes
● This approach will break the deadlock, but at great expense
● Starvation – How do we ensure that starvation will not occur? That is,
how can we guarantee that resources will not always be preempted
from the same process?
Summary