Slide_iconblue_pc
BD21332_
Chapter 7DeadlocksChapter 7Deadlocks
7.2
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Chapter 7:  DeadlocksChapter 7:  Deadlocks
The Deadlock Problem
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
7.3
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
To develop description of deadlocks,which prevent sets of concurrent processesfrom completing their tasks.
To present number of different methodsfor preventing or avoiding deadlocks in acomputer system.
Chapter ObjectivesChapter Objectives
7.4
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
set of blocked processes each holding aresource and waiting to acquire resource heldby another process in the set.
Example:
System has disk drives.
P1 and P2 each hold one disk drive and each needsanother one.
Another example:
semaphores A and Binitialized to 1
    P0   P1
wait (A);wait(B)
wait (B);wait(A)
The Deadlock ProblemThe Deadlock Problem
7.5
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Traffic only in one direction.
Each section of bridge can be viewed as aresource.
If deadlock occurs, it can be resolved if onecar backs up (preempt resources and rollback).
Several cars may have to be backed up if adeadlock occurs.
Starvation is possible.
Bridge Crossing ExampleBridge Crossing Example
7.6
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Resource types R1R2., Rm
CPU cycles, memory space, I/O devices.
Each resource type Ri has Wi instances.
Each process utilizes resource as follows:
request
use
release
System ModelSystem Model
7.7
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Mutual exclusion:  only one process at time can use aresource.
Hold and wait:  process holding at least one resource iswaiting to acquire additional resources held by otherprocesses.
No preemption: resource can be released only voluntarilyby the process holding it, after that process has completedits task.
Circular wait: there exists set {P0P1…, P0of waitingprocesses such that Pis waiting for resource that is heldby P1P1 is waiting for resource that is held by P2…, Pn–1is waiting for resource that is held by Pnand Pn is waitingfor resource that is held by P0.
Deadlock CharacterizationDeadlock Characterization
Deadlock can arise if four conditions hold simultaneously:
7.8
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
is partitioned into two types:
P {P1P2…, Pn}, the set consisting of all the processesin the system.
R {R1R2…, Rm}, the set consisting of all resourcetypes in the system.
request edge – directed edge P Rj
assignment edge – directed edge Rj  Pi
Resource-Allocation GraphResource-Allocation Graph
A set of vertices V and a set of edges E.
7.9
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Resource-Allocation Graph (cont.)Resource-Allocation Graph (cont.)
Process
Resource Type with instances
Pi requests instance of Rj
Pi is holding an instance of Rj
Pi
Rj
Pi
Rj
7.10
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Example of ResourceAllocation GraphExample of ResourceAllocation Graph
7.11
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Resource Allocation GraphWith DeadlockResource Allocation GraphWith Deadlock
7.12
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Graph With CycleBut No DeadlockGraph With CycleBut No Deadlock
7.13
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
If graph contains no cycles  no deadlock.
If graph contains cycle 
if only one instance per resource type, thendeadlock.
if several instances per resource type, possibility ofdeadlock.
Basic FactsBasic Facts
7.14
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Ensure that the system will never enter adeadlock state.
Allow the system to enter deadlock state andthen recover.
Ignore the problem and pretend that deadlocksnever occur in the system; used by mostoperating systems, including UNIX.
Methods for Handling DeadlocksMethods for Handling Deadlocks
7.15
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Mutual Exclusion – not required for sharableresources; must hold for nonsharable resources.
Hold and Wait – must guarantee that whenever aprocess requests resource, it does not hold anyother resources.
Require process to request and be allocated all itsresources before it begins execution, or allow process torequest resources only when the process has none.
Low resource utilization; starvation possible.
Deadlock PreventionDeadlock Prevention
Restrain the ways request can be made:
7.16
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
No Preemption 
If process that is holding some resources requestsanother resource 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.
Circular Wait – impose total ordering of allresource types, and require that each processrequests resources in an increasing order ofenumeration.
Deadlock Prevention (Cont.)Deadlock Prevention (Cont.)
7.17
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Simplest and most useful model requires that eachprocess declare the maximum number of resources ofeach type that it may need.
The deadlock-avoidance algorithm dynamically examinesthe resource-allocation state to ensure that there cannever be circular-wait condition.
Resource-allocation state is defined by the number ofavailable and allocated resources, and the maximumdemands of the processes.
Deadlock AvoidanceDeadlock Avoidance
Requires that the system has some additional apriori  information available.
7.18
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
When process requests an available resource, systemmust decide if immediate allocation leaves the system insafe state.
System is in safe state:
if there exists sequence <P1P2…, Pnof ALL the processesin the systems.
Such that for each Pithe resources that Pcan still request canbe satisfied by currently available resources resources held byall the Pjwith i.
That is:
If Pi resource needs are not immediately available, then Pi canwait until all Pj have finished.
When Pj is finished, Pi can obtain needed resources, execute,return allocated resources, and terminate.
When Pi terminates, P+1 can obtain its needed resources, andso on.
Safe StateSafe State
7.19
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
If system is in safe state  no deadlocks.
If system is in unsafe state  possibility ofdeadlock.
Avoidance  ensure that system will neverenter an unsafe state.
Basic FactsBasic Facts
7.20
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Safe, Unsafe, Deadlock StateSafe, Unsafe, Deadlock State
7.21
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Single instance of resource type
Use resource-allocation graph
Multiple instances of resource type
Use the banker’s algorithm
Avoidance algorithmsAvoidance algorithms
7.22
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Claim edge Pi  Rjrepresented by dashedline; indicates that process Pi may requestresource Rj.
Claim edge converts to request edge when aprocess requests resource.
Request edge converted to an assignment edgewhen the  resource is allocated to the process.
When resource is released by process,assignment edge reconverts to claim edge.
Resources must be claimed priori in thesystem.
Resource-Allocation Graph SchemeResource-Allocation Graph Scheme
7.23
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Resource-Allocation GraphResource-Allocation Graph
7.24
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Unsafe State InResource-Allocation GraphUnsafe State InResource-Allocation Graph
7.25
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Suppose that process Pi requests resource Rj.
The request can be granted only if convertingthe request edge to an assignment edge doesnot result in the formation of cycle in theresource allocation graph.
Resource-Allocation Graph AlgorithmResource-Allocation Graph Algorithm
7.26
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Multiple instances.
Each process must priori claim maximum use.
When process requests resource it mayhave to wait.
When process gets all its resources it mustreturn them in finite amount of time.
Banker’s AlgorithmBanker’s Algorithm
7.27
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Available:  Vector of length m.
If available[jkthen there are available k instances ofresource type Rj.
Maxm matrix.
If Max[i,jkthen process Pi may request at most instancesof resource type Rj.
Allocationx m matrix.
If Allocation[i,jk then Pi is currently allocated k instances of Rj.
Needx m matrix.
If Need[i,j= kthen Pi may need k more instances of Rj tocomplete its task.
Need [i,j] Max[i,j– Allocation [i,j].
Data Structuresfor the Banker’s AlgorithmData Structuresfor the Banker’s Algorithm
Let n number of processes, and number ofresources types.
7.28
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
1.Let Work and Finish be vectors of length m and n,respectively.  Initialize:
Work  Available
Finish [i= false for i 0, 1, …, n- 1.
2.Find an such that both:
(a) Finish [i== false
(b) Needi   Work
If no such exists, go to step 4.
3. Work  Work Allocationi
Finish[i= true
Go to step 2.
4.If Finish[i== true for all ithen the system is in safestate.
Safety AlgorithmSafety Algorithm
7.29
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Requesti  request vector for process Pi.
    If Requesti [jk, then process Pi wants k instances ofresource type Rj.
1.If Requesti  Needi go to step 2. Otherwise, raise errorcondition, since process has exceeded its maximum claim.
2.If Requesti  Availablego to step 3. Otherwise Pi  must wait,since resources are not available.
3.Pretend to allocate requested resources to Pi by modifying thestate as follows:
Available  Available   Requesti  ;
Allocationi   Allocationi  Requesti  ;
Needi  = Needi  – Requesti  ;
If safe  the resources are allocated to Pi.
If unsafe  Pi must wait, and the old resource-allocationstate is restored.
Resource-Request Algorithm for Process PiResource-Request Algorithm for Process Pi
7.30
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Example of Banker’s AlgorithmExample of Banker’s Algorithm
processes P0  through P4;
    resource types:
            A (10 instances), B (5instances), and C (7 instances).
Snapshot at time T0:
AllocationMaxAvailable
CC
P002
 P12
 P22
 P32
 P423
7.31
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Example (Cont.)Example (Cont.)
The content of the matrix Need is defined to beMax – Allocation.
Need
C
 P03
 P12
 P20
 P31
 P41
The system is in safe state since the sequenceP1P3P4P2P0satisfies safety criteria.
7.32
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Example:  P1 Request (1,0,2)Example:  P1 Request (1,0,2)
Check that Request  Available (that is, (1,0,2) (3,3,2)  true.
AllocationNeedAvailable
CCC
P00
P120
P20
P31
P41
Executing safety algorithm shows that sequenceP1P3P4P0P2satisfies safety requirement.
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
7.33
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Deadlock DetectionDeadlock Detection
Allow system to enter deadlock state.
Detection algorithm.
Recovery scheme.
7.34
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Maintain wait-for  graph:
Nodes are processes.
Pi  Pj   if Pi is waiting for Pj.
Periodically invoke an algorithm that searchesfor cycle in the graph. If there is cycle, thereexists deadlock.
An algorithm to detect cycle in graphrequires an order of n2 operations, where n isthe number of vertices in the graph.
Single Instance of EachResource TypeSingle Instance of EachResource Type
7.35
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Resource-Allocation Graph andWait-for GraphResource-Allocation Graph andWait-for Graph
Resource-Allocation Graph
Corresponding wait-for graph
7.36
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Available: vector of length m indicates thenumber of available resources of each type.
Allocation:  An x m matrix defines the numberof resources of each type currently allocated toeach process.
Request:  An x m matrix indicates the currentrequest  of each process. If Request [i, jk,then process Pi is requesting k more instancesof resource type Rj.
Several Instances of EachResource TypeSeveral Instances of EachResource Type
7.37
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
1.Let Work and Finish be vectors of length m and n,respectively Initialize:
(a) Work Available
(b)  For i 0, 1, …, n-1,
      if Allocationi  0, then Finish[i] false; otherwise,Finish[i] true.
2.Find an index such that both:
(a)Finish[i== false
(b) Requesti  Work
If no such i exists, go to step 4.
Detection AlgorithmDetection Algorithm
7.38
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
3.`Work  Work Allocationi
Finish[itrue
Go to step 2.
4.If Finish[i== false, for some i i nthenthe system is in deadlock state.
Moreover, if Finish[i== falsethen Pi isdeadlocked.
Detection Algorithm (Cont.)Detection Algorithm (Cont.)
This algorithm requires an order of O(x n2) operationsto detect whether the system is in deadlocked state.
7.39
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Five processes P0 through P4;
Three resource types:
        A(7 instances), B(2 instances), and C(6 instances).
Snapshot at time T0:
 Allocation Request Available
C
P00
P12
P230
P30
P42
Sequence <P0P2P3P1P4will result inFinish[itrue for all i.
Example of Detection AlgorithmExample of Detection Algorithm
7.40
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
P2 requests an additional instance of type C.
Request
C
 P00
 P11
P21
P30
P42
State of system?
Can reclaim resources held by process P0butinsufficient resources to fulfill other processes;requests.
Deadlock exists, consisting of processes P1 P2P3,and P4.
Example (Cont.)Example (Cont.)
7.41
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
When, and how often, to invoke depends on:
How often 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, theremay be many cycles in the resource graph andso we would not be able to tell which of the manydeadlocked processes “caused” the deadlock.
Detection-Algorithm UsageDetection-Algorithm Usage
7.42
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Recovery from Deadlock:Process TerminationRecovery from Deadlock:Process Termination
Abort all deadlocked processes.
Abort one process at 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.43
Modified By Dr. Khaled Wassif
Operating System Concepts – 7th Edition
Silberschatz, Galvin and Gagne ©2005
Slide_iconblue_pc
Slide_iconvertical
Selecting 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.
Recovery from Deadlock:Resource PreemptionRecovery from Deadlock:Resource Preemption
Slide_iconblue_pc
BD21332_
End of Chapter 7End of Chapter 7