Deadlocks
Outlook
Deadlock Characterization
Deadlock Prevention D dl k A id
Deadlock Avoidance
Deadlock Detection and Recovery ead oc etect o a d eco e y
Deadlock Characterization
Motivation
System owns many resources of the types Memory CPU Files and I/O
Memory, CPU, Files, and I/O
Many resources require exclusive access; e g
Many resources require exclusive access; e.g.
printer or CD-RW
Usage pattern
request Request use R
release R
Allocation of more than one resource may lead to a deadlock
a deadlock
Deadlock Example p
Assume two processes P
1and P
2with P
istarting to copy data from resource CD
ito CD
i+1P1 P2
P1 P2
Request CD1 Request CD2 Request CD2 Request CD1
<Copy> Copy <Copy> Copy
Release CD2 Release CD1
R l CD1 R l CD2
Release CD1 Release CD2
Deadlock Example p
Both may wait on each other to release the resource
P1 P2
P1 P2
Request CD1
Request CD2 Request CD2
Request CD2
Request CD1
<C > <C >
<Copy> <Copy>
Release CD2 Release CD1
Release CD1 Release CD2
Necessary Conditions for a Deadlock y
Mutual exclusion – only one process can use a t ti
resource at a time
Hold and wait – Process holds at least one resource and is waiting to acquire other ones
No preemption – Process releases resources only p p y after completing its task
Circular wait – There is a process ordering such
that process P
iiwaits for a resource allocated by
Resource Allocation Graph p
Vertices
{P
1,…,P
n} – the set of processes
{R R } the set of resources; each
Pi
{R
1,…,R
m} – the set of resources; each resource may have several instances;
each instance is denoted by a dot
Ri
each instance is denoted by a dot
Edges
P R
k: P requests an instance of R
kPi Pj
P
i R
k: P
irequests an instance of R
k
R
k P
j: P
jholds an instance of R
k Rk
Resource Allocation Graph: Example p p
Resource Allocation Graph: Properties p p
Deadlock Cycle y
Resource Allocation Graph: Properties p p
All resources have a single instance: Deadlock Cycle
Cycle involves only single instance resources Deadlock
(i.e. in such situation a cycle is a necessary and sufficient condition for a deadlock)
Cycle y Deadlock?
Methods for handling deadlocks g
Use a protocol to prevent or avoid deadlocks, assuring that the system never enters a deadlock state
system never enters a deadlock state
Deadlock prevention – provides a set of method to assure that at least one of the four necessary deadlock conditions cannot hold
Deadlock avoidance – decide for each process if it can proceed or
Deadlock avoidance decide for each process if it can proceed or has to wait based on available resources, currently allocated
resources and the resources allocated by a process in future (processes have to know this in advance!)
Allow the system to enter a deadlock state, detect it and recover
Ignore the problem and pretend that deadlocks never occur in the system
the system
Deadlock Prevention
Deadlock Prevention
Idea: Ensure that one of the four necessary conditions must not hold
conditions must not hold M t l l i
Mutual exclusion
Reasonable for sharable resources like read only files
files
(However, other resources like a printer are
intrinsically non-sharable and thus need mutual y exclusion)
Hold and Wait
Request all resources prior to execution or
Release all resources before allocating new ones
Deadlock Prevention
No preemption
Preempt all resources of a process requesting a resource which is already allocated
Process is inserted in the waiting queue of preempted and the new requested resource
P ti if ll th
Process can continue if all these resources are granted
Alternative: preempt only these resources
Alternative: preempt only these resources which are being requested during the wait
Process is placed in waiting queue of requested
Process is placed in waiting queue of requested resource
Allocated resources are preempted only if needed by p p y y another process
Deadlock Prevention
Circular Wait
Total ordering on resources F: {R1,…Rm} IN
Lock resources in ascending/descending order
Alternative: all resources greater/smaller or equal have to be
Alternative: all resources greater/smaller or equal have to be released before locking the new one
L ki ll i di d i f t t i l it A
Locking all resources in ascending order in fact prevents circular wait. Assume for the sake of contradiction a process set {P1,…,Pn} such that Pi holds
resource Ri and waits for resource Ri+1 which is held by Pi+1 (modulo n). Since Pi request resource Ri after Ri+1 (modulo n) we have:
Pi request resource Ri after Ri+1 (modulo n), we have:
Thus:
Thus:
Deadlock Prevention
Circular Wait
Ordering alone does not prevent circular wait
It´s up to the application programmers to follow
It s up to the application programmers to follow the ordering
System may issue a warning if ordering is not
System may issue a warning if ordering is not followed
How to decide on the right ordering?
How to decide on the right ordering?
Idea: ordering is determined dynamically while programs are executing
programs are executing
Example: Lock-order verifier witness on BSD UNIX used in conjunction with mutual exclusion locks
used in conjunction with mutual-exclusion locks
Deadlock Prevention (witness example) ( p )
Thread 1:
pthread mutex lock(&lockA);
pthread_mutex_lock(&lockA);
pthread_mutex_lock(&lockB);
/* do some work */
th d t l k( l kB)
pthread_mutex_unlock(&lockB);
pthread_mutex_unlock(&lockA);
Thread 2:
pthread_mutex_lock(&lockB);
pthread mutex lock(&lockA);_ _ /* do some work */
pthread_mutex_unlock(&lockA);
pthread mutex unlock(&lockB);
pthread_mutex_unlock(&lockB);
Lock ordering if Thread 1 executed first: Once Thread 2 executes, witness generates a warning
Deadlock Avoidance
Deadlock Avoidance: Safe States
Assumption: each process declares the max. number of resources
instances it needs unsafe
instances it needs
Definition: P1,…,Pn is a safe sequence if possible resource requests of Pi can
deadlock if possible resource requests of Pi can
be satisfied by
Currently available resources and
Resources held by all Pj for j<i
System is in a safe state if one such sequence exists
System is in an unsafe state if no such sequence exists
System in safe state no deadlock f can occur
Deadlock system is in unsafe state
safe
Example p
Consider 12 resource instances and three processes P
0, P
1, P
2with the following
allocations and maximum needs allocations and maximum needs
Maximum Allocated Maximum Allocated
P
010 5
P
14 2
P 9 2
P
29 2
Example p
P
11, P ,
00, P ,
22is a safe sequence q
Maximum Allocated Available
P1 4 2 3
P0 10 5 5
P1 finishes
P0 10 5 5
P2 9 2 10 P0 finishes
A System can go from Safe to Unsafe State y g
Example: assume P2 is granted an additional resource
Maximum Allocated
P
010 5
P
14 2
P
29 3
Only 2 resources available only P1 can be allocated all resources
allocated all resources
A System can go from Safe to Unsafe State y g
Maximum Allocated Available
P1 4 2 2
P0 10 5 4 P1 finishes
P2 9 3 …
P0 may request 5 more resources P0 has to wait
P22 may request 6 more resources y q P22 has to wait
Deadlock!
Deadlock!
Deadlock Avoidance: General Idea
System starts in a safe state
For each request
For each request
Grant resource if system remains in a safe state
P h t it th i
Process has to wait, otherwise
We consider two example algorithms which assure the system staying in safe state
assure the system staying in safe state
Resource-allocation-graph algorithm
Banker’s algorithm
Banker s algorithm
Resource-Allocation-Graph Algorithm p g
Assumption: each resource has exactly one instance
Idea: extend resource allocation Graph by claim edges
Idea: extend resource allocation Graph by claim edges
Transitions:
Claim Edge Request Edge
request
R R R
granted released
Assignment Edge P P P
Assignment Edge P P P
Grant a resource (either as assigned if it is free or as requested if it is held by another process) if the graph remains cycle free
Resource-Allocation-Graph Algorithm: Example p g p
R
22may not be granted to P y g
22( ) (b)
(a) (b)
Banker’s Algorithm g
Resource-allocation-graph algorithm not applicable when resources can have more than one instance.
resources can have more than one instance.
The following algorithm is applicable then as well.
A t d l i d it i d f h
A process must declare in advance its maximum need for each resource type.
This number may not exceed the number of available resources f th t
of these types.
When a process request resources the system checks if granting the resources leaves the system in safe state
If it will, resources are allocated
Otherwise, the process must wait until other processes have released enough resources
Before we proceed: we need the following data structures…
Banker’s Algorithm g
Let n be the number of processes and m be the number of resource types
number of resource types
We declare the following vector of length m and the
We declare the following vector of length m and the following matrices of size n x m
Available[j]=k k instances of resource Rj available
Max[i][j]=k Pi requests at most k instances of Rj All ti [i][j] k Pi i ll t d k i t f Rj
Allocation[i][j]=k Pi is allocated k instances of Rj Need[i][j]=Max[i][j] Allocation[i][j]
Need[i][j]=Max[i][j] - Allocation[i][j]
Banker’s Algorithm g
Some notation before we proceed
L t ( 1 ) d ( 1 ) b t t f l th
Let x = (x1,…xn) and y = (y1,…,yn) be two vectors of length n.
We say x · y iff
x1 · y1 , … , xn · yn
W if d
We say x < y if x · y and x ≠ y
Example: if x=(0,3,2,1) and y=(1,7,3,2) then x · y and x < y.
We treat each row in the matrices Allocation and Need
t f l th d f t th All ti
as vectors of length m and refer to them as Allocationi and Needi
Allocationi specifies the ressources currently allocated to process
Allocationi specifies the ressources currently allocated to process Pi
Nexti specifies the additional ressources that Pi may still request to complete ist task
to complete ist task.
Banker’s Algorithm: Safety Algorithm g y g
1.Let Work and Finish be vectors of length m
d ti l I iti li
and n , respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1.
2.Find an i such that both:
(a) Finish [i] = false (b) N d W k (b) Needi Work
If no such i exists, go to step 4.
3 Work = Work + Allocation 3. Work = Work + Allocation
iFinish [ i ] = true go to step 2.
g p
4.If Finish [ i ] == true for all i , then the system is
in a safe state.
Banker’s Algorithm: Request Algorithm g q g
Input: Resource request Requesti by process Pi, i.e., if Requesti[j] ==
k then process Pp ii wants k instances of resource type Ryp jj.
If Requesti · Needi we can continue, otherwise we are already done (error: the process has exceeded its maximum claim)
If Requesti · Available we can continue, otherwise Pi must wait, since the resources are not available
P t d ll ti
Pretend resource allocation
Available = Available – Requesti
Allocationi = Allocationi + Requesti Need = Need Request
Needi = Needi – Requesti
Check for safe state (Safety Algorithm)
Pi gets resource if result state is safe
Otherwise
Otherwise
Pi must wait
Restore old state
Banker’s Algorithm: Example
Consider a system with fife processes P0, P1, P2, P3, P4
g p
0 1 2 3 4
and three resource types A, B, C.
Available Instances
A B C
A B C
10 5 7
Allocation Max
C C
Need
A B C
A B C A B C
P0 0 1 0 7 5 3
P1 2 0 0 3 2 2
A B C
P0
P1 1 2 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 0 0 2 4 3 3 P4 4 3 1
Banker’s Algorithm: Example
Is the system in safe
g p
Available Instances
state?
Available
A B C
Instances
A B C
10 5 7
Allocation Max Fin
i h Need
A B C A B C ish
P0 0 1 0 7 5 3
P 2 0 0 3 2 2
A B C
P0 7 4 3
P 1 2 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P1 1 2 2
P2 6 0 0
P3 0 1 1
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
P3 0 1 1
P4 4 3 1
Banker’s Algorithm: Example
Consider now that
g p
Available Instances
process P1 requests one additional resource of
t A d t f t B
Available
A B C
3 3 2
Instances
A B C
10 5 7
type A and two of type B.
Allocation Max Fin
ish Need
A B C A B C ish
P0 0 1 0 7 5 3
P 2 0 0 3 2 2
A B C
P0 7 4 3
P 1 2 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P1 1 2 2
P2 6 0 0
P3 0 1 1
Request(1) = ( , , ).
P4 0 0 2 4 3 3 P4 4 3 1
Request(1) ( , , ).
First check that Request(1) · Available.
Banker’s Algorithm: Example
Consider now that
g p
Available Instances
process P1 requests one additional resource of
t A d t f t B
Available
A B C
Instances
A B C
10 5 7
type A and two of type B.
Allocation Max Fin
ish Need
A B C A B C ish
P0 0 1 0 7 5 3
P 3 2 2
A B C
P0 7 4 3
P
P1 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P1
P2 6 0 0
P3 0 1 1
Request(1) = ( 1 , 0 , 2 ). Old Allocation(1) = ( 2 , 0 , 0 )
P4 0 0 2 4 3 3 P4 4 3 1
Request(1) ( 1 , 0 , 2 ). Old Allocation(1) ( 2 , 0 , 0 )
Banker’s Algorithm: Example
Consider now that
g p
Available Instances
process P1 requests one additional resource of
t A d t f t B
Available
A B C
Instances
A B C
10 5 7
type A and two of type B.
Allocation Max Fin
ish Need
A B C A B C ish
P0 0 1 0 7 5 3
P 3 0 2 3 2 2
A B C
P0 7 4 3
P 0 2 0
P1 3 0 2 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P1 0 2 0
P2 6 0 0
P3 0 1 1
Check if a safe sequence can be found. As done before we
P4 0 0 2 4 3 3 P4 4 3 1
Check if a safe sequence can be found. As done before we can find for instance <P1 , P3 , P4 , P0 , P2>. Exercise.
Banker’s Algorithm: Example
Consider now that new
g p
Available Instances
system state. Can the following request be
t d?
Available
A B C
2 3 0
Instances
A B C
10 5 7
granted?
Allocation Max Fin
ish Need
A B C A B C ish
P0 0 1 0 7 5 3
P 3 0 2 3 2 2
A B C
P0 7 4 3
P 0 2 0
P1 3 0 2 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P1 0 2 0
P2 6 0 0
P3 0 1 1
Request(4) = ( 3 , 3 , 0 ).
P4 0 0 2 4 3 3 P4 4 3 1
Request(4) ( 3 , 3 , 0 ).
Banker’s Algorithm: Example
Consider now that new
g p
Available Instances
system state. Can the following request be
t d?
Available
A B C
2 3 0
Instances
A B C
10 5 7
granted?
Allocation Max Fin
ish Need
A B C A B C ish
P0 0 1 0 7 5 3
P 3 0 2 3 2 2
A B C
P0 7 4 3
P 0 2 0
P1 3 0 2 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P1 0 2 0
P2 6 0 0
P3 0 1 1
Request(0)=( 0 , 2 , 0 ). Obviously Request(0) · Available.
P4 0 0 2 4 3 3 P4 4 3 1
Request(0) ( 0 , 2 , 0 ). Obviously Request(0) · Available.
But remains the system in a safe state?
Deadlock Detection and Recovery
Deadlock Detection
System without deadlock prevention or avoidance deadlock may occur
deadlock may occur
If deadlocks are to be handled, however, the system dead oc s a e to be a d ed, o e e , t e syste must provide then
Algorithm which examines whether a deadlock has occurred
Algorithm which examines whether a deadlock has occurred
Algorithm to recover from a deadlock
We consider two methods again
W it f h h h l i t d
Wait-for-graph: each resource has only one instance and
Algorithm similar to bankers-algorithm when resources can have several instances
DD: Single Instance of a Resource g
Remove resource nodes ---
Resource allocation graph Wait-for graph
Deadlock wait for graph contains a circle
Maintain wait-for-graph Maintain wait-for-graph
DD: Several Instances of a Resource Type yp
We consider m resource types and n processes.
We define similar data structures as in the Banker’s algorithm
Available: A vector of length m indicates the number of available resources of each type.
Allocation: An n x m matrix defines the number of resources of each
Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process.
Request: An n x m matrix indicates the current request of each
If R t [i][j] k th P i ti k
process. If Request [i][j]= k, then process Pi is requesting k more instances of resource type Rj.
I th f ll i id i th l ti · d f h
In the following we consider again the relation · and for each processor Pi the rows of Allocation and Request as vectors Allocationi and Requesti
DD: Several Instances of a Resource Type yp
1. Let Work and Finish be vectors of length m
and n , respectively Initialize:
(a) Work = Available ( )
(b) For i = 1,2, …, n , if Allocation
i 0, then Finish [i] = false;otherwise, [ ] ; , Finish [i] = [ ] true .
2. Find an index i such that both:
2. Find an index i such that both:
(a) Finish [ i ] == false (b) Request Work (b) Request
i Work
If no such i exists go to step 4
If no such i exists, go to step 4.
DD: Several Instances of a Resource Type yp
3 Work = Work + Allocation 3. Work = Work + Allocation
iFinish [ i ] = true go to step 2
go to step 2.
4 If Fi i h [ i ] f l f i 1 i th
4. If Finish [ i ] == false, for some i , 1 i n , then the system is in deadlock state. Moreover, if
Fi i h [ i ] f l th P i d dl k d
Finish [ i ] == false , then P
iis deadlocked.
Example
Is the system in a
p
Available Instances
deadlocked state?
Available
A B C
Instances
A B C
7 2 6
Allocation Request Fin i h A B C A B C ish
P0 0 1 0 0 0 0
P 2 0 0 2 0 2
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
Example
Suppose now that
p
Available Instances
process P2 makes one additional request for
i t f t C
Available
A B C
Instances
A B C
7 2 6
an instance of type C.
Is the system in a
Allocation Request Fin
i h Is the system in a deadlocked state?
A B C A B C ish
P0 0 1 0 0 0 0
P 2 0 0 2 0 2
P1 2 0 0 2 0 2
P2 3 0 3 0 0 1
P3 2 1 1 1 0 0
We can reclaim the resources of Process P0. But then?
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
We can reclaim the resources of Process P0. But then?
Detection-Algorithm Usage g g
When to invoke the deadlock detection algorithm? In the extreme, whenever a requested resource could not be granted immediately.q g y
Advantage: in this case, the process causing the deadlock can be identified.
Disadvantage: incurs a considerable overhead in computation time.
Less expensive alternative: invoke the algorithm in less frequent intervals
h (d dl k f l )
e.g. once per hour (deadlocks occur infrequently) or
whenever CPU utilization drops below 40% (deadlock eventually cripples system throughput and causes CPU utilization drop)
Once a deadlock was detected: how to recover from a deadlock?
(Inform the user/operator: he can for instance manually terminate and restart processes or the whole system)
restart processes or the whole system)
System recovers automatically by process termination
System recovers automatically by resource preemption
Recovery from Deadlock: Process Termination y
System reclaims all resources allocated by the terminated thread
Two possible methods
Abort all deadlocked processes
Abort one process at a time
What is a good sequence? Many Factors: Priority, remaining computation time, number and type of allocated/required resources interactive/batch
allocated/required resources, interactive/batch
Recovery from Deadlock: Resource Preemption y p
Successively preempt resources and pass them to others Continue this until deadlock is broken
Continue this until deadlock is broken
Issues to be addressed
Issues to be addressed
Select victim – which resources and which process are to be preempted? (consider for instance the time a process already computed and/or the time it still predicts its outstanding
computed and/or the time it still predicts its outstanding computation.)
Rollback – Process has to be rolled back to a safe state from where it can resume execution once it got all required
where it can resume execution once it got all required resources (back)
Starvation – how to assure that resources are not always preempted from one process? (e g include the number of preempted from one process? (e.g. include the number of rollback in the cost factor when determining the process from which resources are preempted)
Summary and References
Summaryy
Deadlock: two or more processes waiting indefinitely for an event which can only be caused by the waiting processes themselves
Three principal methods to deal with deadlocks
Protocols to avoid or prevent deadlocks, assuring that the system never enters a deadlock state
Allow the system running into a deadlock situation, detect it at some point in time and
Allow the system running into a deadlock situation, detect it at some point in time and recover
Just ignore the problem as done in Windows or UNIX (deadlock occur infrequently)
Four necessary and sufficient conditions for a deadlock: mutual exclusion, hold and wait no preemption and circular wait
and wait, no preemption, and circular wait
Idea of deadlock prevention: just assure that not all four condition can occur at the same time
Deadlock avoidance algorithms: less stringent but require a priory information g g q p y on how (many) resources might be used by a process.
Deadlock detection alternatives: how often to invoke? what to do if a deadlock is detected?
Whenever preemption is used three issues must be addressed: selecting a
Whenever preemption is used, three issues must be addressed: selecting a victim, rollback and starvation.
References
Silberschatz, Galvin, Gagne, „Operating , , g , „ p g
System Concepts“, Seventh Edition, Wiley, 2005
2005