Chapter 7: Deadlocks
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
The Deadlock Problem
In a multiprogramming environment, several processes may
compete for a number of resources. A process requests
resources; if the resurces are not available at that time, the
process enters a waiting state. Sometimes, a waiting process will
never be able to change state, because the resources it has
requested are held by other waiting processes. This situation is
called a deadlock.
A set of blocked processes each holding a resource and waiting to
acquire a resource held by another process in the set.
Example
System has 2 CD RW drives.
P1 and P2 each holds one CD RW drive and each needs another one.
Example
semaphores A and B representing CD RW drives are initialized to 1
P0 P1
wait (A); wait(B)
wait (B); wait(A)
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 backs
up (preempt resources and rollback).
Several cars may have to be backed up if a deadlock
occurs.
Starvation is possible.
System Model
System consists of a finite number of resources
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices,
files, semaphores
Each resource type Ri has Wi instances.
If a system has two CPU’s, then the resource type CPU has two
instances.
Each process utilizes a resource as follows:
request
use
Release
A process cannot request more than the total number of
resources available in the system
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion: At least one of the resources is in a non-sharable
mode (two processes cannot use a resource at the same time), where
only one process at a time can use that resource(e.g. printer). If
another process request that resource, the requesting process must
be delayed untill the resource has been released.
Hold and wait: There must be at least one process holding at least
one resource and waiting to acquire additional resources currently
being hold by other processes.
No preemption: No resource can be preempted before the holding
process completes its task with that resource.
Circular wait: there exists a set {P0, P1, …, P0} of waiting processes
such that
P0 is waiting for a resource that is held by P1,
P1 is waiting 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 a resource that is held by P0.
Resource-Allocation Graph
Consist 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 of a Resource Allocation Graph
R={R1,R2,R3,R4}
P={P1,P2,P3}
E={P1R1,R1P2,P2R3,R3P3,R2P1,R2P2}
Resource instances=[1 2 1 3]
Available=[0 0 0 3]
Finding Deadlocks
A cycle in a resource-allocation graph is a necessary
condition for a deadlock:
If the resource allocation graph contains no cycles then
there is no deadlock in the system at that instant.
If the resource allocation graph contains a cycle then a
deadlock may exist.
If there is a cycle and each resource has exactly one
instance then a deadlock has occurred.
Example of a Resource Allocation Graph
No cycle No deadlock
Resource Allocation Graph - Example
Here are 2 cycles:
P1R1 P2 R3 P3 R2 P1
P2 R3 P3 R2 P2
hence, P1,P2,P3 are deadlocked
Resource Allocation Graph - Example
Here is a cycle:
P1R1 P3 R2 P1
But, there is no deadlock. P4 can
finish after a while and R2 will then
be assigned to P3. Hence, the cycle
will be broken.
Basic Facts
If graph contains no cycles no deadlock.
If graph contains a cycle
if only one instance per resource type exists, then deadlock.
if several instances per resource type exist, deadlock may
or may not exist.
Methods for Handling Deadlocks
Use a protocol to ensure that the system will never enter
a deadlock state:
Deadlock prevention: provides a set of methods to ensure
that at least one of the necessary conditions mentioned
before cannot hold.
Deadlock avoidance: requires that the operating system be
given additional information in advance concerning which
resources a process will request and use during its lifetime.
With this additional knowledge, the operating system can
decide for each request whether or not the process should
wait.
Allow the system to enter a deadlock state, detect it, and
then recover. OS can provide an algorithm that examines
the state of the system to determine whether a deadlock
has occurred and an algorithm to recover from the
deadlock
Ignore the problem and pretend that deadlocks never
occur in the system; used by most operating systems,
including UNIX, Linux and Windows.
Deadlock Prevention
The aim is to ensure that at least one of the four conditions we discussed
before, cannot hold.
Mutual Exclusion – not required for sharable resources (e.g., read-
only files); must hold for non-sharable resources
In general, we don’t have systems with all resources being sharable.
Some resources such as printers are non-sharable(cannot be
simultaneously shared by several processes). Hence, it is not possible to
prevent deadlocks by denying ME for such resources.
Hold and Wait – must guarantee that whenever a process requests
a resource, it does not hold any other resources (possible solutions
are:)
Protocol 1: Each process must request and get all of its resources before
it begins execution.
Protocol 2: Each process can request resources only when it has no
resources allocated. Before it can request any additional resources, it
must release all the resources that it is currently allocated.
Deadlock Prevention
Protocol 1: Consider a process that copies data from a DVD drive
to a file on disk, sorts the file, and then prints the results to a
printer. If all resources must be requested at the beginning of the
process, then the process must initially request the DVD drive,
disk file, and printer. It will hold the printer for its entire execution,
even though it needs the printer only at the end.
Protocol 2: Allows the process to request initially only the DVD
drive and disk file. It copies from the DVD drive to the disk and
then releases both the DVD drive and the disk file. The process
must then request the disk file and the printer. After copying the
disk file to the printer, it releases these two resources and
terminates.
Both protocols cause low resource utilization; starvation possible.
A process that requests several commonly used resources may
have to wait indefinitely.
Deadlock Prevention (Cont.)
No Preemption
There is no preemption for already allocated resources. To ensure
that this condition does not hold, following protocols can be used:
If a process that is holding some resources requests another
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 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.
Circular Wait – to ensure that this condition never holds,
impose a total ordering of all resource types, and to require
that each process requests resources in an increasing order
of enumeration.
Deadlock Prevention (Cont.)
Formally define a function,
F: R→N
where R= {R1, R2, …. , RN} is a set of resource types, and N is the set
of natural numbers (a unique integer number, that is assigned to each
resource type).
For example:
Resources = {tape drive, disk drive, printer}
Function F defined as:
F (tape drive) = 1
F (disk drive) = 5
F (printer) = 12
Each process can request resources only in an increasing order of
enumaration.
If a process wants to use the tape drive and the printer at the same
time, it should first request the tape drive, then the printer (since, 1<12).
Whenever a process requests a resource Rj, it must have released all
resources Ri with F(Ri) ≥ F (Rj).
With this protocol, a circular wait is impossible.
Deadlock Prevention (Cont.)
Proof by contradiction:
Assume a set of processes is in a circular wait:
{P1, P2, …. , PN}
Every Pi+1 is holding Ri and requesting Ri+1. So, we must have F (Ri) < F
(Ri+1), i = 1,2, .....
This means that,
F (R1) < F (R2) < … < F (Rn) < F (R1) F (R1) < F (R1)??? İmpossible
contradiction
R3
Example: N=3 So, no circular wait
P1
P3 R1
P2
R2
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
Given some additional information on how each process will
request resources, it is possible to construct an algorithm that
will avoid deadlock states. The algorithm will dynamically
examine the resource allocation operations to ensure that
there won’t be a circular wait on resources.
When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe
state.
A state is safe if the system can allocate resources up to its
maximum to each process in some order (safe sequence of
processes) avoiding a deadlock. A deadlock state is an
unsafe state
.
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.
Safe, Unsafe, Deadlocked State Spaces
Example
Consider a system with 12 tape drives and 3 processes P0, P1 and P2.
Assume that we know the maximum number of tape drives each processes
may request as:
P0 requires 10 tape drives,
P1 requires 4, and
P2 requires as many as 9.
Suppose at time T0, 9 tape drives are allocated as follows:
P0←5, P1←2, and P2←2. Available tape drive = 3
Process Allocation Maximum Need (more)
P0 5 10 5
P1 2 4 2
P2 2 9 7
At T0, there are 12-(5+2+2)=3 free tape drives. The sequence
<P1, P0, P2> is a safe sequence.
Example (continue)
It is possible to go from a safe to an unsafe state: Consider the above
example. Assume at time T1, P2 requests one tape drive and gets it. Now
we have 2 free tape drives.
The system is in an unsafe state, because only P1 can be allocated all its
tape drives. After it finishes and returns all 4 tape drives,
P0 is allocated 5, may request 5 more → has to wait
P2 is allocated 3, may request 6 more → has to wait
P0 and P2 may be deadlocked!
Process Allocation Maximum Need
P0 5 10 5
P1 2 4 2
P2 3 9 6
Banker’s Algorithm
Multiple instances in each Resource
Each process must declare the maximum number of
instances of each resource type that it may need.
When a process requests a resource, it has to wait
when enough resources are not available.
When a process gets all its resources, it must return
them in a finite amount of time.
Banker’s Algorithm – A deadlock avoidance
algorithm
Data structures of the algorithm:
Let n = number of processes, and m = number of resources types.
Available[m]: Vector of length m indicates number of available
resources of each type. If available [ j ] = k, there are k instances of
resource type Rj available.
Max[n,m]: n x m matrix defines maximum demand of each process.
If Max [i,j] = k, then process Pi may request at most k instances of
resource type Rj.
Allocation[n,m]: n x m matrix defines the number of resources of
each type currently allocated to each process. If Allocation[i,j ] = k
then Pi is currently allocated k instances of Rj.
Need[n,m]: n x m matrix indicates the remaining resource need of
each process. 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].
REMARKS
Each row vector in Allocation and Need are denoted as
Allocation (i) and Need (i). So, Allocation (i) specifies the
resources currently allocated to process Pi.
Let X and Y be vectors of length n. We say,
X≤Y X[i] ≤ Y[i], i=1, 2, …, n
X<Y X ≤ Y and X ≠ Y
: if and only if
The Algorithm for Process Pi
1. Process Pi makes a request. Requesti = request vector for
process Pi. If Requesti [j] = k then process Pi wants k
instances of resource type Rj.
2. If Request(i) > Need(i), then there is an error. Otherwise, go
to step 3.
3. If Request(i) >Available, then Pi must wait, since resources
are not available. Otherwise, go to step 4
4. Pretend to allocate requested resources to Pi by modifying the
state as follows:
Available = Available - Request(i);
Allocation(i) = Allocation(i)+ Request(i);
Need(i) = Need(i) – Request(i)
5. Check whether the resulting state is safe or not using safety
algorithm.
• If safe the resources are allocated to Pi.
• If unsafe Pi must wait, and the old resource-allocation
state is restored
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. If an i is found,
Work = Work + Allocation(i)
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state
Example of Banker’s Algorithm
Suppose a system with three resources types is supporting two
processes. The state of the system is as follows.
Available ═ [1 4 1] (i.e. not in use at the time being)
MAX ALLOCATION REQUEST NEED
R1 R2 R3 R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 1 3 1 0 0 0 1 2 0 1 3 1
P2 1 4 1 0 0 0 0 2 1 1 4 1
Suppose Request (1) is to be processed:
Available ═ [ 1 4 1] - [ 1 2 0] ═ [ 0 2 1].
Allocation ═ [ 1 2 0
0 0 0]
Need ═ [011
1 4 1]
Then, apply the safety algorithm
Example of Banker’s Algorithm (continue)
Work ═ [ 0 2 1]
Finish ═ [false, false]
i═1 : Need (1) ═ [ 0 1 1 ] ≤ Work? Yes
Work ═ Work + Allocation (1) ═ [ 1 4 1]
Finish [1] ═ true
i═2 : Need (2) ═ [ 1 4 1 ] ≤ Work? Yes
Work ═ Work + Allocation (2) ═ [ 1 4 1]
Finish [2] ═ true
═> System is in safe state, so do the allocation.
Example of Banker’s Algorithm
5 processes P0 through P4;
3 resource types:
A (10 instances), B (5instances), 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
Example (Cont.)
The content of the matrix Need is defined to be Max –
Allocation.
Need Available
ABC ABC
P0 743 332
P1 122
P2 600
P3 011
P4 431
The system is in a safe state since the sequence < P1, P3, P4,
P2, P0> satisfies safety criteria. (apply safety algorithm to verify)
Example: P1 Request (1,0,2)
Check that Request Available (that is, (1,0,2) (3,3,2) true.
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 1 600
P3 2 1 1 011
P4 0 0 2 431
Executing safety algorithm shows that sequence < P1, P3, P4,
P0, P2> satisfies safety requirement.
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
Deadlock Detection
If a system has no deadlock prevention and no deadlock
avoidance scheme, then it needs a deadlock detection
scheme with recovery from deadlock capability.
For this, information should be kept on the allocation of
resources to processes, and on outstanding allocation
request.
Then, an algorithm is needed which will determine
whether the system has entered a deadlock state. This
algorithm must be invoked periodically.
A Deadlock Detection Algorithm
Data structures used:
Available[m]: as in Banker’s Algorithm
Allocation [n, m]: as in Banker’s Algorithm
Request [n,m]: A two dimensional array of size n×m which
indicates the current request of each process. If Request [ i ,j ]
═ k, then Pi, is requesting k more instances of resource type Rj,
Allocation(i) and Request(i) are the i’th rows of Allocation and
Request matrices.
Work and Finish are vectors of length m and n, as in the safety
algorithm.
The Algorithm
A1: Initialize Work ═ Available
for i ═ 1 to n do
if Allocation (i ) ═ 0 then
Finish[i] ═ true
else
Finish[i] ═ false
A2: Find an index i such that both
Finish[i] ═ false
Request (i) ≤ Work
If no such i can be found, go to A4
A3: For that i found in A2 do
Work ═ Work + Allocation (i)
Finish[i] ═ true
go to A2
A4: If Finish [i] ≠ true for all i, then the system is in a
deadlock state and for those i’s for which Finish[i] ═
false, Pi’s are deadlocked.
Example of Detection Algorithm
Five processes P1 through P5; three resource types
A (7 instances), B (2 instances), and C (8 instances).
Snapshot at time T0:
Allocation Request Available
ABC ABC ABC
P1 0 1 0 000 000
P2 2 0 2 202
P3 3 0 3 000
P4 2 1 1 100
P5 0 0 2 002
Sequence <P1, P3, P4, P2, P5> will result in Finish[i] = true
for all i. (Apply the detection algorithm to verify this!)
Apply the detection algorithm:
A1. Work = Available = [0 0 0]
Finish [i] = False, for i = 1, 2, 3, 4, 5
A2. P1 : Finish [1] = False,
Request [1] ≤ Work (i.e [0 0 0] ≤ [0 0 0]) yes
A3. Work = Work + Alloaction(1)
= [0 0 0] + [0 1 0] = [0 1 0]
Finish[1] = True
A2. P2 : Finish[2] = False
Request[2] ≤ Work (i.e [2 0 2] ≤ [0 1 0]) No
A2. P3 : Finish[3] = False
Request[3] ≤ Work (i.e [0 0 0] ≤ [0 1 0]) yes
A3. Work = [0 1 0] + [3 0 3] = [3 1 3]
Finish[3] = True
Apply the detection algorithm(cont)
A2. P4 : Finish[4] = False
Request[4] ≤ Work (i.e [1 0 0] ≤ [3 1 3]) yes
A3. Work = [3 1 3] + [2 1 1] = [5 2 4]
Finish[4] = True
A2. P2 : Finish[2] = False
Request[2] ≤ Work (i.e [2 0 2] ≤ [5 2 4]) yes
A3. Work = [5 2 4] + [2 0 2] = [7 2 6]
Finish[2] = True
A2. P5: Finish[5] = False
Request[5] ≤ Work (i.e [0 0 2] ≤ [7 2 6]) yes
A3. Work = [7 2 6] + [0 0 2] = [7 2 8]
Finish[5] = True
A4. Finish[i] = True for all processes, there is no
deadlock.
Example (Cont.)
P3 requests an additional instance of type C.
Request
ABC
P1 000
P2 202
P3 001
P4 100
P5 002
State of system?
Can reclaim resources held by process P1, but insufficient
resources to fulfill other processes; requests.
Deadlock exists, consisting of processes P2, P3, P4, and P5.
Recovery from Deadlock
If the system is in a deadlock state, some method for
recovering it from the deadlock must be applied. There
are various ways of recovery from deadlock:
Process Termination: Abort one or more processes in order
to break the deadlock (mostly this is done)
Resource Preemption: Preempt some resources from some
of the deadlocked processes.
Recovery from Deadlock: Process Termination
Abort all deadlocked processes
Abort one process at a time until the deadlock cycle is
eliminated
In which order should we choose processes 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?
Recovery from Deadlock: Resource Preemption
Preempt some resources from processes and give these
resources to other processes untill the deadlock cycle is broken.
Three issues need to be addressed:
selecting a victim – Which processes and which resources
are to be preempted? Determine order of preeemption to
minimize cost.
Rollback – If a resources is preempted from a process, what
should be done with that process? Return to some safe state
and restart process from that state
Starvation – same process may always be picked as victim,
most common solution is to include number of rollbacks
in cost factor
Ignore Deadlocks - UNIX
UNIX does not provide any method to handle deadlocks.
So, the UNIX approach is just to ignore the problem on
the assumption that most users would prefer an
occasional deadlock to a rule restricting all users to one
process, one open file, and one of everything.