chapter 7 - deadlocks - sduimada.sdu.dk/~jamik/dm510-16/material/chapter7.pdf · chapter 7 -...
TRANSCRIPT
1
CHAPTER 7 -DEADLOCKS
2
OBJECTIVESTo develop a description of deadlocks, which prevent setsof concurrent processes from completing their tasks
To present a number of different methods for preventingor avoiding deadlocks in a computer system
3 . 1
SYSTEM MODEL
SYSTEM MODELSystem consists of resources
Resource types R1, R2, … , Rm
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Each process utilizes a resource as follows:
request
use
release
3 . 24 . 1
DEADLOCK CHARACTERIZATION
DEADLOCK CHARACTERIZATIONDeadlock can arise if four conditions hold simultaneously.
Mutual exclusion
Hold and wait
No preemption
Circular wait
4 . 24 . 3
MUTUAL EXCLUSIONOnly one process at a time can use a resource
4 . 4
HOLD AND WAITA process holding at least one resource is waiting to acquire
additional resources held by other processes
4 . 5
NO PREEMPTIONA resource can be released only voluntarily by the process
holding it, a�er that process has completed its task
4 . 6
CIRCULAR WAITThere exists a set {P0 , P1 , … , Pn } of waiting processes such
that P0 is waiting for a resource that is held by P1 , P1 iswaiting for a resource that is held by P2 , … , Pn–1 is waiting
for a resource that is held by Pn, and Pn is waiting for aresource that is held by P0.
DEADLOCK WITH MUTEX LOCKS/ * t h r e a d o n e r u n s i n t h i s f u n c t i o n * / v o i d * d o _ w o r k _ o n e ( v o i d * p a r a m ) { p t h r e a d _ m u t e x _ l o c k ( & f i r s t m u t e x ) ; p t h r e a d _ m u t e x _ l o c k ( & s e c o n d m u t e x ) ; / * * * D o s o m e w o r k * / p t h r e a d _ m u t e x _ u n l o c k ( & s e c o n d m u t e x ) ; p t h r e a d _ m u t e x _ u n l o c k ( & f i r s t m u t e x ) ; p t h r e a d _ e x i t ( 0 ) ; } / * t h r e a d t w o r u n s i n t h i s f u n c t i o n * / v o i d * d o _ w o r k _ t w o ( v o i d * p a r a m ) { p t h r e a d _ m u t e x _ l o c k ( & s e c o n d m u t e x ) ; p t h r e a d _ m u t e x _ l o c k ( & f i r s t m u t e x ) ; / * * * D o s o m e w o r k * / p t h r e a d _ m u t e x _ u n l o c k ( & f i r s t m u t e x ) ; p t h r e a d _ m u t e x _ u n l o c k ( & s e c o n d m u t e x ) ; p t h r e a d _ e x i t ( 0 ) ; }
4 . 7
RESOURCE-ALLOCATION GRAPHA set of vertices V and a set of edges E.
V is partitioned into two types:
P = {P1 , P2 , … , Pn }, the set consisting of all theprocesses in the system
R = {R1 , R2 , … , Rn }, the set consisting of all resourcetypes in the system
request edge – directed edge Pi → Rj
assignment edge – directed edge Rj → Pi
4 . 84 . 9
LEGEND
Process
LEGEND
Resource Type with 3 instances
4 . 10
LEGEND
P2 requests instance of R
4 . 11
LEGEND
P2 is holding an instance of R
4 . 124 . 13
EXAMPLE RESOURCESP = { P1, P2, P3 }
R = { R1, R2, R3, R4 }
E = { P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → p1, R3 → P3 }
RESOURCE ALLOCATION GRAPH
4 . 14
GRAPH WITH A DEADLOCK
4 . 15
WITH A CYCLE BUT NO DEADLOCK
4 . 16
BASIC FACTSIf graph contains no cycles → no deadlock
If graph contains a cycle →
if only one instance per resource type, then deadlock
if several instances per resource type, possibility ofdeadlock
4 . 175 . 1
METHODS FOR HANDLINGDEADLOCKS
METHODS FOR HANDLINGDEADLOCKS
1. Ensure that the system will never enter a deadlock state
2. Allow the system to enter a deadlock state and thenrecover
3. Ignore the problem and pretend that deadlocks neveroccur in the system; used by most operating systems
5 . 26 . 1
DEADLOCK PREVENTION
6 . 2
DEADLOCK PREVENTIONRestrain the ways request can be made
Mutual Exclusion – not required for sharable resources;must hold for nonsharable resources
DEADLOCK PREVENTIONHold and Wait – multiple ways
1. must guarantee that whenever a process requests aresource, it does not hold any other resources.
2. Request all resources up front before execution. (all ornone)
6 . 3
DEADLOCK PREVENTIONNo Preemption
If a process that is holding some resources requests anotherresource that cannot be immediately allocated to it, then all
resources currently being held are released
Preempted resources are added to the list of resources forwhich the process is waiting
Process will be restarted only when it can regain its oldresources, as well as the new ones that it is requesting
6 . 46 . 5
DEADLOCK PREVENTIONCircular Wait
impose a total ordering of all resource types, and requirethat each process requests resources in an increasing order
of enumeration
DEADLOCK EXAMPLE WITH LOCKORDERING
v o i d t r a n s a c t i o n ( A c c o u n t f r o m , A c c o u n t t o , d o u b l e a m o u n t ) { m u t e x l o c k 1 , l o c k 2 ; l o c k 1 = g e t _ l o c k ( f r o m ) ; l o c k 2 = g e t _ l o c k ( t o ) ; a c q u i r e ( l o c k 1 ) ; a c q u i r e ( l o c k 2 ) ; w i t h d r a w ( f r o m , a m o u n t ) ; d e p o s i t ( t o , a m o u n t ) ; r e l e a s e ( l o c k 2 ) ; r e l e a s e ( l o c k 1 ) ; }
6 . 67 . 1
DEADLOCK AVOIDANCE
DEADLOCK AVOIDANCERequires that the system has some additional a priori
information available
Simplest and most useful model requires that each processdeclare the maximum number of resources of each type that
it may need
7 . 2
DEADLOCK AVOIDANCEThe deadlock-avoidance algorithm dynamically examines
the resource-allocation state to ensure that there can neverbe a circular-wait condition
Resource-allocation state is defined by the number ofavailable and allocated resources, and the maximum
demands of the processes
7 . 3
SAFE STATEWhen a process requests an available resource, system must
decide if immediate allocation leaves the system in a safestate
System is in safe state if there exists a safe sequence
<P1, P2, … , Pn>
of ALL the processes in the systems such that for each P~i ,the resources that Pi can still request can be satisfied by
currently available resources + resources held by all the Pj ,with j < i
7 . 4
SAFE STATEThat is:
If Pi resource needs are not immediately available, then Pican wait until all Pj have finished
When Pj is finished, Pi can obtain needed resources,execute, return allocated resources, and terminate
When Pi terminates, Pi+1 can obtain its needed resources,and so on
7 . 57 . 6
BASIC FACTSIf a system is in safe state → no deadlocks
If a system is in unsafe state → possibility of deadlock
Avoidance → ensure that a system will never enter anunsafe state.
SAFE, UNSAFE, DEADLOCK STATE
7 . 77 . 8
AVOIDANCE ALGORITHMSSingle instance of a resource type → Use a resource-
allocation graph
Multiple instances of a resource type → Use the banker’salgorithm
SCHEMEClaim edge Pi → Rj indicated that process Pi may requestresource Rj ; represented by a dashed line
Claim edge converts to request edge when a processrequests a resource
Request edge converted to an assignment edge when theresource is allocated to the process
When a resource is released by a process, assignmentedge reconverts to a claim edge
Resources must be claimed a priori in the system
7 . 9
RESOURCE-ALLOCATION GRAPH
7 . 10
UNSAFE STATE IN RESOURCE-ALLOCATION GRAPH
7 . 11
RESOURCE-ALLOCATION GRAPHALGORITHM
Suppose that process Pi requests a resource Rj
The request can be granted only if converting the requestedge to an assignment edge does not result in the formation
of a cycle in the resource allocation graph
Cycle detection is O(n2)
7 . 12
BANKER’S ALGORITHMMultiple instances
Each process must a priori claim maximum use
When a process requests a resource it may have to wait
When a process gets all its resources it must return themin a finite amount of time
7 . 137 . 14
BANKER’S ALGORITHM SETUPLet n = number of processes,
Let m = number of resources types.
DATA STRUCTURES NEEDEDAvailable: Vector of length m. If available[j] = k,there are k instances of resource type Rj available
Max: n x m matrix. If Max [i,j] = k, then process Pi mayrequest at most k instances of resource type R j
Allocation: n x m matrix. If Allocation[i,j] = kthen Pi is currently allocated k instances of Rj
Need: n x m matrix. If Need[i,j] = k, then Pi mayneed k more instances of Rj to complete its task
Need[i,j] = Max[i,j] – Allocation[i,j]
7 . 15
SAFETY ALGORITHM 1Let Work and Finish be vectors of length m and n,
respectively.
Initialize:
Work = Available
Finish[i] = false for i = 0,1,… ,n-1
7 . 16
SAFETY ALGORITHM 2Find an i such that both:
1. Finish[i] = false
2. Needi ≤ Work
If no such i exists, go to step 4
7 . 177 . 18
SAFETY ALGORITHM 3Work = Work + Allocationi
Finish[i] = true
go to step 2
7 . 19
SAFETY ALGORITHM 4If Finish [i] == true for all i, then the system is in a
safe state
RESOURCE-REQUEST ALGORITHMLet Requesti be request vector for for Process Pi
If Requesti[j] = k then process Pi wants k instances ofresource type Rj
When resource request made by Pi:
7 . 20
RESOURCE-REQUEST ALGORITHM1. If Requesti ≤ Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim
2. If Requesti ≤ Available, go to step 3. Otherwise Pi mustwait, since resources are not available
3. Pretend to allocate requested resources to Pi bymodifying the state as follows:
Available = Available – Request
Allocationi = Allocationi + Requesti
Needi = Needi – Requesti
7 . 217 . 22
RESOURCE-REQUEST ALGORITHMIf safe → the resources are allocated to Pi
If unsafe → Pi must wait, and the old resource-allocationstate is restored
EXAMPLE OF BANKER’S ALGORITHM5 processes P0 through P4
3 resource types:
A (10 instances)
B (5 instances)
C (7 instances)
7 . 23
EXAMPLE OF BANKER’S ALGORITHM
7 . 24
EXAMPLE OF BANKER’S ALGORITHM
The system is in a safe state since the sequence<P1,P3,P4,P2,P0> satisfies safety criteria
7 . 257 . 26
EXAMPLE OF BANKER’S ALGORITHMNow a request from P1 comes for (1,0,2)
Check first that
Requesti ≤ Available
EXAMPLE OF BANKER’S ALGORITHM
<P1,P3,P4,P0,P2> satisfies safety criteria
7 . 277 . 28
EXERCISECan request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
8 . 1
DEADLOCK DETECTION
8 . 2
DEADLOCK DETECTIONAllow system to enter deadlock state
Detection algorithm
Recovery scheme
SINGLE INSTANCE OF EACHRESOURCE TYPE
Maintain wait-for graph
Nodes are processes
Pi → Pj if Pi is waiting for Pj
Periodically invoke an algorithm that searches for a cyclein the graph. If there is a cycle, there exists a deadlock
An algorithm to detect a cycle in a graph requires O(n2)operations, where n is the number of vertices in the graph
8 . 3
GRAPHS
8 . 4
SEVERAL INSTANCES OF ARESOURCE TYPE
Available: A vector of length m indicates the number ofavailable resources of each type
Allocation: An n x m matrix defines the number ofresources of each type currently allocated to each process
Request: An n x m matrix indicates the current request ofeach process. If Request[i][j] = k, then process Piis requesting k more instances of resource type Rj
8 . 5
DETECTION ALGORITHM 1Let Work and Finish be vectors of length m and n,
respectively
Initialize:
Work = Available
For i = 1,2, … , n, if Allocationi ≠ 0, thenFinish[i] = false; otherwise, Finish[i] =true
8 . 6
DETECTION ALGORITHM 2Find an index i such that both:
Finish[i] == false
Request i ≤ Work
If no such i exists, go to step 4
8 . 78 . 8
DETECTION ALGORITHM 3Work = Work + Allocationi
Finish[i] = true
go to step 2
DETECTION ALGORITHM 4If Finish[i] == false, for some i, 1 ≤ i ≤ n, then thesystem is in deadlock state.
Moreover, if Finish[i] == false, then Pi isdeadlocked
Algorithm requires an order of O(m x n2) operations todetect whether the system is in deadlocked state
8 . 9
EXAMPLEFive processes P0 through P4
Three resource types
A (7 instances)
B (2 instances)
C (6 instances)
8 . 10
EXAMPLESnapshot at time T0
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] =true for all i
8 . 11
EXAMPLEP2 requests an additional instance of type C
State of system?
8 . 12
DETECTION-ALGORITHM USAGEWhen, and how o�en, to invoke depends on:
How o�en a deadlock is likely to occur?
How many processes will need to be rolled back?
→ One for each disjoint cycle
If detection algorithm is invoked arbitrarily, there may bemany cycles in the resource graph and so we would not beable to tell which of the many deadlocked processes“caused” the deadlock.
8 . 139 . 1
RECOVERY FROM DEADLOCK
9 . 2
PROCESS TERMINATIONAbort all deadlocked processes
Abort one process at a time until the deadlock cycle iseliminated
PROCESS TERMINATIONIn which order should we choose to abort?
1. Priority of the process
2. How long process has computed, and how much longer tocompletion
3. Resources the process has used
4. Resources process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?
9 . 3
RESOURCE PREEMPTIONSelecting a victim – minimize cost
Rollback – return to some safe state, restart process forthat state
Starvation – same process may always be picked asvictim, include number of rollback in cost factor
9 . 410 . 1
QUESTIONS
10 . 2
BONUSExam question number 5: Deadlocks