Cs2106 Lec7 Deadlock
Transcript of Cs2106 Lec7 Deadlock
-
8/8/2019 Cs2106 Lec7 Deadlock
1/60
Lecture 7
Deadlock
28 September, 2010
1
-
8/8/2019 Cs2106 Lec7 Deadlock
2/60
Process 2
:down(T)
down(S)
up(S)
up(T)
semaphore S = T = 1
Process 1
:down(S)
down(T)
up(T)
up(S)
2
-
8/8/2019 Cs2106 Lec7 Deadlock
3/60
-
8/8/2019 Cs2106 Lec7 Deadlock
4/60
while (1)
thinkwait till left chopstick is available
pick up left chopstick
wait till right chopstick is availablepick up right chopstick
eatput down left chopstick
put down right chopstick4
-
8/8/2019 Cs2106 Lec7 Deadlock
5/60
while (1)
thinkdown(chopstick[i])
down(chopstick[(i+1)%N]eat
up(chopstick[i])
up(chopstick[(i+1)%N]
5
-
8/8/2019 Cs2106 Lec7 Deadlock
6/60
6
-
8/8/2019 Cs2106 Lec7 Deadlock
7/60
Resource
7
-
8/8/2019 Cs2106 Lec7 Deadlock
8/60
acquire resource
(wait until available)use resource
release resource
8
-
8/8/2019 Cs2106 Lec7 Deadlock
9/60
software
vs.hardware
resource
9
-
8/8/2019 Cs2106 Lec7 Deadlock
10/60
preemptive
vs.non-preemptive
resource
10
-
8/8/2019 Cs2106 Lec7 Deadlock
11/60
single copy
vs.multiple copies
of a resource
11
-
8/8/2019 Cs2106 Lec7 Deadlock
12/60
4conditions
for deadlock
12
-
8/8/2019 Cs2106 Lec7 Deadlock
13/60
mutual exclusioneach resource must be eitherassigned to exactly one process
or is available
13
-
8/8/2019 Cs2106 Lec7 Deadlock
14/60
while (1)
thinkdown(chopstick[i])
down(chopstick[(i+1)%N]eat
up(chopstick[i])
up(chopstick[(i+1)%N]
14
-
8/8/2019 Cs2106 Lec7 Deadlock
15/60
hold and waitprocesses holding resources grantedearlier can request for new resource
15
-
8/8/2019 Cs2106 Lec7 Deadlock
16/60
while (1)
thinkdown(chopstick[i])
down(chopstick[(i+1)%N]eat
up(chopstick[i])
up(chopstick[(i+1)%N]
16
-
8/8/2019 Cs2106 Lec7 Deadlock
17/60
no preemptionresources granted cannot beforcefully taken away
17
-
8/8/2019 Cs2106 Lec7 Deadlock
18/60
while (1)
thinkdown(chopstick[i])
down(chopstick[(i+1)%N]eat
up(chopstick[i])
up(chopstick[(i+1)%N]
18
-
8/8/2019 Cs2106 Lec7 Deadlock
19/60
circular waitinga circular chain of processes, each waiting for aresource held by the next member of the chain
19
-
8/8/2019 Cs2106 Lec7 Deadlock
20/6020
-
8/8/2019 Cs2106 Lec7 Deadlock
21/60
http://library.thinkquest.org/08aug/01033/img.htm
21
http://library.thinkquest.org/08aug/01033/img.htmhttp://library.thinkquest.org/08aug/01033/img.htm -
8/8/2019 Cs2106 Lec7 Deadlock
22/60
-
8/8/2019 Cs2106 Lec7 Deadlock
23/6023
-
8/8/2019 Cs2106 Lec7 Deadlock
24/60
A
R
C
T
B
S
A requests RB requests S
C requests TA requests SB requests T
C requests R
24
-
8/8/2019 Cs2106 Lec7 Deadlock
25/60
Deadlock
Detectionis the system deadlocked, and if so,
which process are involved?
25
-
8/8/2019 Cs2106 Lec7 Deadlock
26/60
-
8/8/2019 Cs2106 Lec7 Deadlock
27/60
1. !periodically build a ! ! ! ! ! resource graph2. !run depth first search on! ! the graph to detect cycle
27
-
8/8/2019 Cs2106 Lec7 Deadlock
28/60
Deadlock Detection(if each resource
type has multiple copies)
28
-
8/8/2019 Cs2106 Lec7 Deadlock
29/60
0 0 1 0
2 0 0 1
0 1 2 0
4 2 3 1 2 1 0 0
resources in existence resources available
allocation matrix request matrix
2 0 0 1
1 0 1 0
2 1 0 0
A
B
C
A
B
C
R S T UR S T U
R S T UR S T U
29
-
8/8/2019 Cs2106 Lec7 Deadlock
30/60
A CB
R TS U
30
-
8/8/2019 Cs2106 Lec7 Deadlock
31/60
is there a process
whose requests can
be satisfied?
31
-
8/8/2019 Cs2106 Lec7 Deadlock
32/60
A
R TS U
32
-
8/8/2019 Cs2106 Lec7 Deadlock
33/60
B
R TS U
33
-
8/8/2019 Cs2106 Lec7 Deadlock
34/60
C
R TS U
34
-
8/8/2019 Cs2106 Lec7 Deadlock
35/60
C
R TS U
35
-
8/8/2019 Cs2106 Lec7 Deadlock
36/60
0 0 1 0
2 0 0 1
0 1 2 0
4 2 3 1 2 1 0 0
resources in existence resources available
allocation matrix request matrix
2 0 0 1
1 0 1 0
2 1 0 0
A
B
C
A
B
C
R S T UR S T U
R S T UR S T U
36
-
8/8/2019 Cs2106 Lec7 Deadlock
37/60
A B
R TS U
37
-
8/8/2019 Cs2106 Lec7 Deadlock
38/60
0 0 1 0
2 0 0 1
0 0 0 0
4 2 3 1 2 2 2 0
resources in existence resources available
allocation matrix request matrix
2 0 0 1
1 0 1 0
0 0 0 0
A
B
C
A
B
C
R S T UR S T U
R S T UR S T U
38
-
8/8/2019 Cs2106 Lec7 Deadlock
39/60
0 0 1 0
0 0 0 0
0 0 0 0
4 2 3 1 4 2 2 1
resources in existence resources available
allocation matrix request matrix
2 0 0 1
0 0 0 0
0 0 0 0
A
B
C
A
B
C
R S T UR S T U
R S T UR S T U
39
-
8/8/2019 Cs2106 Lec7 Deadlock
40/60
suppose we have
deadlock,
now what?
40
-
8/8/2019 Cs2106 Lec7 Deadlock
41/60
1. preempt
2. rollback
3. terminate
41
-
8/8/2019 Cs2106 Lec7 Deadlock
42/60
Deadlock
Avoidanceif we know the resources required by a process,
can we avoid deadlock by careful allocation?
42
-
8/8/2019 Cs2106 Lec7 Deadlock
43/60
-
8/8/2019 Cs2106 Lec7 Deadlock
44/60
mutual exclusioneach resource must be either
assigned to exactly one process
or is available
44
-
8/8/2019 Cs2106 Lec7 Deadlock
45/60
allow sharing ofresources
45
-
8/8/2019 Cs2106 Lec7 Deadlock
46/60
46
-
8/8/2019 Cs2106 Lec7 Deadlock
47/60
hold and waitprocesses holding resources grantedearlier can request for new resource
47
-
8/8/2019 Cs2106 Lec7 Deadlock
48/60
allocate only if allresources are available
48
-
8/8/2019 Cs2106 Lec7 Deadlock
49/60
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
-
8/8/2019 Cs2106 Lec7 Deadlock
50/60
no preemptionresources granted cannot beforcefully taken away
50
-
8/8/2019 Cs2106 Lec7 Deadlock
51/60
allow resources to bepreempted
51
-
8/8/2019 Cs2106 Lec7 Deadlock
52/60
52
-
8/8/2019 Cs2106 Lec7 Deadlock
53/60
circular waitinga circular chain of processes, each waiting for aresource held by the next member of the chain
53
-
8/8/2019 Cs2106 Lec7 Deadlock
54/60
order resource
numerically and
acquire in order
54
-
8/8/2019 Cs2106 Lec7 Deadlock
55/60
1
2
3 4
5
6
55
-
8/8/2019 Cs2106 Lec7 Deadlock
56/60
Livelock
56
-
8/8/2019 Cs2106 Lec7 Deadlock
57/60
Starvation
57
-
8/8/2019 Cs2106 Lec7 Deadlock
58/60
Mars Pathfinder58
http://library.thinkquest.org/08aug/01033/img.htmhttp://library.thinkquest.org/08aug/01033/img.htmhttp://library.thinkquest.org/08aug/01033/img.htm -
8/8/2019 Cs2106 Lec7 Deadlock
59/60
Process A (low):
down(mutex)
:
work:
:
up(mutex)
Process B (high):
down(mutex)
:
important tasks:
:
up(mutex)
59
-
8/8/2019 Cs2106 Lec7 Deadlock
60/60
Priority Inversion