OS Ch07 Deadlocks PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

College of Computer and lnfo1·mation Systems

Depat'tment of Computer Science


Unive1·sity of Umm AI-Qura

14012203-4: Operating Systems


First Term: 2022/23

Chapter 7:
Deadlocks
IPERATIN
SYSTEM
~ COHCEPTS
Operating Systems 11.-. . . . H• ■"CMII
~lbftt ,a'l"n

~
,._.,...., ~ ,,inabi.. " ~ iii . .

Dl'. Ahdulbaset Gaddah


aagaddah@ 11qu.edu.sa

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

•!• To present a n,umber of different methods for preventing or avoiding


deadlocks in a computer system
■ Deadlock Prevention
■ Dead lock Avoida nee
■ Deadlock Detection
■ Recovery from, Deadlock

'
-,
Operating Systems - Dr. Abdulbaset Gaddah 2

Operating Systems - Dr. Abdulb·aset Gaddah 1


Scanned with CamScanner
Real, Life Example of Deadlock

Operating Systems - Dr. Abdulbaset Gaddah 3

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

•!• Permanent - none of the ,events are ever triggered


•!• Involves conflicting needs for resou rces by two o,r more processes
•!• No efficient solution in the general case
•!• Most ,operating systems do not prevent or deal with deadlocks

Operating Systems - Dr. Abdulbaset Gaddah 4

Operating Systems - Dr. Abdulbaset Gaddah 2


Scanned with CamScanne:r
B·r idge Crossing Example
•!• Potential Deadloc:k

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

Operating Systems - Dr. Abdulbaset Gaddah 5

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

•!• A consumable resource - can be created (produced} and destroyed


(consumed)
■ Examples: interrupts; signals; messages; and information in 1/0 buffers

'
-,
Operating Systems - Dr. .Abdulbaset Gaddah 6

Operating Systems - Dr. Abdulb·aset Gaddah 3


Scanned with CamScanne'r
['\
Deadl!ock - Example 1
•!• Memory Request: space is available for allocation of 200Kbytes, and
the following sequence of events occur:

P1 P2
• • • • ••

Request 70 Kbytes; Request 70 Kbytes;


•• • • ••

•• • • ••

Request 80 Kbytes; Request 80 Kbytes;

•!• Deadlock occurs if both processes progress to their secon,d r,equest

r...... ../

Operating Systems - Dr. Abdulbaset Gaddah 7

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

Ste1> Action Step 1\ction


Po Request (D) CJo Request (T)
P1 Lock(D) qi Lock (T)
p~ Req11esr (T) Q2 Recp1est {)))
P3 Lock(T) q, Lock (D)
P4 Pe1fo11n function q4 Perfo1"J11 function
})5 Uulock (D) (15 Unlock (T)
PG- U11lock (T) qt\ U1ilock (D)

•!• Deadlock occurs if the multiprogramming system interleaves the


execution of the two processes as follows: Po , p1 , q0 , q1 , p2 , q2

'
-,
Operating Systems - Dr. .Abdulbaset Gaddah 8

Operating Systems - Dr. Abdulb·aset Gaddah 4


Scanned with CamScanne'r
Deadl ock - Example 3
1

r-,.,

•!• Consider a pair of processes, in which each process attempts to


receive a message from the other process and t hen send a message
1

to the other process

P1 P2
..' " '

Receive (P2): Receive (P1 );


." ,.,
Send (P2, M1 ); Send (P1 , M2);

•!• Deadlock occurs if the Receive is blocking

Operating Systems - Dr. Abdulbaset Gaddah

r,
System Model
\.., (.,/

•!• A system consists of a finite number of resources to be distributed


among a number of competing processes
■ The resources may be partitioned! into several types (Rl, R2, ... , Rm)
• Example: CPU cycles, files, and I/0 devices
• Each resource type Ri has Wi instances

•!• Each pr,ocess utilizes a resource as follows:


■ Request
• If the request cannot be granted immediately, then tihe requ:esting process
must wait until it can acquire the resou rce
■ Use
■ Release

r
Operating Systems - Dr. Abdulbaset Gaddah 10

10

Operating Systems - Dr. Abdulb•aset Gaddah s


Scanned with CamScanner
The Conditions for Deadlock
•!• These 3 conditions must be present for a deadlock to ibe possible:
■ Mutual exclusion: only one process may use a resource at a time
■ Hold-and-wait: a process may hold allocated resources while awaiting
assignment of other resources
■ No pre-emption: no resource can be forcib.ly removed from a process
holding it

•!• 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

Operating Systems - Dr. Abdulb·aset Gaddah 6


Scanned with CamScanner
RAG - Example 1
•!• The sets P, R, and E:
■ P = {Pl, P2}
■ R = {Rt, R2}
Circular ,vait is rcsol,rable ns
111 E = {P1-+R1-+ P2-+R2-+P1} there arc 111ultiple instances of
the resource types

Jll RI

Pl Circular P2 Pl Circular P2
wait

Both graphs contain a cycle •


R2 R2

Deadlock . rodeadlock

Operating Systems - Dr. Abdulbaset Gaddah 13

13

RAG - Example 2

•!• The sets P, R, and E:


■ P = {Pl, P2, P3}
■ R = {Rt, R2, R3, R4} Jt I R3

■ E = {Pl~Rl , P2-+R3, Rl -+P2,


R2-+ P2, R2~Pl , R3-+P3}
Pl P2 P3
•!• The graph does not con.tain any
cycles (no deadlock)
•••
R2 R4

Operating Systems - Dr. Abdulbaset Gaddah 14

14

Operating Systems - Dr. Abdulb·aset Gaddah 7


Scanned with CamScanne:r
RAG - Example 3
•!• The sets P, R, and E:
■ P = {Pl , P2, P3}
■ R = {Rt, R2, R3, R4}
Rl Jl3
11 E = {P1-+R1-+ P2-+R3-+P3-+R2-+P1,
P2-+R3-+ P3-+ R2-+P2}

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

■ Pl is waiting for P2 to re:lease Rl

Operating Systems - Dr. Abdulbaset Gaddah 15

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

•!• One cycle exist in the system Pt P3

■ There is no deadlock

■ The circular wait is resolvable


P4
■ P4 may release its instance of R2, and R2
that resource can then be a !located to
P3 breaking t he cycle

Operating Systems - Dr. Abdulbaset Gaddah 16

16

Operating Systems - Dr. Abdulb·aset Gaddah 8


Scanned with CamScanner
Basic Facts

•!• If graph contains no cycles ⇒ no deadlock

•!• If graph contains a cycle ⇒


■ if only one instance per resource type, then a definite deadl!ock
■ if several instances per resource type, possibility of deadlock

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

•!• Deadlock avoidance


■ do not grant a resource request if this allocation miglht lead to deadlock

•!• Deadlock detection


■ always grant resource requests, when possible, but period:ically check for
the presence of deadlock and then recover from it

•!• Ignore the problem


■ pretend that deadlocks rnever occur in the system; used by most OSes,
inclluding UNIX and Windows (restart your computer if a deadloc:k occur)
f

Operating Systems - Dr. Abdulbaset Gaddah 18

18

Operating Systems - Dr. Abdulb·aset Gaddah 9


Scanned with CamScanner
Deadl'ock Prevention
•!• The OS is design in such a way that the 1possibility of deadlock is
excluded

•!• Deadlock prevention places restri-ctions on resource requests so that


dea.dlock cannot occur
■ Deadlock can be prev-ented by denying ait least any one of the ,deadlock
conditions discussed earlier

•!• Two main methods:


• Indirect
• prevent the occurrence of one of the three necessary conditions
■ Direct
• prevent the occurre:nc.e of circular wait

Operating Systems - Dr. Abdulbaset Gaddah

19

r,
Indirect Method of Deadlock Prevention
\._ (.,/

•!• Mutual Exclusion


■ cannot be disallowed. If access to a resource requires mutual exclusion
then it must be supported by the OS
• Not required for sharable resources; must hold for non-sharable resour,ces

•!• Hold and Wait


■ can be prevented by requiring that a process request all of its required
resources at one time and blocking the process until ,all requests can be
granted simultaneously
■ This approach is inefficient in three ways:
• a process may be held up for a long time waiting for all of its resource
requests to be filled (starvation)
• resources allocated to a process may remain unused for a long time, during
whic1h time they are denied to other processes (low resource utillization)
• a process may not know in advance all of the resources that it will require

f
'
-,
Operating Systems - Dr. Abdulbaset Gaddah 20

20

Operating Systems - Dr. Abdulb-aset Gaddah 10


Scanned with CamScanner
['\
Indirect Method of Deadlock Prevent,ion (Cont.)
•!• No Preemption
■ Can be prevented in several ways:
• If a process holding certain resources is denied a further req1uest, that process
must release its origin.al resources and request them again together with the
additional resource
• If a process requests a resource that is currently held by another low priority
process or blocked process, then the second process is preempted, and it
requests again for its required resources
■ The above approaches are possible for resources whose states are easily
saved and restored later, such as memory and registers

' ,../
Operating Systems - Dr. Abdulbaset Gaddah 21

21

Direct Method ,o f Deadlock Preventi'on


•!• Circular Wait
■ Can be prevented by defining a linear ordering of all resource types
■ If a process has been anocatedi resources of type R, then it may req1
uest
only those resources of types following R in the ordering

•!• This protocol prevents deadlock but may be inefficient, slowing


down processes and denying resource access unnecessarily

Operating Systems - Dr. Abdulbaset Gaddah 22

22

Operating Systems - Dr. Abdulb-aset Gaddah 11


Scanned with CamScanner
Deadl'ock Avoidance
•!• A decision is made dynamically whether the current resource
allocation request will, if granted, potentially lead to a ,deadlock
•!• Requires knowledge ,o f future pr,ocess resource requests
•!• Allows the three necessary conditions but makes judicious choices to
assure that the deadlock point is never reached

•!• Two approaches:


■ Process Initiation Denial - do not start a process if it's total demand
might lead t o deadlock
■ Resource Allocation Denial - do not grant an incremental! resource
request if this allocation might lead to deadllock

Operating Systems - Dr. Abdulbaset Gaddah

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

•!• Not optimal,


■ This strategy doesn't work well because it assumes the worst case, that
all processes will make their maximum resource requests at the same
time, which is unlikely. So, it may keep processes f rom starting that
actually would have run successfully

Operating Systems - Dr. Abdulbaset Gaddah 24

24

Operating Systems - Dr. Abdulb·aset Gaddah 12


Scanned with CamScanner
Resource Allocation Denial
•!• Referred to as the banker's algorithm
•!• Consider a system with a fixed number of processes and resources
• At any time, a 1
process may have zero or more resources allocated to it

•!• State of the system reflects the current allocation of resou1rces to


processes
• Thus, the resource allocation state is ,defined by the number of available
and allocated resources, and the maximum claims of the pr,ocesses

•!• 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 ,./

•!• Consider a system of n processes and m of different types of


resources. Let us define the following vectors and matrices:
•!• Resource: vector of length m. If Resource [j] = k, there are k
instances of resource type R1 1n the system
•!• Available: vector of length m. If Available U] = k, there are k
instainces of resource type R1 currently available
•!• Claim : n x m matrix. If Claim [i, j] = k, then process P; willl request
at most k instances of resource type R1
•!• Allocation : n x m matrix. If Allocation[i, j] = k then ~ is currently
allocated (i.e. holding) k instances of Rj
•!• Need : n x m matrix. If Need[i, j] = k, then P; may need k more
instainces of R- to complete its task:
Need [i, j] = ~laim[i, j] - All'location[i, j]
r'
-,
Operating Systems - Dr. ,Abdulbaset Gaddah 26

26

Operating Systems - Dr. Abdulb,aset Gaddah 13


Scanned with CamScanne'r
Safety Algorithm
-,-f--1~--
\.
- /

1. Let Work and Finish be vectors of length m and n, respectively.


Initialization: Finish of n size
Work of m size
■ Work(i) : = Available(i) for a/I 1~· F
■ Finish [i] : = false for i = 1,2, ... , n. I F

2. Find an i such that both :: F

• Finish [i] == false


Note: ah\1ay, sc.in the Nt!ed array frvm to 1
• Need (j, i) < Work(i) for all i to botton1 to find the fi_rst process P i \Vhose
Finish = fa lse 1.1111.i 1~ I\ eed can bi: n1cL
■ I f no such i exists, go to step 4
3. Work (i) : = Work(i) + Al/ocation(t i) for all i
(Resources freed \vhci1 process con1plclcs!)
■ Finish[i] : == true
■ go to step 2
4. If Finish [i] === true for all i, then t he system is in a safe state

,_,;
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

1. If Requesti ~ Needi go to step 2. Otherwise, error (process exceeded


its maximum claim)
2. If Requesti ~ Available, go to step 3. Otherwise Pi must wait,
(resources not available)
3. ''Allocate'' requested resou rces to Pi as follows:
■ Available := Available - Requesti
Assume that the request is granted
■ Allocation 1 :,= Allocationi + Requesti
Update the system state accordingly
■ Nee di : = Needi - Req uesti
Run safety check algorithm:
■ If safe ⇒ the resources are allocated to 1
Pi
■ If unsafe ⇒ r,estore the old resource-allocation state and block Pi
until it is safe to grant the request ,
- - - -- - - - - - - -------1:--+-7-,,
-
Operating Systems - Dr. Abdulbaset Gaddah 28

28

Operating Systems - Dr. Abdulb•aset Gaddah 14


Scan:n ed with CamScanner
Determination of Safe State
•!• State of a system consist ing of four processes and t hree resources
•!• Allocations have been m.ade to the four pr,ocesses

•!• I s t his a safe state?

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 , .

(o) lnirinl stnte

'-./
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

(b) P2 runs to con1pl~tion

r ,
' -
Operating Systems - Dr. .Abdulbaset Gaddah 30

30

Operating Systems - Dr. Abdulb·aset Gaddah 15


Scanned with CamScanner
P1 Runs to Completion
r'-1.
\. ... /

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 \'

(e) Pl runs to co1npletion

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 , ,

(dl PJ runs to completion

•!• 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

Operating Systems - Dr. Abdulb,aset Gaddah 16


Scanned with CamScanner
r,
Determination ofan Unsafe State
'-.. L./

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

RI R: R3 Rl R2 R3 Suppose that P1 makes the


I 9 _; I 6 I I 1 I , request for one additional unit
R "'5ourc,: v~ tor R A vailalil,: v,:c tur , , each of R1 and R3
Is this safe?
Rl R2 R3 Rl R2 R.3 Rl .~ : R :l
.., ., ,.,
Pl
P2
3
6
. -I -J
~
Pl
P2
-
5
0
1
l
l
Pl
P2
l
1
-0 2
l

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

Rl R:2 R3 Rl R2 RJ The answer is no. since each


9 ., o 1 I o I , I 1 process will need at least one
R "'5oure-,: ''<'<:to r R A v,ailalik Y<'Cl ur , ,
additional unit of R1 to finish .
(bJ, Pl l·1:q 11es cs oue w 1jt ea~l• of Rl a1ul R .3 Request denied: P1 is blocked
(
,_,,,
Operating Systems - Dr. Abdulbaset Gaddah 33

33

r,
Basic Facts
\ .J./

•!• If a system is in safe state ⇒ no deadlocks

•!• If a system is in unsafe state ⇒ possibility o-f deadlock

•!• Avoidance ⇒ ensure that a system w ill never enter an unsafe state

unsafe
deadlock
-
safe

r
Operating Systems - Dr. .Abdulbaset Gaddah 34

34

Operating Systems - Dr. Abdulb-aset Gaddah 17


Scanned with CamScanne:r
[',
Example of Banker's Algorithm
\.
- I

•!• 5 p'rocesses P0 through ~ ; 3 resource types: A (10 instarnces), B (5


instances), and C(7 instances)
•!• Snapshot at time fa: Claim - Allocation

Processes Allocation Claim Available Need


A B C A B C A B C A B C
PO 0 1 0 7 5 3 3 3 2 7 4 3
Pl 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 16 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1

■ Work = J.,a;-2: 5-,3-;-2: :7-,4;3': -7,5;3': 1-0,-5;5: 1 O, 5, 7


■ The system is in a safe state since the sequence < Pl, P3, PO, P2, P4>
satisfies safety criteria
'-../
Operating Systems - Dr. Abdulbaset Gaddah 35

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

Processes Allocation Claim Available Need


A B C A B C A B C A B C
PO 0 1 0 7 5 3 2 3 0 7 4 3
Pl 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1

■ Work =.2,3,(:):_5,.3;2~,j~: 10,5;5: 10,5,7


■ Executing safety algorithm shows that sequence < Pl, P3, PO, P2, P4 >
satisfies safety criteria
■ Thus, the request for (1,0,2) by Pl can be granted r'
-,
Operating Systems - Dr. .Abdulbaset Gaddah 36

36

Operating Systems - Dr. Abdulb·aset Gaddah 18


Scanned with CamScanne:r
[',
Can Request for (3,3,,0 ) by P4 be Granted?
•!• Check tlh at Request ::; Need (that is, (3,3,0) ::; (4,3,1)) ⇒ true
•!• Check tihat Request ::; Available (that is, (3,3,0) < (3,3,2)) ⇒ true

Processes Allocation Claim Available Need


A B C A B C A B C A B C
PO 0 1 0 7 5 3 0 0 2 7 4 3
Pl 2 0 0 3 2 2 1 .2 2
P2 3 0 2 9 0 2 ,6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 3 3 2 4 3 3 1 0 1

■ 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

Operating Systems - Dr. Abdulb·aset Gaddah 19


Scanned with CamScanner
Exercise - the Banker's Algorithm
-.-Hr-,.,1----
\....-,'
•!• We hav,e 3 resource types with amount: A =·9, B = 3, and C = 6;
•!• And have 4 processes P0 through P3 with initial state:

Processes Allocation Claim Available Need


A B C A B C A B C A B C
PO 1 0 0 3 2 2
Pl 5 1 1 6 1 3
P2 2 1 1 3 1 4
P3 0 0 2 4 2 2

■ Calculate Available vector and Need matrix .


■ Is the system in a safe state?
■ Suppose that P0 is requesting Q = (1,0,1). Should this request be
granted immediately?
Operating Systems - Dr. Abdulbaset Gaddah

39

r,
Deadl!ock Detection
\...(..I

•!• If deadlock-prevention or deadlock-avoidance are not employed,


then deadlock may occur. Hence, the OS must provide:
■ an algorithm to detect if deadlock is present
■ an alg·orithm to recover from deadlock
•!• Requested resources are granted to processes whenever possible

•!• When should we invoke the detection algorithm?


■ Every time a request for alllocation cannot be granted immediately
• This will help identify not only the deadlocked set of processes but also the
specific process that caused the deadlocik
■ For every resource request
• I ncur a considerable overhead in computation time (consume CPU time)
■ At arbitrary points in time
• Difficult to tell which of t!he many deadlocked processes caused the deadlock
r
Operating Systems - Dr. .Abdulbaset Gaddah 40

40

Operating Systems - Dr. Abdulb-aset Gaddah 20


Scanned with CamScanner
Deadl!ock Detection Algorithm
~ ~·1 - - - -
\.
- /

•!• The resource-allocation graph is not applicable to a system with


mu1ltiple instance of each resource type
•!• Let us now turn to a deadlock-detection algorithm that is applicable
to such a system
•!• The algorithm employs several data structures that are similar to
those used in the banker's algorithm
■ Use an Allocation matrix and an Available vector as 1
previous
■ Available : A vector of length m indicates the number of ava:ilable
res,ources of each type
■ Allocation : An n x m matrix defines the number of resources of each
type currently allocated to each process
■ Also, use Request: An n x m m.atrix indicates the current request of each
process. If Request [i,j] = k, then process Pi is requesting k more
instances of resource type ~
r
'-../
Operating Systems - Dr. Abdulbaset Gaddah 41

41

Deadl!ock Detection Algorithm (Cont.) 1

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

2. Find an i'ndex i such that both:


Note: ahvays s~.a n 1h.: Re(1uest array from top
■ Finish[i] == false to botton1 to find the fiJst proce:.~ Pi \\'hose
Fini,h = fa l\c and 1L, Re<aiuest c.tn be 1nc1.
■ Request(j,i) < Work(i) for all i
■ If no such i exist.s, go to step 4.
3. Work (i) : = Work(O + Allocation(i i) for all i
Finish[i] : = true
■ go to step 2
4. If Finish[ij === false, for some i, 1 s i s n, then the system is in
deadlock state. Moreover, if Finish[i] == false, then '1 is deadlocked
(

Operating Systems - Dr. Abdulbaset Gaddah 42

42

Operating Systems - Dr. Abdulb·aset Gaddah 21


Scanned with CamScanner
Example of Detection Algorithm
~r-,,1----
\...l,'

•!• 5 processes P0 through ~ ; 3 resource types: A (7 instances), B (2


instances), and C(6 instances)
•!• Snapshot at time fa:
Processes Allocation Request Available
A B C A B C A B C
PO 0 1 0 0 0 0 0 0 0
Pl 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2

■ Work = D,0;(): .0-;i-;O: -J,t;3':-5,-1;3': ~ : 7,2,6


■ Sequenc,e <PO, P2, Pl, P3, P4 > will result in Finish[i] = true for all i
■ There is no deadlock
'..,/
Operating Systems - Dr. Abdulbaset Gaddah 43

43

r,
Example of Detection Algorithm (Cont.)
\.J../

•!• P2 requests an additiona I instance of type C


1

Processes Allocation Request Available


A B C A B C A B C
PO 0 1 0 0 1Q, 0 0 0 0
Pl 2 0 0 2 ,o 2
P2 3 0 3 0 ,o 1
P3 2 1 1 1 ,o 0
P4 0 0 2 0 10 2

■ Mark IPO (Finish/OJ= true) since Request(O/ i) s Work(i) for all i


■ Set W = W + (0,1,0) = (0,1,0)
• I nsufficient resources to fulfill otheir pr,ocesses' requests. Algorithm
terminates. Pl, P2, P3, and P4 are deadlocked
r
Operating Systems - Dr. Abdulbaset Gaddah 44

44

Operating Systems - Dr. Abdulb-aset Gaddah 22


Scanned with CamScanne:r
Example of Detection Algorithm (Cont.)
\.
--
~; --------------
Processes Allocation Request Available
A B C D E A B C D E A B C D E
.PO 1 0 1 1 0 0 1 0 0 1 0 0 0 0 1
Pl 1 1 0 0 0 0 0 l!. 0 1
P2 0 0 0 1 0 0 0 0 0 1
P3 0 0 0 0 0 1 0 :l 0 1

■ Mark P3, because P3 has no allocated resources


■ Set Work = (0, 0, 0, 0, 1)
■ P2's Request ~ Work. So, mark P2 and set Work= Work+ (0,0,0,1,0) =
(0,0,0,1,1)
■ No other unma·rked process has a row in Request < Work. Therefore,
terminate the algorithm
■ Concludes that PO and Pl are deadlocked
r
Operating Systems - Dr. Abdulbaset Gaddah

45

r,
Deadllock !Recovery
\._(../

•!• Needed when deadlock is detected. The following approaches are


possible:
■ Abort all deadlocked processes (one of the most common solution
adopted in1OS! !)
■ Rollback each deadlocked process to some previously defined checkpoint
and restart them (original deadlock ma,y reoccur)
■ Successively abort deadlock processes until deadlock no longer exists
(each time we need to invoke the deadlock detection algorithm)
■ Successively preempt some resources from processes and give them to
other processes until deadlock no longer exists
•!• For the two last approaches: a victim process needs to be selected
according to:
■ least amount of CPU time consumed so far
■ least total resources allocated so far
■ least amount of ''work'' produced so far....
'
-,
Operating Systems - Dr. .Abdulbaset Gaddah 46

46

Operating Systems - Dr. Abdulb·aset Gaddah 23


Scanned with CamScanner
Chapter 7: Deadlocks

Questions & comments


Operating Systems - Dr. Abdulbaset Gaddah 47

47

Operating Systems - Dr. Abdulb•aset Gaddah 24


Scan:n ed with CamScanne:r

You might also like