Professional Documents
Culture Documents
=
mn m m
n
n
c c c
c c c
c c c
C
1 1
2 22 21
1 12 11
A matrix containing the # of resources of
Type n claimed by process m.
(
(
(
(
=
mn m m
n
n
r r r
r r r
r r r
R
1 1
2 22 21
1 12 11
A matrix containing the # of resources of
Type n requested by process m.
We see that:
j j
m
i
ij
e a c = +
=1
16
For two vectors, X and Y
Deadlock Detection
- Many resources of each type
Y X s iff
i i
y x s
for m i s s 0
For example:
| | | |
| | | | 4 2 3 2 4 3 2 1
4 4 3 2 4 3 2 1
s
s
17
The detection algorithm is applied periodically
1. Find an unmarked process P
i
for which R
i
A.
2. If such a process is found, add C
i
to A. Mark the process
and go to step 1.
3. If no such process is found, terminate.
All unmarked processes are deadlocked
Deadlock Detection
- Many resources of each type
s
How on earth does this algorithm work?!?!?
18
Exercise:
Deadlock Detection
- Many resources of each type
| | 1 3 2 4 = E | | 0 0 1 2 = A
(
(
(
=
0 2 1 0
1 0 0 2
0 1 0 0
C
Is there deadlock in the system??
(
(
(
=
0 0 1 2
0 1 0 1
1 0 0 2
R
1
| | 0 2 2 2 ' = A
2
| | 1 2 2 4 ' ' = A
3
| | 1 3 2 4 ' ' ' = A
NO!!!
19
Deadlock Detection
How to recover from deadlock
Preemption
Process rollback
Kill one of the deadlocked processs
20
Deadlock Avoidance
Avoids deadlock by cautious allocations
Safe and unsafe states
An allocation can only be granted if it puts
the system in a safe state.
21
Deadlock Avoidance
- Safe and unsafe states
A
B
Safe Safe
Safe
Safe
Safe
Unsafe
p q
r s
Both
Finished
I8
I7
I6
I5
I4 I3 I2 I1
Pri
nte
r
Plo
tter
Plotter
Printer
Unreach-
able
22
Deadlock Avoidance
- Safe and unsafe states
Algorithms to determine the state
Based on the E,A,C and R matrices.
Example:
A total of 10 resources, with the following allocations:
Proc Has Max
A 3 9
B 2 4
C 2 7
Is the system in a safe or unsafe state??
Free 3
1
5
2
7
3
10
SAFE!!!
23
Deadlock Avoidance
-Bankers Algorithm
Made by Dijkstra
Originating from loan allocations in a bank
Examines if an allocation leads to a safe or
unsafe state
Is run when resource is requested
Two cases
One type of resource
Multiple types of resources
24
Deadlock Avoidance
-Bankers Algorithm
Example using one type of resource:
7 2 C
4 2 B
9 3 A
Max Has Proc
Free 3
Initial state
SAFE!
A requests 1
7 2 C
4 2 B
9 4 A
Max Has Proc
Free 2
UNSAFE!
B requests 1
7 2 C
4 3 B
9 3 A
Max Has Proc
Free 2 SAFE!
C requests 1
7 3 C
4 3 B
9 3 A
Max Has Proc
Free 1 SAFE!
25
Deadlock Prevention
Very difficult to achieve
Attack one of the conditions for deadlock
Attack Mutual Exclusion
Attack Hold and Wait
Attack No preemption
Attack Circular wait