Professional Documents
Culture Documents
CPSC 355/655
mb 1 version 10/2002
Deadlock
mb 2 10/1999
Indefinite Postponement
mb 3 10/1999
Deadlock versus
Indefinite Postponement
• Deadlock example
C: begin critical section X D: begin critical section Y
begin critical section Y begin critical section X
manipulate X & Y manipulate X & Y
end critical section Y end critical section X
end critical section X end critical section Y
mb 4 10/1999
Resource Allocation Graph
• RAG shows
» which processes have requested which resources
– and are currently waiting for them to be granted
» which resources are owned by which processes
– i.e., which resources have been granted to which
processes
A I
B II
A
I C
IV
E III
II D
mb 6 10/2000
Directed Cycles
Α
Ι ΙΙ
P0
Β
R0 R1
Pn-1 P1
... R2
P2
Dots in the resource box indicate how many when there are
multiple equivalent resources.
•• two resources
••
mb 8 10/2000
Directed Cycles
and Multiple Resources
A directed cycle does not guarantee deadlock
if there are multiple resources of the same type.
I I
•• B •• B
A A
C II C II
mb 9 10/2000
Conditions Necessary for
Deadlock
• Exclusive use
» A process is allowed exclusive access to resources it has
been granted.
mb 10 10/2000
Deadlock Exists if ...
AND
mb 11 10/1999
Cycles May Be Broken
Exclusive use
Hold and wait
No preemption
Circular wait
mb 12 10/1999
There is no deadlock if ...
A series of processes
» can finish
» voluntarily giving up resources
» which are then reassigned
» eventually freeing up one in each cycle
A C
•• •• E
B D A C
•• •• E
mb 13
B D 10/1999
Dealing with Deadlock
Three approaches
» Detection and Recovery
– detect deadlock when it occurs
– somehow break deadlock and allow most processes to
continue useful work
» Prevention
– determine how resources are managed
• focuses on the resources owned by the requesting process and the
available resources
• will not take into account the entire state of the current system
» Avoidance
– determine if resource can be granted
• based on current state of entire system
– which processes own which resources
mb 14 10/2000
Proactive vs Reactive
Approaches
• Proactive
» keep deadlock from happening
» Prevention
» Avoidance
• Reactive
» act in response to deadlock
» Detection and Recovery
mb 15 10/1999
Deadlock Detection
mb 16 10/1999
Reallocating Resources
B D
C
mb 17 10/1999
Detection Example 1
I
•• C
I
•• C
III
A B ••
III
A B ••
II
•• D
II
•• D
mb 18 10/1999
Detection Example 2
I I
•• ••
C C
A B A B
II II
IV IV
III III
D D
mb 19 10/1999
Detection Example 3
B IV B IV
II II
•• A III
•• A III
C V C V
I F I F
••• D ••• D
E VI VII E VI VII
mb 20 10/2000
Ingredients for Detection
mb 21 10/2000
Detection Algorithm
continue ← true
while (continue)
find i such that FINi = false and REQik ≤ FREEk for all k
if i does not exist then
continue ← false
else
FREEk ← FREEk + OWNik for all k
FINi ← true
end-if
end-while
if there exists i such that FINi = false then
return DEADLOCK
else
return NO DEADLOCK
end-if
mb 22 10/1999
Detection with
One Resource of Each Type
mb 23 10/1999
WAIT FOR Graphs
A B
X
reduces to WAIT FOR graph Z
Y
X
Z
Y
mb 24 10/2000
WAIT FOR Example
A
C
C B
mb 25 10/2000
Deadlock Recovery
Process preemption
» take away one or all resources
» it must be able to recover
» starvation possible
mb 26 10/1999
Which Process?
mb 27 10/1999
Choosing a Victim
A B C
A B C
mb 28 10/1999
Deadlock Prevention
DENY one of the 4 necessary conditions
mb 30 10/1999
Deny Hold and Wait
• e.g., require all resources to be
requested at the same time
» at job level
– before or when process starts
» at sub-job (step) level
– give up all resources at end of step
– request all resources for next step
• can be very wasteful
» can lead to low resource usage
• may lead to indefinite postponement
• most practical in batch environment
• in other environments, applicable to
» swap files
mb 31 10/1999
Deny No Preemption
mb 32 10/2002
Deny Circular Wait
Rα Rβ
Pn-1 P1
... Rγ
P2
mb 36 2/2002
Deadlock Avoidance
Requests for resources are
» granted, or
» denied (postponed)
A I A owns I
B II B is requesting II
mb 38 10/2000
Avoidance vs Prevention
Assume A & B each need both I & II and A owns I.
A
I II
I II
I II
I II
B
mb 41 10/2000
Safe Example
A I A I
II II
B •• B ••
C III C III
mb 42 10/2000
Unsafe Examples
D IV G
V VII
E •• H
••••
F VI K
mb 43 10/2000
Banker’s Algorithm
Bank
» has money in several currencies
» currencies can not be exchanged
» will loan it out
» but only if available funds are sufficient
– will not possibly lead to deadlock
Customers
» have various needs
» credit line specifies the maximum need
» request funds in stages
» will finish when all needs are fulfilled
» and return all funds to bank
mb 44 10/1999
Banker’s Algorithm
(for 1 type of resource)
To determine if a request should be granted to
process Pk:
Safety algorithm
continue ← true
Pretend to grant resource while (continue)
find i such that FINi = false and Ni ≤ AV
Ak = ALLOCk + REQUEST
if i does not exist then
Nk = NEEDk - REQUEST
continue ← false
AV = AVAIL - REQUEST
else
Ai = ALLOCATEDi for i ≠ k
AV ← AV + Ai
Ni = NEEDi for i ≠ k
FINi ← true
FINi = false for all i end-while
if there exists i such that FINi = false then
return UNSAFE
else
return SAFE
mb 46 10/2000
Applying Safety Algorithm #1
Capital = initial AVAIL = 10
A 5 3 3 2
B 6 4 4 2
C 4 0 1 3
Capital = 10
A 5 3 3 2
B 6 4 4 2
C 4 0 2 2
Capital = 10
A 2 1 1 1
B 6 3 3 3
C 9 3 5 4
I II
I II
B
mb 52 10/2000
Combined Approach
• Internal resources
» PCBs, page tables, process headers,…
» can be ordered, prevention
• CPU, real memory
» prevention via “deny no preemption”
• Swap space
» prevention, allocate all at the beginning
• Non-shareable job resources
» dedicated disk drives, tapes
» avoidance, or prevention, allocate all together
• “Shareable” job resources
» disks; make shareable through drivers
• Logical job resources
mb 53 » locks; detection (and recovery) 10/1999