Module-III (Part-II):
Deadlocks
by:
Dr. Soumya Priyadarsini Panda
Assistant Professor
Dept. of CSE
Silicon Institute of Technology, Bhubaneswar
System Model
System consists of resources
Resource types R1, R2, . . ., Rm
Examples: CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Each process utilizes a resource as follows:
request
use
release
A set of processes is in a deadlock state when every process in
the set is waiting for an event that can be caused only by
another process in the set.
Deadlock Characterization
In a deadlock situation, processes never finish executing, and system
resources are tied up.
preventing other jobs from starting.
Necessary Conditions:
A deadlock situation can arise if the following four conditions hold
simultaneously in a system:
1. Mutual exclusion
2. Hold and wait
3. No preemption
4. Circular wait
Necessary Conditions
1. Mutual Exclusion:
At least one resource must be held in a non-sharable mode; that is, only
one process at a time can use the resource.
If another process requests that resource, the requesting process must
be delayed until the resource has been released.
2. Hold and wait:
A process must be holding at least one resource and waiting to acquire
additional resources that are currently being held by other processes.
3. No preemption:
Resources cannot be pre-empted. i.e., a resource can be released only
voluntarily by the process holding it, after that process completes its
task.
Necessary Conditions Cont…
4. Circular wait:
A set {P0, P1, …, Pn} of waiting processes must exist such that
P0 is waiting for a resource held by P1,
P1 is waiting for a resource held by P2,
…,
Pn–1 is waiting for a resource held by Pn,
and Pn is waiting for a resource held by P0.
Resource-Allocation Graph
Deadlocks can be described more precisely in terms of a directed graph
called a system resource-allocation graph.
This graph consists of a 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 the processes in the system
R = {R1, R2, …, Rm}, the set consisting of all resource types in the system
request edge – directed edge Pi Rj
assignment edge – directed edge Rj Pi
Resource-Allocation Graph Cont…
Process
Resource Type with 4 instances
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Example-1
P={P1,P2,P3}
R={R1,R2,R3}
1instance of R1, 2 instances of R2,
1 instance of R3, 3 instances of R4
E={P1 R1, P2 R3, R1 P2, R2 P2, R2 P1,
R3 P3 }
Resource allocation graph:
Cont…
Whether deadlock situation occur??
Cont…
Whether deadlock situation occur??
No deadlocks
Example-2
Whether deadlock situation occur??
Cont…
2 cycles exist in the graph:
P1 -> R1 -> P2 -> R3 ->P3 ->R2 ->P1
P2 -> R3 -> P3 -> R2 -> P2
Process P1, P2, and P3 are deadlocked
Example-3
Graph with a cycle but no deadlocks.
Cont…
Cont…
If 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 of deadlock
Methods for Handling Deadlocks
Deadlocks can be handled using below 3 methods:
1. Using protocol to ensure that the system will never enter a deadlock
state:
Deadlock prevention
Deadlock avoidance
2. Allow the system to enter a deadlock state, detect it, and then recover
3. Ignore the problem and pretend that deadlocks never occur in the
system;
used by most operating systems, including UNIX
Deadlock Prevention
Deadlock prevention provides a set of methods to ensure that at least
one of the necessary conditions cannot hold.
Mutual Exclusion:
not required for sharable resources (e.g., read-only files);
must hold for non-sharable resources
Cont…
Hold and Wait:
Must guarantee that whenever a process requests a resource, it does
not hold any other resources
Require process to request and be allocated all its resources before it
begins execution, or allow process to request resources only when the
process has none allocated to it.
Disadvantages: Low resource utilization, starvation
Cont…
No Preemption:
If a process that is holding some resources and requests another
resource that cannot be immediately allocated to it,
then all resources the process is currently holding are preempted.
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 old resources,
as well as the new ones that it is requesting
Cont…
Circular Wait:
Impose a total ordering of all resource types, and require that each
process requests resources in an increasing order of enumeration
Deadlock Avoidance
Requires that the system has some additional a priori information
available.
Simplest and most useful model requires that each process declare the
maximum number of resources of each type that it may need.
The deadlock-avoidance algorithm dynamically examines the resource-
allocation state to ensure that there can never be a circular-wait condition
Resource-allocation state is defined by the number of available and
allocated resources, and the maximum demands of the processes
Safe State
A state is safe if the system can allocate resources to each process (up to
its maximum) in some order and still avoid a deadlock.
A system is in safe state if there exists a sequence <P1, P2, …, Pn> of all
the processes in the systems such that-
for each Pi, the resources that Pi can still request can be satisfied by:
currently available resources + resources held by all the Pj,
with j < i
Cont…
That is:
If Pi resource needs are not immediately available, then Pi can 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
Example
Consider a system with 12 resources and 3 processes:
Process Maximum Needs Currently holding(at time t0)
P0 10 5
P1 4 2
P2 9 2
Remaining resources=3
At time t0, the system is in a safe state.
The sequence <P1, P0, P2> satisfies the safety condition.
But if at time t1, if P2 is allocated with 1 resource deadlock situation
occurs.
Basic Facts
If 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 an unsafe state.
Deadlock Avoidance Algorithms
Single instance of a resource type
Use a resource-allocation graph
Multiple instances of a resource type
Use the banker’s algorithm
Resource-Allocation Graph Scheme
Introduce a new type of edge: Claim edge Pi Rj
That indicated that process Pi may request resource Rj at some time
in the future.
It is represented in the graph by a dashed line.
Claim edge converts to request edge when a process requests a
resource.
Request edge converted to an assignment edge when the resource is
allocated to the process.
When a resource is released by a process, assignment edge reconverts
to a claim edge.
Resources must be claimed a priori in the system
Example
Unsafe State in Resource-Allocation Graph
Resource-Allocation Graph Algorithm
Suppose that process Pi requests a resource Rj
The request can be granted only if converting the request edge to an
assignment edge does not result in the formation of a cycle in the resource
allocation graph
Note:
The resource-allocation-graph algorithm is not applicable to a resource
allocation system with multiple instances of each resource type.
Banker’s Algorithm
Used when multiple instances of resources are present in a resource
allocation system.
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 them in a finite
amount of time
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.
Available: 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 may request at most k instances of resource
type Rj
Allocation: n x m matrix.
If Allocation[i, j] = k then Pi is currently allocated k instances of Rj
Need: n x m matrix.
If Need[i, j] = k, then Pi may need k more instances of Rj to complete its task
Need [i, j] = Max[i, j] – Allocation [i, j]
Safety Algorithm
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 i such that both:
(a) Finish [i] = false
(b) Needi Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state
Resource-Request Algorithm for Process Pi
When a request for resources is made by process Pi , the following
actions are taken:
1. 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 must wait, since resources are not available
3.Pretend to allocate requested resources to Pi by modifying the state as
follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
Cont…
If the resulting resource-allocation state is safe, the resources are
allocated to Pi
If the new state is unsafe, then Pi must wait for Requesti , and the old
resource-allocation state is restored.
Example-1
Given 5 processes P1, P1, P2, P3, P4 and 3 resource types:
A (10 instances), B (5 instances), and C (7 instances)
Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Find the need matrix. Whether the system will be in safe state. If yes find
the safe sequence for which the system will be in safe state.
Answer
The content of the Need matrix:
Need[i]= Max[i] – Allocation[i]
Allocation Max Need
ABC AB C AB C
P0 010 753 743
P1 2 0 0 322 122
P2 3 0 2 902 600
P3 2 1 1 222 011
P4 0 0 2 433 431
Available resources: A B C
3 3 2
The system is in a safe state since the sequence < P1, P3, P4, P0, P2>
satisfies safety criteria
Detailed steps for obtaining safe sequence
To select a process Pi for the safe sequence,
Need[Pi]<=Available
For the 3 resources (A, B, C) with available instances at time0 (3, 2, 2):
For process P0 :
Need[P0] > Available (i.e. (7, 4, 3)> (3, 3, 2)
So condition is false
P0 can’t be allocated at time0
For process P1 :
Need[P1] < = Available (i.e. (1, 2, 2) < (3, 3, 2)
So condition is true P1 can be allocated
Available Allocation(P1 ) After P1 release the resources:
ABC ABC Available= (5, 3, 2)
332 200
210 322 Safe sequence obtained: < P1 ,
Cont…
For process P2 :
Need[P2] > Available (i.e. (6, 0, 0)> (5, 3, 2)
So condition is false
P0 can’t be allocated at time0
For process P3 :
Need[P3] < Available (i.e. (0, 1, 1) < (5, 3, 2)
So condition is true P3 can be allocated
Available Allocation (P3 ) After P3 release the resources:
ABC ABC Available= (7, 4, 3)
532 211
Safe sequence obtained: < P1 , P3,
521 222
Cont…
For process P4 :
Need[P4] < Available (i.e. (4, 3, 1) < (7, 4, 3)
So condition is true P4 can be allocated
Available Allocation (P4 )
ABC ABC After P4 release the resources:
743 002 Available= (7, 4, 5)
312 433
Safe sequence obtained: < P1 , P3, P4,
Cont…
For remaining process P0 :
Need[P0] <= Available (i.e. (7, 4, 3) < (7, 4, 5)
So condition is true P0 can be allocated
Available Allocation (P0 )
ABC ABC
745 010 After P0 release the resources:
Available= (7, 5, 5)
002 753
Safe sequence obtained: < P1 , P3, P4, P0,
Cont…
Next remaining process P2 :
Need[P2] <= Available (i.e. (6, 0, 0) < (7, 5, 5)
So condition is true P2 can be allocated
Available Allocation (P2 )
After P2 release the resources:
ABC ABC Available= (10, 5, 7)
755 302
155 902 = total number of resource instances as
given for resource A, B, C
Safe sequence obtained: < P1 , P3, P4, P0, P2
Therefore, the system is in a safe state for the safe sequence < P1, P3, P4,
P0, P2> satisfies safety criteria
Example Cont…
If P1 Request (1,0,2), then can the request be graned?
Check that Request Available (that is, (1,0,2) (3,3,2)
true
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement
Check can request for (3,3,0) by P4 be granted?
Check can request for (0,2,0) by P0 be granted?
Alternative Approach for Solution
Question:
Determine the new system state is safe. To do so, execute the safety algorithm
Therefore, the new system state is safe. So process p1 can be granted the resource
request immediately.
Deadlock Detection
Allow system to enter deadlock state then detect it and recover from it.
Detection Algorithm
Single instance of resource type
Multiple instance of resource type
Recovery scheme
Single Instance of Each Resource Type
Maintain wait-for graph
Nodes are processes
Pi Pj if Pi is waiting for Pj
Periodically invoke an algorithm that searches for a cycle in the graph.
If there is a cycle, there exists a deadlock
An algorithm to detect a cycle in a graph requires an order of n2
operations, where n is the number of vertices in the graph
Resource-Allocation Graph and Wait-for
Graph
Resource-Allocation Graph Corresponding wait-for graph
Several Instances of a Resource Type
Detection Algorithm:
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi 0, then
Finish[i] = false; otherwise, Finish[i] = true
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti Work
If no such i exists, go to step 4
Cont….
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish[i] == false, for some i, 1 i n, then the system is in deadlock
state. Moreover, if Finish[i] == false, then Pi is deadlocked
Algorithm requires an order of O(m x n2) operations to detect
whether the system is in deadlocked state
Example
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
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all I
No deadlock
Example (Cont.)
P2 requests an additional instance of type C
Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
State of system?
Can reclaim resources held by process P0, but insufficient resources to
fulfill other processes; requests
Deadlock exists, consisting of processes P1, P2, P3, and P4
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 be many cycles
in the resource graph and so we would not be able to tell which of the
many deadlocked processes “caused” the deadlock.
Recovery from Deadlock
Process Termination
Resource Preemption
Process Termination
To eliminate deadlocks by aborting a process, any one of the below
two methods can be used:
Abort all deadlocked processes
Abort one process at a time until the deadlock cycle is eliminated
Cont…
In which order should we choose to abort?
1. Priority of the process
2. How long process has computed, and how much longer to
completion
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?
Resource Preemption
To eliminate deadlocks using resource preemption, some resources can
be preempted from processes and given to other processes until the
deadlock cycle is broken.
Three issues need to be addressed:
Selecting a victim – minimize cost
Which resources and which processes are to be preempted
Rollback – return to some safe state, restart process for that state
Starvation – same process may always be picked as victim, include
number of rollback in cost factor
Assignment Question to Submit