-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
1/41
Silberschatz, Galvin and Gagne20028.1Operating System Concepts
Chapter 8: Deadlocks
System Model Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Combined Approach to Deadlock Handling
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
2/41
Silberschatz, Galvin and Gagne20028.2Operating System Concepts
The Deadlock Problem
A set of blocked processes each holding a resource andwaiting to acquire a resource held by another process in theset.
Example
System has 2 tape drives.
P1andP2each hold one tape drive and each needs another one. Example
semaphoresAandB, initialized to 1
P0 P1wait (A);wait(B)wait (B);wait(A)
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
3/41
Silberschatz, Galvin and Gagne20028.3Operating System Concepts
Bridge Crossing Example
Traffic only in one direction.
Each section of a bridge can be viewed as a resource.
If a deadlock occurs, it can be resolved if one car backsup (preempt resources and rollback).
Several cars may have to be backed up if a deadlockoccurs.
Starvation is possible.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
4/41
Silberschatz, Galvin and Gagne20028.4Operating System Concepts
System Model
Resource typesR1,R2, . . .,Rm
CPU cycles, memory space, I/O devices
Each resource typeRihasWiinstances.
Each process utilizes a resource as follows:
request use
release
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
5/41
Silberschatz, Galvin and Gagne20028.5Operating System Concepts
Deadlock Characterization
Mutual exclusion:only one process at a time can use aresource.
Hold and wait:a process holding at least one resourceis waiting to acquire additional resources held by otherprocesses.
No preemption:a resource can be released onlyvoluntarily by the process holding it, after that process hascompleted its task.
Circular wait:there exists a set {P0,P1, ,P0} of waitingprocesses such thatP0is waiting for a resource that is
held byP1,P1is waiting for a resource that is held byP2, ,
Pn1is waiting for a resource that is held byPn,
andP0is waiting for a resource that is held byP0.
Deadlock can arise if four conditions hold simultaneously.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
6/41
Silberschatz, Galvin and Gagne20028.6Operating System Concepts
Resource-Allocation Graph
V is partitioned into two types:
P= {P1,P2, ,Pn}, the set consisting of all the processes in
the system.
R= {R1,R2, ,Rm}, the set consisting of all resource types
in the system.
request edge directed edgeP1Rj
assignment edge directed edgeRjPi
A set of verticesVand a set of edgesE.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
7/41Silberschatz, Galvin and Gagne20028.7Operating System Concepts
Resource-Allocation Graph (Cont.)
Process
Resource Type with 4 instances
Pirequests instance ofRj
Piis holding an instance ofRj
Pi
Pi
Rj
Rj
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
8/41Silberschatz, Galvin and Gagne20028.8Operating System Concepts
Example of a Resource Allocation Graph
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
9/41Silberschatz, Galvin and Gagne20028.9Operating System Concepts
Resource Allocation Graph With A Deadlock
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
10/41Silberschatz, Galvin and Gagne20028.10Operating System Concepts
Resource Allocation Graph With A Cycle But No Deadlock
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
11/41Silberschatz, Galvin and Gagne20028.11Operating System Concepts
Basic Facts
If graph contains no cyclesno deadlock.
If graph contains a cycle if only one instance per resource type, then deadlock.
if several instances per resource type, possibility of deadlock.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
12/41Silberschatz, Galvin and Gagne20028.12Operating System Concepts
Methods for Handling Deadlocks
Ensure that the system willneverenter a deadlockstate.
Allow the system to enter a deadlock state and then
recover.
Ignore the problem and pretend that deadlocks neveroccur in the system; used by most operating systems,
including UNIX.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
13/41Silberschatz, Galvin and Gagne20028.13Operating System Concepts
Deadlock Prevention
Mutual Exclusion not required for sharable resources;must hold for nonsharable resources.
Hold and Wait must guarantee that whenever aprocess requests a resource, it does not hold any other
resources.
Require process to request and be allocated all its resourcesbefore it begins execution, or allow process to request
resources only when the process has none. Low resource utilization; starvation possible.
Restrain the ways request can be made.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
14/41Silberschatz, Galvin and Gagne20028.14Operating System Concepts
Deadlock Prevention (Cont.)
No 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 for
which 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.
Circular Wait impose a total ordering of all resource
types, and require that each process requests resourcesin an increasing order of enumeration.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
15/41
Silberschatz, Galvin and Gagne20028.15Operating System Concepts
Deadlock Avoidance
Simplest and most useful model requires that eachprocess declare themaximum numberof resources ofeach type that it may need.
The deadlock-avoidance algorithm dynamically examinesthe resource-allocation state to ensure that there can
never be a circular-wait condition.
Resource-allocationstateis defined by the number ofavailable and allocated resources, and the maximum
demands of the processes.
Requires that the system has some additionala prioriinformationavailable.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
16/41
Silberschatz, Galvin and Gagne20028.16Operating System Concepts
Safe State
When a process requests an available resource, systemmust decide if immediate allocation leaves the system in a
safe state.
System is in safe state if there exists a safe sequence of allprocesses.
Sequence is safe if for eachPi, theresources thatPican still request can be satisfied bycurrently available resources + resources held by all thePj,withj
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
17/41
Silberschatz, Galvin and Gagne20028.17Operating System Concepts
Basic Facts
If a system is in safe stateno deadlocks.
If a system is in unsafe statepossibility of deadlock.
Avoidanceensure that a system will never enter anunsafe state.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
18/41
Silberschatz, Galvin and Gagne20028.18Operating System Concepts
Safe, Unsafe , Deadlock State
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
19/41
Silberschatz, Galvin and Gagne20028.19Operating System Concepts
Resource-Allocation Graph Algorithm
Claim edgePiRjindicated that processPimay requestresourceRj; represented by a dashed line.
Claim edgeconverts torequest edgewhen a process
requests a resource.
When a resource is released by a process,assignmentedge reconverts to a claim edge.
Resources must be claimeda prioriin the system.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
20/41
Silberschatz, Galvin and Gagne20028.20Operating System Concepts
Resource-Allocation Graph For Deadlock Avoidance
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
21/41
Silberschatz, Galvin and Gagne20028.21Operating System Concepts
Unsafe State In Resource-Allocation Graph
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
22/41
Silberschatz, Galvin and Gagne20028.22Operating System Concepts
Bankers Algorithm
Multiple instances.
Each process must a priori claim maximum use.
When a process requests a resource it mayhave to wait.
When a process gets all its resources it must
return them in a finite amount of time.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
23/41
Silberschatz, Galvin and Gagne20028.23Operating System Concepts
Data Structures for the Bankers Algorithm
Available:Vector of lengthm. If available [j] =k, there arekinstances of resource typeRjavailable.
Max:n x mmatrix. IfMax[i,j] =k, then processPimay
request at mostkinstances of resource typeRj.
Allocation:nxmmatrix. If Allocation[i,j] =kthenPiis
currently allocatedkinstances ofRj.
Need:nxmmatrix. IfNeed[i,j] =k, thenPimay needk
more instances ofRjto complete its task.
Need[i,j]= Max[i,j] Allocation[i,j].
Letn= number of processes, and
m= number of resources types.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
24/41
Silberschatz, Galvin and Gagne20028.24Operating System Concepts
Safety Algorithm
1.LetWorkandFinishbe vectors of lengthmandn,
respectively. Initialize:Work=Available
Finish[i] =falsefori- 1,3, ,n.
2.Find anisuch that both:(a)Finish[i] =false
(b)NeediWorkIf no suchiexists, go to step 4.
3.Work=Work+AllocationiFinish[i] =truego to step 2.
4.IfFinish[i] == true for alli, then the system is in a safe state.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
25/41
Silberschatz, Galvin and Gagne20028.25Operating System Concepts
Resource-Request Algorithm for ProcessPi
Request= request vector for processPi. IfRequesti[j] =kthen
processPiwantskinstances of resource typeRj.
1.IfRequestiNeedigo to step 2. Otherwise, raise errorcondition, since process has exceeded its maximum claim.
2.IfRequestiAvailable, go to step 3. OtherwisePimust
wait, since resources are not available.3.Pretend to allocate requested resources toPiby modifying thestate as follows:
Available=Available-Requesti;
Allocationi=Allocationi+Requesti;
Needi=NeediRequesti;;
If safe the resources are allocated to Pi.
If unsafe Pimust wait, and the old resource-allocationstate is restored
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
26/41
Silberschatz, Galvin and Gagne20028.26Operating System Concepts
Example of Bankers Algorithm
5 processesP0throughP4;3 resource typesA(10 instances),
B(5instances, andC(7 instances).
Snapshot at timeT0:
Allocation Max Available
A B C A B C A B C
P0 0 1 07 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 24 3 3
Need
A B CP0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
27/41
Silberschatz, Galvin and Gagne20028.27Operating System Concepts
Example (Cont.)
The content of the matrix. Need is defined to be Max Allocation.
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
The system is in a safe state since the sequence satisfies safety criteria.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
28/41
Silberschatz, Galvin and Gagne20028.28Operating System Concepts
ExampleP1Request (1,0,2) (Cont.)
Check that RequestAvailable (that is, (1,0,2)(3,3,2)true.AllocationNeedAvailable
A B C A B CA B C
P0 0 1 0 7 4 3 2 3 0
P1 3 0 20 2 0
P2 3 0 1 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
Executing safety algorithm shows that sequence
satisfies safety requirement.
Can request for (3,3,0) byP4be granted?
Can request for (0,2,0) byP0be granted?
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
29/41
Silberschatz, Galvin and Gagne20028.29Operating System Concepts
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
30/41
Silberschatz, Galvin and Gagne20028.30Operating System Concepts
Single Instance of Each Resource Type
Maintainwait-forgraph Nodes are processes.
PiPjifPiis waiting forPj.
Periodically invoke an algorithm that searches for a cycle
in the graph.
An algorithm to detect a cycle in a graph requires anorder ofn2operations, wherenis the number of verticesin the graph.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
31/41
Silberschatz, Galvin and Gagne20028.31Operating System Concepts
Resource-Allocation Graph and Wait-for Graph
Resource-Allocation Graph Corresponding wait-for graph
S lI t f R T
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
32/41
Silberschatz, Galvin and Gagne20028.32Operating System Concepts
Several Instances of a Resource Type
Available:A vector of lengthmindicates the number ofavailable resources of each type.
Allocation:Annxmmatrix defines the number ofresources of each type currently allocated to eachprocess.
Request:Annxmmatrix indicates the current requestof each process. IfRequest[ij] =k, then processPiis
requestingkmore instances of resource type.Rj
.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
33/41
Silberschatz, Galvin and Gagne20028.33Operating System Concepts
Detection Algorithm
1.LetWorkandFinishbe vectors of lengthmandn,respectively Initialize:
(a)Work=Available
(b)Fori= 1,2, ,n, ifAllocationi0, then
Finish[i] = false;otherwise,Finish[i] =true.
2.Find an indexisuch that both:(a)Finish[i] ==false
(b)RequestiWork
If no suchiexists, go to step 4.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
34/41
Silberschatz, Galvin and Gagne20028.34Operating System Concepts
Detection Algorithm (Cont.)
3.Work=Work+AllocationiFinish[i] =truego to step 2.
4.IfFinish[i] == false, for somei, 1in, then the system is indeadlock state. Moreover, ifFinish[i] ==false, thenPiis deadlocked.
Algorithm requires an order of O(mxn2)operations to detectwhether the system is in deadlocked state.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
35/41
Silberschatz, Galvin and Gagne20028.35Operating System Concepts
Example of Detection Algorithm
Five processesP0throughP4;three resource typesA (7 instances),B(2 instances), andC(6 instances).
Snapshot at timeT0:
Allocation Request Available
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 30 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
Sequence will result inFinish[i] = true for all
i.
C
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
36/41
Silberschatz, Galvin and Gagne20028.36Operating System Concepts
Example (Cont.)
P2requests an additional instance of typeC.Request
A B C
P0 0 0 0
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2
State of system?
Can reclaim resources held by processP0, but insufficientresources to fulfill other processes; requests.
Deadlock exists, consisting of processesP1,P2,P3, andP4.
D i Al ih U
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
37/41
Silberschatz, Galvin and Gagne20028.37Operating System Concepts
Detection-Algorithm Usage
When, and how often, to invoke depends on: How often 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
be able to tell which of the many deadlocked processes
caused the deadlock.
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
38/41
Silberschatz, Galvin and Gagne20028.38Operating System Concepts
Recovery from Deadlock: Process Termination
Abort all deadlocked processes.
Abort one process at a time until the deadlockcycle is eliminated.
In which order should we choose to abort? Priority of the process.
How long process has computed, and how muchlonger to completion.
Resources the process has used.
Resources process needs to complete. How many processes will need to be terminated.
Is process interactive or batch?
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
39/41
Silberschatz, Galvin and Gagne20028.39Operating System Concepts
Recovery from Deadlock: Resource Preemption
Selecting a victim minimize cost.
Rollback return to some safe state, restartprocess for that state.
Starvation same process may always bepicked as victim, include number of rollback incost factor.
CombinedApproachtoDeadlockHandling
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
40/41
Silberschatz, Galvin and Gagne20028.40Operating System Concepts
Combined Approach to Deadlock Handling
Combine the three basic approaches
prevention avoidance
detection
allowing the use of the optimal approach for each of
resources in the system.
Partition resources into hierarchically orderedclasses.
Use most appropriate technique for handlingdeadlocks within each class.
T ffi D dl kf E i 84
-
7/23/2019 Deadlock Condition System Programming & Operating system pdf
41/41
Traffic Deadlock for Exercise 8.4