Deadlock
28 September, 2010
1
semaphore S = T = 1
Process 1 Process 2
: :
down(S) down(T)
down(T) down(S)
up(T) up(S)
up(S) up(T)
2
: :
down(S)
down(T)
down(T)
down(S)
3
while (1)
think
wait till left chopstick is available
pick up left chopstick
wait till right chopstick is available
pick up right chopstick
eat
put down left chopstick
put down right chopstick
4
while (1)
think
down(chopstick[i])
down(chopstick[(i+1)%N]
eat
up(chopstick[i])
up(chopstick[(i+1)%N]
5
6
Resource
7
acquire resource
(wait until available)
use resource
release resource
8
software
vs.
hardware
resource
9
preemptive
vs.
non-preemptive
resource
10
single copy
vs.
multiple copies
of a resource
11
4
conditions
for deadlock
12
mutual exclusion
each resource must be either
assigned to exactly one process
or is available
13
while (1)
think
down(chopstick[i])
down(chopstick[(i+1)%N]
eat
up(chopstick[i])
up(chopstick[(i+1)%N]
14
hold and wait
processes holding resources granted
earlier can request for new resource
15
while (1)
think
down(chopstick[i])
down(chopstick[(i+1)%N]
eat
up(chopstick[i])
up(chopstick[(i+1)%N]
16
no preemption
resources granted cannot be
forcefully taken away
17
while (1)
think
down(chopstick[i])
down(chopstick[(i+1)%N]
eat
up(chopstick[i])
up(chopstick[(i+1)%N]
18
circular waiting
a circular chain of processes, each waiting for a
resource held by the next member of the chain
19
20
http://library.thinkquest.org/08aug/01033/img.htm
21
Deadlock
Modeling
22
23
A B C
A requests R
B requests S
C requests T
A requests S
B requests T
C requests R
R S T
24
Deadlock
Detection
is the system deadlocked, and if so,
which process are involved?
25
Deadlock Detection
(if each resource
type has one copy)
26
1. !periodically build a ! ! !
! ! resource graph
27
Deadlock Detection
(if each resource
type has multiple copies)
28
resources in existence resources available
4 2 3 1 2 1 0 0
R S T U R S T U
29
A B C
R S T U
30
is there a process
whose requests can
be satisfied?
31
A
R S T U
32
B
R S T U
33
C
R S T U
34
C
R S T U
35
resources in existence resources available
4 2 3 1 2 1 0 0
R S T U R S T U
36
A B
R S T U
37
resources in existence resources available
4 2 3 1 2 2 2 0
R S T U R S T U
38
resources in existence resources available
4 2 3 1 4 2 2 1
R S T U R S T U
39
suppose we have
deadlock,
now what?
40
1. preempt
2. rollback
3. terminate
41
Deadlock
Avoidance
if we know the resources required by a process,
can we avoid deadlock by careful allocation?
42
Deadlock
Prevention
can we set some rules that prevent deadlock?
43
mutual exclusion
each resource must be either
assigned to exactly one process
or is available
44
allow sharing of
resources
45
46
hold and wait
processes holding resources granted
earlier can request for new resource
47
allocate only if all
resources are available
48
while (1)
think
down(mutex)
state[ i ] = HUNGRY
test( i )
up(mutex)
down(semaphore[ i ])
eat
down(mutex)
state[ i ] = THINK
test( L )
test( R )
up(mutex)
49
no preemption
resources granted cannot be
forcefully taken away
50
allow resources to be
preempted
51
52
circular waiting
a circular chain of processes, each waiting for a
resource held by the next member of the chain
53
order resource
numerically and
acquire in order
54
6
1
2
5
3 4
55
Livelock
56
Starvation
57
Mars Pathfinder
58
Process A (low): Process B (high):
down(mutex) down(mutex)
: :
work important tasks
: :
: :
up(mutex) up(mutex)
59
Priority Inversion
60