Deadlock
Deadlock
Deadlock
Deadlock
What is Deadlock?
• Two or more entities need a resource to make progress, but will
never get that resource
• Examples from everyday life:
– Gridlock of cars in a city
– Class scheduling: Two students want to swap sections of a
course, but each section is currently full.
• Examples from Operating Systems:
– Two processes spool output to disk before either finishes,
and all free disk space is exhausted
– Two processes consume all memory buffers before either
finishes
Deadlock Illustration
AAset
setof
ofprocesses
processesisisin
inaaDEADLOCK
DEADLOCKstate statewhen
whenevery
every
process
processisiswaiting
waitingforforan
anevent
eventinitiated
initiatedby
byanother
another
process
processininthe
theset
set
Process
ProcessAA Process
ProcessBB
Request
RequestXX Request
RequestYY
Request
RequestYY Request
RequestXX
Release
ReleaseXX Release
ReleaseYY
Release
ReleaseYY Release
ReleaseXX
Deadlock Illustration
• Indefinite postponement
– Job is continually denied resources needed to make
progress
R2
WAIT
R1 time
Deadlock detected
R2
R1 time
2) Deadlock Avoidance
– Design system so that if a resource request is made that
could lead to deadlock, then block requesting process.
– Requires knowledge of future requests by processes for
resources.
• Mutual Exclusion
– Non-sharable resources
• Hold and Wait
– A process must be holding resources and waiting for others
• No pre-emption
– Resources are released voluntarily
• Circular Wait P1
R1 R2
P2
Deadlock Prevention
A/W
C/Y
• State of a system
– An enumeration of which processes hold, are waiting for,
or might request which resources
• Safe state
– No process is deadlocked, and there exists no possible
sequence of future requests in which deadlock could occur.
or alternatively,
Available = 2
Deadlock Avoidance
Unsafe State:
Current Loan Max Need
Process 1 8 10
Process 2 2 5
Process 3 1 3
Available = 1
Safe to Unsafe Transition
Current state being safe does not
necessarily imply future states are safe
Current Safe State:
Current Loan Maximum Need
Process 1 1 4
Process 2 4 6
Process3 5 8 Available = 2
User3 6 8 Available = 1
Essence of Banker's Algorithm
Variables
int i {denotes a process}
int Available
int CurrentLoan[i]
boolean Cannot_Finish[i]
Function
Claim[i] = MaximumNeed[i] - CurrentLoan[i];
Banker's Algorithm
Begin
Begin
Available = Total_Units;
Available = Total_Units; Initialize
For i = 1 to N Do
For i = 1 to N Do
Begin
Begin
Available = Available - CurrentLoan [i];
Available = Available - CurrentLoan [i];
Cannot_Finish [i] = TRUE;
Cannot_Finish [i] = TRUE;
End;
End;
i = 1;
i = 1;
while ( i <= N ) Do
while ( i <= N ) Do
begin
begin
If ( Cannot_Finish [i] AND Claim [i] <= Available )
If ( Cannot_Finish [i] AND Claim [i] <= Available )
Then Begin
Then Begin
Cannot_Finish [i] = False;
Cannot_Finish [i] = False;
Available = Available + CurrentLoan [i];
Available = Available + CurrentLoan [i];
i = 1;
i = 1;
End;
End;
Else i = i+1;
Else i = i+1;
End;
End;
If ( Available == Total_Units )
If ( Available == Total_Units )
Then Return ( SAFE )
Then Return ( SAFE ) Find schedule to
Else Return ( UNSAFE );
Else Return ( UNSAFE ); complete all jobs
End;
End;
Banker's Example #1
Total_Units = 10 units
N = 3 processes
Process: 1 2 3 1
Request: 2 3 4 1
Can the fourth request be satisfied?
Process Current Maximum Claim Cannot
Loan Need Finish
1 4
2 4
3 8
Available =
i=
Banker's Example #2
Total_Units = 10 units
N = 3 processes
Process: 1 2 3 1
Request: 4 1 1 2
Available =
i=
Banker's Algorithm: Summary
(+) PRO's:
☺ Deadlock never occurs.
☺ More flexible & more efficient than deadlock prevention. (Why?)
(-) CON's:
Must know max use of each resource when job starts.
=> No truly dynamic allocation
Process might block even though deadlock would never occur
Deadlock Detection
Allow
Allowdeadlock
deadlockto
tooccur,
occur,then
thenrecognize
recognizethat
thatititexists
exists
Pi Rj
Edges:
1) Request Pi Rj
2) Allocate Pi Rj
Resource Graphs: Example
R1 P1
P2 R2
P1 holds 2 units of R1
P1 holds 1 unit of R2
R1 has a total inventory of 4 units
P2 holds 1 unit of R1
P2 requests 1 unit of R2 (and is blocked)
Operations on Resource Graphs:
An Overview
P1 R1
DEADLOCKED?
R2 P2
P1 R1
DEADLOCKED?
R2 P2
1) P2 acquires 2 units of R2
2) P2 releases all resources (finishes)
3) P1 acquires 2 units of R1
4) P1 releases all resources (finishes)
Resource Graphs…
?
Can deadlock occur
with just one copy of
one resource?
Recovering from Deadlock
Once
Once deadlock
deadlock has
has been
been detected,
detected, the
the system
system
must
must be
be restored
restored to
to aa non-deadlocked
non-deadlocked state
state