1
Deadlocks
Thomas Plagemann
With slides from C. Griwodz, K. Li,A. Tanenbaum and M. van Steen
Preempting Scheduler Activations
Scheduler activations are completelypreemptable
Kernelspace
Userspace
2
Preempting Scheduler Activations
Maintaining the run queue needs to be a protected critical sectionLet’s use spin locks for protection
Thread 1 Upcall
Acquire spin lock
Start queue maintenance
Interrupt Thread 1
Acquire spin lock -> block
Resources
Examples of computer resourcesCPUMemoryDisk driveTape drivesPrintersPlotterLoudspeaker
3
ResourcesProcesses
Need access to resources in reasonable order
Typical way to use a resourceRequestUseRelease
Suppose a process holds resource A and requests resource B
At same time another process holds B and requests ABoth are blocked and remain so
ResourcesActive resource
Provides a serviceE.g. CPU, network adaptor
Passive resourceSystem capabilities that are required by active resourcesE.g. memory, network bandwidth
Exclusive resourceOnly one process at a time can use itE.g. loudspeaker, processor
Shared resourceCan be used by multiple processesE.g. memory, bandwidth
4
ResourcesSingle resource
Exists only once in the systemE.g. loudspeaker
Multiple resourceExists several time in the systemE.g. processor in a multiprocessor system
Preemptable resourceResource that can be taken away from a processE.g. CPU can be taken away from processes in user space
Non-preemptable resourceTaking it away will cause processes to failE.g. Disk, files
ResourcesProcess must wait if request is denied
Requesting process may be blockedMay fail with error code
DeadlocksOccur only whenprocesses are granted exclusive access to resources
blockacquire
use
acquire
use
5
DeadlocksFormal definition :
A set of processes is deadlockedif each process in the set is waiting for an eventthat only another process in the set can cause
Usually the event is release of a currently held resourceNone of the processes can …
RunRelease resourcesBe awakened
Four Conditions for Deadlock1. Mutual exclusion condition
Each resource assigned to 1 process or is available
2. Hold and wait conditionProcess holding resources can request additional
3. No preemption conditionPreviously granted resources cannot forcibly taken away
4. Circular wait conditionMust be a circular chain of 2 or more processesEach is waiting for resource held by next member of the chain
6
Deadlock ModelingModeled with directed graphs
Resource R assigned to process AProcess B is requesting/waiting for resource SProcess C and D are in deadlock over resources T and U
A
R B
S C
U D
T
Deadlock ExampleA utility program
Copies a file from a tape to diskPrints the file to a printer
ResourcesTapeDiskPrinter
A deadlock
tape disk printer
A
B
7
Deadlock ModelingHow deadlock occurs
ARequests RRequests SReleases SReleases R
BRequests SRequests TReleases TReleases S
CRequests TRequests RReleases RReleases T
A requests RB requests SC requests TA requests SB requests TC requests R
Resources
Processes A B C
R S T
Deadlock ModelingHow deadlock can be avoided
ARequests RRequests SReleases SReleases R
BRequests SRequests TReleases TReleases S
CRequests TRequests RReleases RReleases T
A requests RC requests TA requests SB requests SB requests TC requests RA releases SA releases RC releases RC releases T
Resources
Processes A B C
R S T
8
Deadlocks: Strategies
Ignore the problemIt is user’s fault
Detection and recoveryFix the problem afterwards
Dynamic avoidanceCareful allocation
PreventionNegate one of the four conditions
The Ostrich Algorithm
Pretend there is no problem
Reasonable if Deadlocks occur very rarely Cost of prevention is high
UNIX and Windows take this approachIt is a trade-off between
ConvenienceCorrectness
9
Deadlock Detection and RecoveryOne Resource of Each Type
A cycle can be found within the graph, denoting deadlock
A B
C
R
S TD E
F
G
U V
W
init notebook L
add current node CN to L
is CN two times in L?cycle detected
is an unmarkedoutgoing arc at CN?
follow the arcCN = next node
backtrackingCN = previous node
Deadlock Detection and RecoveryOne Resource of Each Type
10
Deadlock Detection and RecoveryMultiple Resources of Each Type
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
nmnnn
m
m
CCCC
CCCCCCCC
..................
...
...
321
2232221
1131211
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
nmnnn
m
m
RRRR
RRRRRRRR
..................
...
...
321
2232221
1131211
Existing resources Available resources
( )mEEEE ,...,,, 321 ( )mAAAA ,...,,, 321
Current allocation matrix Request matrix
Deadlock Detection and RecoveryMultiple Resources of Each Type
4 2 3 1 2 1 0 0
0 0 1 02 0 0 10 1 2 0
2 0 0 11 0 1 02 1 0 0
E=(
Current allocation matrix Request matrix
Tape d
rivers
Plotter
s
CD-Rom
s
Scann
ers
A=() )Tap
e driv
ers
Plotter
s
CD-Rom
s
Scann
ers
R=C=
11
Deadlock Detection and RecoveryRecovery
Recovery through preemptionTake a resource from some other processDepends on nature of the resource
Recovery through rollbackCheckpoint a process periodicallyUse this saved state Restart the process if it is found deadlocked
Recovery through killing processesCrudest but simplest way to break a deadlockKill one of the processes in the deadlock cycleThe other processes get its resources Choose process that can be rerun from the beginning
Deadlock AvoidanceResource Trajectories
finished
request releaserequest release
Printer
Plotter
A
Prin
ter
release
release
request
request
Plo
tter
B
start
Two processresource trajectories
12
Deadlock AvoidanceSafe and Unsafe States
state is safe
ABC
322
947
Free: 3
has maxABC
342
947
Free: 1
has maxABC
302
9
7Free: 5
has maxABC
307
9
7Free: 0
has maxABC
300
9
Free: 7
has max
Deadlock AvoidanceSafe and Unsafe States
state is safe
ABC
322
947
Free: 3
has maxABC
422
947
Free: 2
has maxABC
302
9
7Free: 4
has maxABC
442
947
Free: 0
has max
13
Deadlock AvoidanceBanker’s Algorithm for a Single Resource
Each process has a creditSystem knows how many resources a process requests at most before releasing resources
Total resources may not satisfy all creditsKeep track of resources assigned and neededCheck on each allocation whether it is safe
Safe: there exists a sequence of other states that all processes can terminate correctly
Deadlock AvoidanceBanker's Algorithm for a Single Resource
Free: 10
has max
0000
6547
Free: 2
has max
1124
6547
Free: 1
has max
1224
6547
650 50 6
40 470 7
42 4
ABCD
33551 561 6
74 7
ABCD
1224
ABCD
Free: 4Free: 5Free: 6Free: 2
Resource allocation state
14
Deadlock Detection and RecoveryBanker’s Algorithm for Multiple Resources
An example for the deadlockdetection algorithm
A 3 0 1B 0 1 0C 1 1 1
Assigned resources Resources still needed
6 3 4 25 3 2 2
E=(Tap
e driv
ers
Plotter
s
CD-Rom
s
Scann
ers
P=())
1 0 2 0A=( )100
D 1 1 0 1E 0 0 0 0
A 1 1 0B 0 1 1C 3 1 0
020
D 0 0 1 0E 2 1 1 0
Deadlock Detection and RecoveryBanker’s Algorithm for Multiple Resources
An example for the deadlockdetection algorithm
A 3 0 1B 0 1 0C 1 1 1
6 3 4 24 2 2 1
E=(Tap
e driv
ers
Plotter
s
CD-Rom
s
Scann
ers
P=())
2 1 2 1A=( )100
D 0 0 0 0E 0 0 0 0
A 1 1 0B 0 1 1C 3 1 0
020
D - - - -E 2 1 1 0
Assigned resources Resources still needed
15
Deadlock Detection and RecoveryBanker’s Algorithm for Multiple Resources
An example for the deadlockdetection algorithm
A 0 0 0B 0 1 0C 1 1 1
6 3 4 21 2 1 0
E=(Tap
e driv
ers
Plotter
s
CD-Rom
s
Scann
ers
P=())
5 1 3 2A=( )000
D 0 0 0 0E 0 0 0 0
A - - -B 0 1 1C 3 1 0
-20
D - - - -E 2 1 1 0
Assigned resources Resources still needed
Deadlock Detection and RecoveryBanker’s Algorithm for Multiple Resources
An example for the deadlockdetection algorithm
A 0 0 0B 0 0 0C 1 1 1
6 3 4 21 1 1 0
E=(Tap
e driv
ers
Plotter
s
CD-Rom
s
Scann
ers
P=())
5 2 3 2A=( )000
D 0 0 0 0E 0 0 0 0
A - - -B - - -C 3 1 0
--0
D - - - -E 2 1 1 0
Assigned resources Resources still needed
16
Deadlock Detection and RecoveryBanker’s Algorithm for Multiple Resources
An example for the deadlockdetection algorithm
A 0 0 0B 0 0 0C 0 0 0
6 3 4 20 0 0 0
E=(Tap
e driv
ers
Plotter
s
CD-Rom
s
Scann
ers
P=())
6 3 4 2A=( )000
D 0 0 0 0E 0 0 0 0
A - - -B - - -C - - -
---
D - - - -E 2 1 1 0
Assigned resources Resources still needed
Deadlock Detection and RecoveryBanker’s Algorithm for Multiple Resources
An example for the deadlockdetection algorithm
A 3 0 1B 0 1 0C 1 1 1
6 3 4 25 3 2 2
E=(Tap
e driv
ers
Plotter
s
CD-Rom
s
Scann
ers
P=())
1 0 2 0A=( )100
D 1 1 0 1E 0 0 0 0
A 1 1 0B 0 1 1C 3 1 0
020
D 0 0 1 0E 2 1 1 0
SAFE
Assigned resources Resources still needed
17
Deadlock AvoidancePractical Avoidance
Two Phase LockingPhase I
Process tries to lock all resources it needs, one at a timeIf needed resources found locked, start over(no real work done in phase one)
Phase IIRunReleasing locks
Note similarity to requesting all resources at onceAlgorithm works where programmer can arrange
Deadlock PreventionR: Conditions for Deadlock
1. Mutual exclusion conditionEach resource assigned to 1 process or is available
2. Hold and wait conditionProcess holding resources can request additional
3. No preemption conditionPreviously granted resources cannot forcibly taken away
4. Circular wait conditionMust be a circular chain of 2 or more processesEach is waiting for resource held by next member of the chain
18
Deadlock PreventionMutual Exclusion Condition
Some resources are not sharablePrinter, tape, etc
Some resources can be made sharableSome resources can be made virtual
Spooling - PrinterDoes spooling apply to all non-sharable resources?
Mixing - Soundcard
Principle:Avoid assigning resource when not absolutely necessaryA few processes as possible actually claim the resource
Deadlock PreventionHold and Wait Condition
Require processes to request resources before starting
A process never has to wait for what it needsTelephone companies do this
ProblemsMay not know required resources at start of runAlso ties up resources other processes could be using
Variation: Process must give up all resourcesThen request all immediately needed
19
Deadlock PreventionNo Preemption Condition
This is not a viable optionConsider a process given the printer
Halfway through its jobNo forcibly take away printer!!??
Deadlock PreventionCircular Wait Condition
A
1 5432
Normally ordered resourcesA resource graph
1. CD Rom drive2. Tape drive3. Plotter4. Scanner5. Imagesetter
20
Deadlock PreventionCircular Wait Condition
Impose an order of requests for all resourcesMethod
Assign a unique id to each resourceAll resource requests must be in an ascending order of the idsRelease resources in a descending order
Can you prove this method has no circular wait?Is this generally feasible?
Deadlock PreventionOverview
Order resources numericallyCircular waitTake resources awayNo preemptionRequest all resource initiallyHold and waitSpool everythingMutual exclusion
ApproachCondition
21
Non-resource Deadlocks
Possible for two processes to deadlockEach is waiting for the other to do some task
Can happen with semaphoresEach process required to do a down() on two semaphores (mutex and another)If done in wrong order, deadlock results
Preempting Scheduler Activations
So how do they handle this deadlock?
Thread 1 Upcall
Acquire spin lock
Start queue maintenance
Interrupt Thread 1
Acquire spin lock -> block
22
Preempting Scheduler ActivationsDetection and recovery (like in Mach)Basic idea:
Upcall handler checks first the state of each interruptedthreadIf it is in a critical section allow it to finish this
Implementation:Protect critical sections with spin locksAcquire_spin_lock() increments a counter in thethread’s descriptorUpcall handler checks spin lock count of all interruptedthreadsIf there are threads that hold spin locks set flagSwitch to the context of these threadsAfterwards switch back to upcall handler
Summary
ResourceIntroduction to deadlocksStrategies
Ostrich algorithmDeadlock detection and recoveryDeadlock avoidanceDeadlock prevention
Non-resource deadlocks