Professional Documents
Culture Documents
OS Ch07 Deadlocks PDF
OS Ch07 Deadlocks PDF
OS Ch07 Deadlocks PDF
Chapter 7:
Deadlocks
IPERATIN
SYSTEM
~ COHCEPTS
Operating Systems 11.-. . . . H• ■"CMII
~lbftt ,a'l"n
~
,._.,...., ~ ,,inabi.. " ~ iii . .
Cl- .~ - --
1
Objectives
r,
\.J./
- ---
•!• To develop a description of deadlocks, which prevent sets of
concurrent processes from completing their tasks
■ The Deadlock Problem
■ The Conditions for Deadlock
'
-,
Operating Systems - Dr. Abdulbaset Gaddah 2
r,
Deadlock
\ ...L.J
•!• The permanent blocking of a set of processes that either compet e
for system resources or comm1unicate with each other
•!• A set of processes is deadlocked when each process in the set is
holding a resource and waiting to acquire a resource held by another
process in the set
1
GD
m
Traffic only in one direction Each section of a bridge can
•!• Actual Deadlock be viewed as a resource
GD GD
(D -------------------------------
IE ~ ~ - -
A deadlock can be resolved if one car backs up {preempt resources and rollback)
Several cars may 17ave to be backed up
Starvation is possible
r,
Resource Categories
\ ..JI
•!• Two general categories of resou1rces: reusable and consumable
•!• A reusable resource - can be safely used by only one process at a
time and is not depleted by that use
■ Examples: processors; 1/0 channels; main and secondary memory;
devices; and data structures such as files, databases, and semaphores
'
-,
Operating Systems - Dr. .Abdulbaset Gaddah 6
P1 P2
• • • • ••
•• • • ••
r...... ../
r,
Deadllock - Example 2
~~ -----
♦:♦ Two processes (P and Q) compete for exclusive access to a d1isk file
(D) and a tape drive (T)
Process P Process Q
'
-,
Operating Systems - Dr. .Abdulbaset Gaddah 8
r-,.,
P1 P2
..' " '
r,
System Model
\.., (.,/
r
Operating Systems - Dr. Abdulbaset Gaddah 10
10
•!• The above conditions are necessary but not sufficient for a deadlock
to exist. For deadlock to take place, a 4th condition is requ ired:
■ Circular wait: a closed chain of processes exists, such that each process
holds at least one resource needed by the next process in the chain
• The 4th condition is a potential consequence of the first three conditions
• Deadlock occurs if and only if the circular wait condition is unresolvable
• The circular wait condition is unresolvable if the first three conditions hold
• Thus, the four conditions, taken together, constitute necessary and sufficient
conditions for deadlock
r
Operating Systems - Dr. Abdulbaset Gaddah
11
r,
Resource Allocation Graph (RAG)
\ ..JI
•!• RAG is a useful tool in cha;racterizing the allocation of resources to
processes
•!• It is a directed graph that represents a state of the system
•!• A set of vertices V and a set of edges E
■ V is partitioned into two types of nodes:
• P = { P1, P2, ... , ~ -}, all the processes in th•e system
• R = { R1, R2, ... , ~ ·}, all resource types in the system
■ Request edge - Pi ➔ Rj
■ Assignment edge - Rf ➔ Pi
Pi
•• Rj Pi ~◄---~1...: R" Pi ----►
: :e • Rj
• ' I• 1
•
Resource type p; is boldjng an inst.,nce of Rj Pi requests an instance of Rj
\Vith 3 instances
'
-,
Operating Systems - Dr. Abdulbaset Gaddah 12
12
Jll RI
Pl Circular P2 Pl Circular P2
wait
Deadlock . rodeadlock
13
RAG - Example 2
14
Pl P? P3
•!• Two cycles exist in the system
■ Pl, P2, and P3 are deadlocked
■ P2 is waiting for R3, which is held by P3 •••
■ P3 is waiting for Pl or P2 to release R2 R2 R4
15
RAG - Example 4
•!• The sets P, R, and E:
■ P = {Pl, P2, P3, P4}
■ R :..:. {Rt , R2} RI
■ E = {Pl-+R1-+P3-+R2-+P1} P2
■ There is no deadlock
16
r
Operating Systems - Dr. Abdulbaset Gaddah
17
r,
Methods for Handling Deadlocks
\...(../
•!• No single effective strategy that can deal with all types of deadlock
•!• Deadlock prevention
■ disallow one of the three necessary conditio,ns for deadlock occurrence,
or prevent circular wajt condition from happening
18
19
r,
Indirect Method of Deadlock Prevention
\._ (.,/
f
'
-,
Operating Systems - Dr. Abdulbaset Gaddah 20
20
' ,../
Operating Systems - Dr. Abdulbaset Gaddah 21
21
22
23
r,
Process Initiation Denial
\,J I
•!• Each pr,ocess declares its maximum need for each resource. A
process is only allowed to start if the maximum ,claim of all ,current
processes plus the maximum claim of the new process can be met
24
•!• Safe state is one in which there is at least one safe sequence of
resource allocations to processes that does not result in a deadlock
(i.e., all the processes can be run to completion)
•!• Unsafe state is a state that is not safe
',../
Operating Systems - Dr. Abdulbaset Gaddah 25
25
r,
Data Structures for the Banker's Algorit:hm
\,J ,./
26
,_,;
Operating Systems - Dr. Abdulbaset Gaddah 27
27
r,
Resource-Request Algorithm: for Process Pi
\..,,l./
•!• Request_ : request vector of length m for Pi. If Requesti [j] = k, this
means process Pi wants k instances of resource type RJ
28
Rl R2 R3 Rl R2 R3 Rl R2 R3
2 2
,,
Pl 3 ') '.> Pl l 0 0 Pl -
P2 6 l 3 P2 6 l 2 P2 0 0 l
P3 3 1 4 P3 ')
- 1 l P3 l 0 3
PJ ,l 2 ')
- P4 0 0 1 P4 4- 2 0
Claim matrix C _.i\Uocation matrix A C- A
Rl R2 R3 Rl R2 R3
I 9 I 3 I
Resource veetor R
6 I 0 I l I l
Available Yector , .
'-./
Operating Systems - Dr. Abdulbaset Gaddah 29
29
r,
P2 Runs to Completion
-
\._ (./
Note P2 is
completed
RI Rl R3 R2
R2 R3 R2 Rl
,
R3
Pl 3 -
)
-
')
Pl l 0 0 Pl -
')
2 -
P2 0 0 0 P2 0 0 0 P2 0 0 0
p,3 3 1 ~ P3 -
')
1 l P3 l 0 3
P4 4 -
')
"- P4 0 0 -? P4 4 -
')
0
Claim n1atrix C Allocation 1natrix .~ C-A
Rl R2 R3 Rl R2 R3
I 9
Ii
3
I
Resource vecior R
6
I I 6 I 2 I
3 I
Available rector \ 1
r ,
' -
Operating Systems - Dr. .Abdulbaset Gaddah 30
30
Note P1 is
completed
RI R2 R3 Rl R2 R3 Rl R2 R3
Pl 0 0 0 Pl 0 0 0 Pl 0 0 0 I
P2 0 0 0 P2 0 0 0 p~ 0 0 0
'
P3 3 1 4 P3 -
')
l l P3 l 0 3
')
P4 ~ - 0
4 -
? ')
~ P4 0 0 2 P4
Clail11 matrix C Allocation matrix .<\ C -.'\
Rl R2 R3 Rl R2 R3
I 9 J 3 I
Resource vector R
6 I I 7 J ?
I 3 I
-~ vailable vector \'
31 '
./
Operating Systems - Dr. Abdulbaset Gaddah
31
r,
P3 Runs to Completion
\ ._ (../
Note P3 is
completed
,
,
Rl R2 R3 Rl R2 RJ RI R2 R3
Pl 0 0 0 Pl 0 0 0 Pl 0 0 0
P2 0 0 0 P2 0 0 0 P2 0 0 0
r
P3 0 0 0 P3 0 0 0 P3 0 0 0
.,
P4 4 2 -
")
P4 0 0 2 P4 4 - 0
Clai.Jn 1natrix C Allocation matnx A C-A
Rl R2 R3 Rl R2 R3
j 9 I 3 I
Resource vector R
6 I I 9 I 3 I 4 I
Available vector , ,
•!• Thus, the system is in a safe state since the sequence <p2, pl, p3,
p4 > satisfies safety criteria
r ,
' -
Operating Systems - Dr. .Abdulbaset Gaddah 32
32
Rl R2 R3 Rl R2 RJ Rl R1 R3
Pl .. 2
,
- Pl l () 0 Pl
,, ,, 2
,,
P2 G l 3 PZ 5
,
l l P2 l 0 -
P3 3 I P'J - I I P3 1 0 3
,, ,, ., ,.,
P4 ➔
- -
C l ai,11 n1atrix C
- P4 0 0 -
Alloca tion n1atri::x. A.
P4 4 -
C- A
0
P3 3
.
l
..,
4 P'3 -., l l
..,
P'3 l 0
,.
.:,'
P-1 - ... P4 0 0 P+ -I - 0
Clain\ l\l~lrix C •.!>Jtocii tio11 1uatri:x A C- A
33
r,
Basic Facts
\ .J./
•!• Avoidance ⇒ ensure that a system w ill never enter an unsafe state
unsafe
deadlock
-
safe
r
Operating Systems - Dr. .Abdulbaset Gaddah 34
34
35
r,
Can Request for (1,0,2) by Pl be Granted?
\ ...l../
•!• Check t:hat Request < Need (that is, (1,0,2) ~ (1,2,2))
. ⇒ true .
•!• Check tlhat Request s Available (tha1t is, (1,0,2) s (3,3,2)) ⇒ true
36
■ Since Available becomes O for A, 0 for B, and 2 for C, we can not fin.d a
sequence to satisfy the safety requirements
■ Thus, the resulting state is unsafe and the request for (3,3,0) by P4 can
not be granted (P4 blocked)
...... ../
Operating Systems - Dr. Abdulbaset Gaddah 37
37
r,
Deadllock Avoidance Restrictions and Advantages
\,,.l./ -
•!• Restrictions:
■ Max. resource requirement for each process must be stated in advance
■ The processes under consideration must be independent;
• that is, the order in which they execute must be unconstrained by any
synchronization requirements
■ There must be a fixed number of resources to allocate
■ No process may exit whfle holding r,esource·s
•!• Advantages:
■ Allows more concurrency than deadlock prevention
■ Unn,ecessary to preempt and rollback processes, as in deadlock detection
■ Less restrictive than deadloctk prevention
• in deadlock prevention, request for a resource may not be gra:nted even if the
resulting state is safe
'
-,
Operating Systems - Dr. .Abdulbaset Gaddah 38
38
39
r,
Deadl!ock Detection
\...(..I
40
41
r,
'-._f.,/
- ------
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
~ I. " First 'un-mark' all processes
[ ]
• Finis.h i : = 10 se ,or i = 1, 2, ... , n. -:- - i that are not deadlocked
■ Work(i) : = Available(i) for all i;
■ For i = 1,2, ..., n, if Allocation (j,i) = 0, then Finish[i] = tirue
42
43
r,
Example of Detection Algorithm (Cont.)
\.J../
44
45
r,
Deadllock !Recovery
\._(../
46
47