You are on page 1of 53

Deadlock

CPSC 355/655

mb 1 version 10/2002
Deadlock

• Deadlock exists if a process is waiting for


an event which can never occur.
» e.g., two processes each waiting for the other
– to set a flag, or
– to send a message, or
– to give up a resource

• Our focus: contention over resources


» processes waiting for requested resources

mb 2 10/1999
Indefinite Postponement

• where the timing is such that a process


» does not get to do what it is trying to do, or
» does not get whatever it is trying to get,
» indefinitely,
» but there is a possibility that it may eventually succeed
• e.g., a process stuck
» in the ready queue because of priority
» in a busy wait, trying to enter a critical section, unable to
because of timing

mb 3 10/1999
Deadlock versus
Indefinite Postponement

• Indefinite Postponement example


A: while (TSET LOCK); B: while (TSET LOCK);
manipulate X; manipulate X;
LOCK ← FALSE; LOCK ← FALSE;

• 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

process A requests resource I

B II

process B owns resource II


mb 5 10/2000
RAG Example
A owns I and II
B requests IV
C owns III and requests I
D owns IV and requests III
E requests I, II, and III B

A
I C
IV

E III
II D

mb 6 10/2000
Directed Cycles
Α

Ι ΙΙ
P0
Β
R0 R1

Pn-1 P1

... R2

P2

Process Pi owns resource Ri


and requests Ri+1(mod n) for i = 0, 1, ..., n-1.
mb 7 10/2000
Multiple Resources

There may be multiple resources of the same type - I.e.,


equivalent resources.

Dots in the resource box indicate how many when there are
multiple equivalent resources.

•• two resources

••• three 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.

No deadlock exists here. What about here?

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.

• Hold and wait


» When a process requests a resource and is waiting for it, it
will not relinquish control over resources it already owns.
• No preemption
» Nothing can take away resources from a process which
wishes to retain them.
• Circular wait
» A directed cycle exists in the RAG.

mb 10 10/2000
Deadlock Exists if ...

The four necessary conditions hold,


» Exclusive use
» Hold and wait
» No preemption
» Circular wait

AND

there is no way to break the directed cycle by


having a needed resource become available
without violating the other 3 necessary conditions.

mb 11 10/1999
Cycles May Be Broken

If a process finishes using a resource and


voluntarily gives it up, then this is not a
violation of any of the necessary conditions.

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

• Does a deadlock exist now?


» Assume that there will be no further new requests for
resources.

• Graph theory approach applied to the RAG


» prune off processes which have all requested resources
allocated
– assume these processes have now finished
» reassign their resources
» repeat until
– all processes are pruned, or
– deadlock

mb 16 10/1999
Reallocating Resources

many ways to reassign resources


» optimal strategy: always give to a process which will
have all its requested 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

Processes P1, P2, ...,Pn

Resources R1, R2, ...,Rm

OWNik = the number of Rk owned by Pi

REQik = the number of Rk requested by Pi

FREEk = the number of Rk available

FINi = boolean indicating whether Pi is finished

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

• A directed cycle is sufficient.


» assuming that the other necessary conditions hold

• But finding a cycle in a graph with N nodes


takes O(N2) time.

• Reduce graph to WAIT FOR graph.


» eliminating many nodes

mb 23 10/1999
WAIT FOR Graphs

A B

X
reduces to WAIT FOR graph Z
Y

A B reduces to WAIT FOR graph

X
Z
Y
mb 24 10/2000
WAIT FOR Example

reduces to WAIT FOR graph


B

A
C
C B

mb 25 10/2000
Deadlock Recovery

Choose a victim process


» either terminate it or
» preempt its resources.
» one or many in a deadlock

Process preemption
» take away one or all resources
» it must be able to recover
» starvation possible

mb 26 10/1999
Which Process?

• can process recover


• how much time has process used
• how much additional time needed to finish
• which resources are being used
• which resources are needed
• priority
• how many more processes will have to be victims

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

• deny “exclusive use”


• deny “hold and wait”
• deny “no preemption”
• deny “circular wait”

Choose any one of these.

Adopt rules governing resources which prevent


this one condition from ever occurring.
mb 29 10/1999
Deny Exclusive Use

• e.g., require that resources be shareable

» applicable to UNIX files

• e.g., make resources appear to be shareable


by use of device drivers

» applicable to disks, printers, network interface

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

• e.g., take away resources which are needed


by another process
• process must either
» be able to return resources to same state, or
» roll back to an earlier state
• may lead to indefinite postponement
• applicable to
» CPU
» real memory

mb 32 10/2002
Deny Circular Wait

• e.g., order resources and require that


they be granted in that order
• order resources
» R0, R1, R2, R3, ...
» R0 < R1 < R2 < R3 < ...
• require that if Ri < Rk ,
then Ri can only be requested before Rk

• applicable to internal resources


» PCB
» page tables
» kernel semaphores in Linux 7/2001
mb 33
Proof by Contradiction
• R0 < R1 < R2 < R3 < ...
• if Ri < Rk , then Ri can only be requested before Rk
• so if Pm owns Rs and is requesting Rt, this implies
that
Rs < Rt
• if there is a circular wait
P0 owns Rα and is requesting Rβ
P1 owns Rβ and is requesting Rγ
...
Pn-1 owns Rδ and is requesting Rα
this implies
mb 34 Rα < Rβ < Rγ < ... < Rδ < Rα 10/2000
Denying Circular Wait
Po

Rα Rβ

Pn-1 P1

... Rγ

P2

P0 owns Rα and is requesting Rβ


P1 owns Rβ and is requesting Rγ
...
Pn-1 owns Rδ and is requesting Rα
this implies
mb 35 Rα < Rβ < Rγ < ... < Rδ < Rα 10/2000
Deadlock Prevention Rules

• Applicability of rules is determined by


» which resources one process is requesting, and/or
» which resources are available, and/or
» which resources the process has, and/or
» the type of a resource

• They are applied without respect to which


resources are owned by other processes.

mb 36 2/2002
Deadlock Avoidance
Requests for resources are

» granted, or
» denied (postponed)

depending on the overall state of the system.

A resource is granted if this cannot lead to a


deadlock in the future.

Knowledge about all future resource needs of


each process is required.
» A process must declare its maximum resource needs
mb 37 when it starts. 10/1999
Claim Edges

A I A owns I

B II B is requesting II

C III C will request III

mb 38 10/2000
Avoidance vs Prevention
Assume A & B each need both I & II and A owns I.
A

I II

Prevention might allow the following.


A

I II

Avoidance would deny II to B once A owns I.


mb 39 10/2000
Safe States
Assume that
» we know each process’ maximum resource needs
» each process will finish once its has all the resource it
needs and release all resources

The system state is Safe if available resources


can be allocated to processes in some order so
that
» each process will finish
» no deadlock will occur

An Unsafe state may lead to deadlock.


» may not currently be deadlock
mb 40 10/2002
What is Unsafe?

I II

Granting ownership of II to B would make the


state Unsafe.
A

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:

ALLOCATEDi = number of resources already allocated to process Pi


NEEDi = number of additional resources needed by Pi to finish
AVAIL = number of resources available
REQUEST = current request

1 If REQUEST > AVAIL, then deny request because not enough


resources.
2 Pretend to allocate request and apply the Safety algorithm.
3 If Safety Algorithm returns SAFE, then grant request otherwise
deny it.
mb 45 10/2000
Safety Algorithm
(for 1 type of resource)

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

Credit line = initial NEED

Customer Credit Current New Still


Line Loan Loan Need

A 5 3 3 2
B 6 4 4 2
C 4 0 1 3

Cash left (AVAIL) = 3 Cash left = 2

C requests 1. Should this be granted? YES


SAFE because
Would new state be Safe?
A can finish: AVAIL = 5
then B: AVAIL = 9
mb 47 then C 10/1999
Applying Safety Algorithm #2

Capital = 10

Customer Credit Current New Still


Line Loan Loan Need

A 5 3 3 2
B 6 4 4 2
C 4 0 2 2

Cash left (AVAIL) = 3 Cash left = 1

C requests 2. Should this be granted? No

Would new state be Safe? UNSAFE because


all customers need
at least 2 more
mb 48 10/1999
Applying Safety Algorithm #3
Note that a state is SAFE only if all can finish.

Capital = 10

Customer Credit Current New Still


Line Loan Loan Need

A 2 1 1 1
B 6 3 3 3
C 9 3 5 4

Cash left (AVAIL) = 3 Cash left = 1

C requests 2. Should this be granted? No

Would new state be Safe? UNSAFE because


A can finish: AVAIL = 2
but no one else can
mb 49 10/1999
Applying the Banker’s Algorithm
Needs
Customer Request Capital A B C D E Granted?
20 10 6 8 6 10
A 5 15 5 6 8 6 10 yes
B 3 12 5 3 8 6 10 yes
C 4 8 5 3 4 6 10 yes
D 3 5 5 3 4 3 10 yes
E 4 1 5 3 4 3 6 no
C 2 3 5 3 2 3 10 yes
A 2 1 3 3 2 3 10 no
B 2 1 5 1 2 3 10 yes
D 1 0 5 1 2 2 10 no
B 1 0 5 0 2 3 10 yes
6 5 - 2 3 10
E 4 2 5 - 2 3 6 yes
A 2 0 3 - 2 3 10 no
mb 50 D 1 1 5 - 2 2 6 no 7/2001
General Case
Many resources of many types!

Banker has many currencies!

AVAIL and REQUEST are vectors.


one entry for each resource type
AVAILm and REQUESTm

So are ALLOCATEDi and NEEDi.


ALLOCATEDim
NEEDim

NEEDi ≤ AVAIL is true if for all m


NEEDim ≤ AVAILm 10/1999
mb 51
Special Case:
One resource of each type
A

I II

A request will not be granted if a directed cycle would be created.

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

You might also like