Deadlocks
Deadlocks
MODULE 3
DEADLOCKS
A process requests resources, if the resources are not available at that time, the process enters a
waiting state. Sometimes, a waiting process is never again able to change state, because the
resources it has requested are held by other waiting processes. This situation is called a
Deadlock.
SYSTEM MODEL
Under the normal mode of operation, a process may utilize a resource in only the following
sequence:
1. Request: The process requests the resource. If the request cannot be granted
immediately, then the requesting process must wait until it can acquire the resource.
2. Use: The process can operate on the resource.
3. Release: The process releases the resource.
A set of processes is in a deadlocked state when every process in the set is waiting for an event
that can be caused only by another process in the set. The events with which we are mainly
concerned here are resource acquisition and release. The resources may be either physical
resources or logical resources
1
Operating System BCS303
Deadlocks may also involve different resource types. For example, consider a system with one
printer and one DVD drive. Suppose that process Pi is holding the DVD and process P j is
holding the printer. If Pi requests the printer and Pj requests the DVD drive, a deadlock occurs.
DEADLOCK CHARACTERIZATION
Necessary Conditions
A deadlock situation can arise if the following four conditions hold simultaneously in a system:
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 preempted; that is, a resource can be released only
voluntarily by the process holding it, after that process has completed its task.
4. Circular wait: A set {P0, Pl, ... , Pn} of waiting processes must exist such that Po is
waiting for a resource held by P1, P1 is waiting for a resource held by P 2, ... , Pn-1 is
waiting for a resource held by Pn and Pn is waiting for a resource held by Po.
Resource-Allocation Graph
The graph consists of a set of vertices V and a set of edges E. The set of vertices V is
partitioned into two different types of nodes:
P = {P1, P2, ...,Pn}, the set consisting of all the active processes in the system.
R = {R1, R2, ..., Rm} the set consisting of all resource types in the system.
2
Operating System BCS303
Pictorially each process Pi as a circle and each resource type R j as a rectangle. Since resource
type Rj may have more than one instance, each instance is represented as a dot within the
rectangle.
A request edge points to only the rectangle R j, whereas an assignment edge must also designate
one of the dots in the rectangle.
When process Pi requests an instance of resource type R j, a request edge is inserted in the
resource-allocation graph. When this request can be fulfilled, the request edge is
instantaneously transformed to an assignment edge. When the process no longer needs access
to the resource, it releases the resource; as a result, the assignment edge is deleted.
Resource instances:
One instance of resource type R1
Two instances of resource type R2
One instance of resource type R3
Three instances of resource type R4
Process states:
Process P1 is holding an instance of resource type R2 and is waiting for an instance of
resource type R1.
Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an
instance of R3.
Process P3 is holding an instance of R3.
3
Operating System BCS303
If each resource type has exactly one instance, then a cycle implies that a deadlock has
occurred. If the cycle involves only a set of resource types, each of which has only a
single instance, then a deadlock has occurred. Each process involved in the cycle is
deadlocked.
If each resource type has several instances, then a cycle does not necessarily imply that
a deadlock has occurred. In this case, a cycle in the graph is a necessary but not a
sufficient condition for the existence of deadlock.
Processes P1, P2, and P3 are deadlocked. Process P2 is waiting for the resource R3, which is
held by process P3. Process P3 is waiting for either process P1 or process P2 to release resource
R2. In addition, process P1 is waiting for process P2 to release resource R1.
Consider the resource-allocation graph in below Figure. In this example also have a cycle:
P1→R1→P3→R2→P1
4
Operating System BCS303
However, there is no deadlock. Observe that process P4 may release its instance of resource
type R2. That resource can then be allocated to P3, breaking the cycle.
To ensure that deadlocks never occur, the system can use either deadlock prevention or a
deadlock-avoidance scheme.
Deadlock prevention provides a set of methods for ensuring that at least one of the necessary
conditions cannot hold. These methods prevent deadlocks by constraining how requests for
resources can be made.
In the absence of algorithms to detect and recover from deadlocks, then the system is in a
deadlock state yet has no way of recognizing what has happened. In this case, the undetected
deadlock will result in deterioration of the system's performance, because resources are being
held by processes that cannot run and because more and more processes, as they make requests
for resources, will enter a deadlocked state. Eventually, the system will stop functioning and
will need to be restarted manually.
5
Operating System BCS303
DEADLOACK PREVENTION
Deadlock can be prevented by ensuring that at least one of the four necessary conditions cannot
hold.
Mutual Exclusion
The mutual-exclusion condition must hold for non-sharable resources. Sharable
resources, do not require mutually exclusive access and thus cannot be involved in a
deadlock.
Ex: Read-only files are example of a sharable resource. If several processes attempt to
open a read-only file at the same time, they can be granted simultaneous access to the
file. A process never needs to wait for a sharable resource.
Deadlocks cannot prevent by denying the mutual-exclusion condition, because some
resources are intrinsically non-sharable.
Ex:
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.
The second method 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 again request the disk file and the printer. After
copying the disk file to the printer, it releases these two resources and terminates.
6
Operating System BCS303
No Preemption
The third necessary condition for deadlocks is that there be no preemption of resources that
have already been allocated.
To ensure that this condition does not hold, the following protocols can be used:
If a process 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.
The preempted resources are added to the list of resources for which the process is
waiting. The process will be restarted only when it can regain its old resources, as well
as the new ones that it is requesting.
If a process requests some resources, first check whether they are available. If they are, allocate
them.
If they are not available, check whether they are allocated to some other process that is waiting
for additional resources. If so, preempt the desired resources from the waiting process and
allocate them to the requesting process.
If the resources are neither available nor held by a waiting process, the requesting process must
wait. While it is waiting, some of its resources may be preempted, but only if another process
requests them.
A process can be restarted only when it is allocated the new resources it is requesting and
recovers any resources that were preempted while it was waiting.
Circular Wait
One way to ensure that this condition never holds is to impose a total ordering of all resource
types and to require that each process requests resources in an increasing order of enumeration.
To illustrate, let R = {R1, R2, ... , Rm} be the set of resource types. Assign a unique integer
number to each resource type, which allows to compare two resources and to determine
whether one precedes another in ordering. Formally, it defined as a one-to-one function
F: R ->N, where N is the set of natural numbers.
Example: if the set of resource types R includes tape drives, disk drives, and printers, then the
function F might be defined as follows:
F (tape drive) =
1 F (disk drive)
= 5 F (printer) =
12
Now consider the following protocol to prevent deadlocks. Each process can request resources
only in an increasing order of enumeration. That is, a process can initially request any number
of instances of a resource type -R i. After that, the process can request instances of resource type
Rj if and only if F(Rj) > F(Ri).
7
Operating System BCS303
DEADLOCK AVOIDANCE
Safe State
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 a safe state only if
there exists a safe sequence.
Safe sequence: A sequence of processes <P1, P2, ... , Pn> is a safe sequence for the
current allocation state if, for each Pi, the resource requests that Pi can still make can be
satisfied by the currently available resources plus the resources held by all Pj, with j <i.
In this situation, if the resources that Pi needs are not immediately available, then Pi can wait
until all Pj have finished. When they have finished, Pi can obtain all of its needed resources,
complete its designated task, return its allocated resources, and terminate. When Pi terminates,
Pi+1 can obtain its needed resources, and so on. If no such sequence exists, then the system
state is said to be unsafe.
A safe state is not a deadlocked state. Conversely, a deadlocked state is an unsafe state. Not all
unsafe states are deadlocks as shown in figure. An unsafe state may lead to a deadlock. As long
as the state is safe, the operating system can avoid unsafe states
8
Operating System BCS303
Resource-Allocation-Graph Algorithm
If a resource-allocation system has only one instance of each resource type, then a
variant of the resource-allocation graph is used for deadlock avoidance.
In addition to the request and assignment edges, a new type of edge is introduced, called
a claim edge.
A claim edge Pi ->Rj indicates that process Pi may request resource Rj at some time in
the future. This edge resembles a request edge in direction but is represented in the
graph by a dashed line.
When process Pi requests resource Rj, the claim edge Pi ->Rj is converted to a request
edge. When a resource Rj is released by Pi the assignment edge Rj->Pi is reconverted to
a claim edge Pi->Rj.
Note that the resources must be claimed a priori in the system. That is, before process Pi starts
executing, all its claim edges must already appear in the resource-allocation graph.
We can relax this condition by allowing a claim edge Pi ->Rj to be added to the graph only if
all the edges associated with process Pi are claim edges.
Now suppose that process Pi requests resource Rj. The request can be granted only if
converting the request edge Pi ->Rj to an assignment edge Rj->Pi does not result in the
formation of a cycle in the resource-allocation graph.
9
Operating System BCS303
There is need to check for safety by using a cycle-detection algorithm. An algorithm for
detecting a cycle in this graph requires an order of n 2 operations, where n is the number of
processes in the system.
If no cycle exists, then the allocation of the resource will leave the system in a safe state.
If a cycle is found, then the allocation will put the system in an unsafe state. In that case,
process Pi will have to wait for its requests to be satisfied.
To illustrate this algorithm, consider the resource-allocation graph as shown above. Suppose
that P2 requests R2. Although R2 is currently free, we cannot allocate it to P2, since this action
will create a cycle in the graph.
A cycle, indicates that the system is in an unsafe state. If P1 requests R2, and P2 requests R1,
then a deadlock will occur.
Banker's Algorithm
The Banker’s algorithm is applicable to a resource allocation system with multiple instances of
each resource type.
When a new process enters the system, it must declare the maximum number of
instances of each resource type that it may need. This number may not exceed the total
number of resources in the system.
When a user requests a set of resources, the system must determine whether the
allocation of these resources will leave the system in a safe state. If it will, the resources
are allocated; otherwise, the process must wait until some other process releases enough
resources.
10
Operating System BCS303
To implement the banker's algorithm the following data structures are used.
Available: A vector of length m indicates the number of available resources of each type. If
available [j] = k, there are k instances of resource type Rj available.
Max: An n x m matrix defines the maximum demand of each process. If Max [i,j] = k, then
process Pi may request at most k instances of resource type Rj
Allocation: An 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: An 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.
Safety Algorithm
The algorithm for finding out whether or not a system is in a safe state. This algorithm can be
described as follows:
4. If Finish [i] == true for all i, then the system is in a safe state
This algorithm may require an order of m x n2 operations to determine whether a state is safe.
11
Operating System BCS303
Resource-Request Algorithm
1. If RequestiNeedigo to step 2. Otherwise, raise error condition, since process has exceeded
its maximum claim
2. If RequestiAvailable, go to step 3. Otherwise Pi must wait, since resources are not available
3. Have the system pretend to allocate requested resources to Pi by modifying the state as
follows:
Available = Available – Request;
Allocationi= Allocationi + Requesti;
Needi=Needi – Requesti;
Example
Consider a system with five processes Po through P4 and three resource types A, B, and C.
Resource type A has ten instances, resource type B has five instances, and resource type C has
seven instances. Suppose that, at time T0the following snapshot of the system has been taken:
12
Operating System BCS303
The system is currently in a safe state. Indeed, the sequence <P1, P3, P4, P2, P0> satisfies the
safety criteria.
Suppose now that process P1 requests one additional instance of resource type A and two
instances of resource type C, so Request1 = (1,0,2). Decide whether this request can be
immediately granted.
Then pretend that this request has been fulfilled, and the following new state is arrived.
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety
requirement.
13
Operating System BCS303
DEADLOCK DETECTION
If a system does not employ either a deadlock-prevention or a deadlock avoidance algorithm,
then a deadlock situation may occur. In this environment, the system may provide:
An algorithm that examines the state of the system to determine whether a deadlock has
occurred
An algorithm to recover from the deadlock
If all resources have only a single instance, then define a deadlock detection algorithm
that uses a variant of the resource-allocation graph, called a wait-for graph.
This graph is obtained from the resource-allocation graph by removing the resource
nodes and collapsing the appropriate edges.
An edge from Pi to Pj in a wait-for graph implies that process Pi is waiting for process Pj
to release a resource that Pi needs. An edge Pi → Pj exists in a wait-for graph if and only
if the corresponding resource allocation graph contains two edges Pi →Rq and Rq→Pi for
some resource Rq.
Example: In below Figure, a resource-allocation graph and the corresponding wait-for graph is
presented.
A deadlock exists in the system if and only if the wait-for graph contains a cycle. To
detect deadlocks, the system needs to maintain the wait-for graph and periodically
invoke an algorithm that searches for a cycle in the graph.
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.
14
Operating System BCS303
A deadlock detection algorithm that is applicable to several instances of a resource type. The
algorithm employs several time-varying data structures that are similar to those used in the
banker's algorithm.
Algorithm:
4. If Finish[i] == false, for some i, 1 in, 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
Consider a system with five processes Po through P4 and three resource types A, B, and C.
Resource type A has seven instances, resource type B has two instances, and resource type C
has six instances. Suppose that, at time T0, the following resource-allocation state:
15
Operating System BCS303
After executing the algorithm, Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for
all i
Suppose now that process P2 makes one additional request for an instance of type C. The
Request matrix is modified as follows:
The system is now deadlocked. Although we can reclaim the resources held by process Po, the
number of available resources is not sufficient to fulfill the requests of the other processes.
Thus, a deadlock exists, consisting of processes P1, P2, P3, and P4.
Detection-Algorithm Usage
If deadlocks occur frequently, then the detection algorithm should be invoked frequently.
Resources allocated to deadlocked processes will be idle until the deadlock can be broken.
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.
16
Operating System BCS303
The system recovers from the deadlock automatically. There are two options for breaking a
deadlock one is simply to abort one or more processes to break the circular wait. The other is to
preempt some resources from one or more of the deadlocked processes.
Process Termination
To eliminate deadlocks by aborting a process, use one of two methods. In both methods, the
system reclaims all resources allocated to the terminated processes.
1. Abort all deadlocked processes: This method clearly will break the deadlock cycle, but
at great expense; the deadlocked processes may have computed for a long time, and the
results of these partial computations must be discarded and probably will have to be
recomputed later.
2. Abort one process at a time until the deadlock cycle is eliminated: This method
incurs considerable overhead, since after each process is aborted, a deadlock-detection
algorithm must be invoked to determine whether any processes are still deadlocked.
If the partial termination method is used, then we must determine which deadlocked process (or
processes) should be terminated. Many factors may affect which process is chosen, including:
Resource Preemption
17
Operating System BCS303
3. Starvation. How do we ensure that starvation will not occur? That is, how can we
guarantee that resources will not always be preempted from the same process?
18