Title: Deadlocks
1 2fig_13_00
Example Tasks T0 and T1 must share resources R1
and R2 They will communicate with semaphores S0
and S1 Instruction flow T0 sets S0 1 and
waits for R1 T0 sets S1 1 and waits for R2 T1
sets S1 S1 1 2 and waits for R2 T1 sets S0
S0 1 2 and waits for R1 No way for either
task to make progress deadlock
3fig_13_00
Example system similar (identical copies) and
dissimilar resources to be shared
fig_13_00
4fig_13_00
- Necessary conditions for deadlock
- --All must hold simultaneously
- --These conditions are not sufficient and not
independent - Mutual exclusionif process gets control of a
resource no other process can use it - Hold and waitthere are processes holding
resources and requesting resources held by other
processer - No preemptionresources cannot be preempted by
another process - Circular waitthere is a set of processes P0, P1,
Pn where - Pi is waiting for a resource held by Pi1, i
0,,n-1 and - Pn is waiting for a resource held by P0
5fig_13_01
Deadlock analysis tool resource allocation
graph Example suppose we have the following
system of tasks and resource, where dots indicate
copies of a type of resource Waiting
for resource Holding resource
fig_13_01
6fig_13_02
Example state --if no cycles, there is no
deadlock --if there is a cycle, there is a
potential for deadlock
fig_13_02
7fig_13_03
Example cycle (T1,R1,T2,R5) and deadlock
fig_13_03
8fig_13_04
Example cycle (T1,R1,T2,R5) but no deadlock
fig_13_04
9fig_13_05
Deadlock prevention Can prevent one of
necessary conditions Mutual exclusioncannot
guarantee this wont occur, some resources are
not sharable Hold and wait can require task get
all resources before executingmay be
inefficient Can require running task give up
resources before getting a new onemay lead to
starvation of low priority tasks No
preemption Can allow preemption similar problems
to Hold and wait No circular wait Can put a
linear order on the resources and use this to
constrain task requests
10fig_13_05
Deadlock avoidance Algorithms need more
information about how tasks use
resources Example two tasks share two resources
but task scheduling guarantees that they will not
both request the same resource during a given
interval
fig_13_05
11fig_13_06
Example graph-based algorithm using claim and
request edges Claim edge task may request
resource, calculated at beginning Request edge
actual request, convert a claim edge Example T1
has a claim on R1 but cannot request it at this
time because a deadlock will occur
fig_13_06
12fig_13_07
Deadlock Avoidance Bankers algorithm Each task
must state total resources it needs when it
enters banker uses this information to
construct a schedule ensuring that an unsafe
state will never be entered
fig_13_07
13fig_13_08
Example A/D converter10 channels
fig_13_08
14fig_13_09
A safe state 2 channels are not
allocated Possible schedule T0 gets 2 free
channels, completes Then T2 and finally T1 can
complete
fig_13_09
15fig_13_10
Poor allocation Let T1 go first it will be
blocked eventually and deadlock will occur
fig_13_10
16fig_13_11
Deadlock recovery --Task terminationsimple but
may be too severe --Resource preemption Must
take into consideration --which task to
preempt --rollback considerations --starvation
fig_13_11