Uploaded by Eyasu Desta


UNIT – 1
Review of Basic Concepts of Operating Systems :
Process Deadlocks
Introduction to Deadlocks:
 In a multiprogramming environment, several processes
compete for resources. A situation may arise where a
process is waiting for a resource that is held by other
waiting processes. This situation is called a deadlock
 "A set of processes is in a deadlock state when every
process in the set is waiting for an event that can only
be caused by another process in the set.“
 What is deadlock ?“
when two or more threads waiting for each other to
release lock and get stuck for infinite time , situation is
called deadlock. it will only happen in case of
Deadlock Examples:
Following are some examples of deadlock.
Example 1:
The following figure is of a traffic deadlock. It makes
the nature of deadlock very clear. None of the cars
can pass unless at least one of them backs up or is
Example 2:
When two trains approach each other at a crossing,
both shall come to a full stop and neither will start up
again until the other has gone.
Example 3:
Suppose we have, a printer and a tape drive. Process
A requests for the printer and gets it. Process B
requests tape drive and it is granted to it. Now A asks
for the tape drive but the is denied until B releases it.
At this point, the process B asks for the printer before
the tape drive.
Now both processes are waiting for each other to
release the resource and are blocked. Both processes
will remain in this situation forever. This situation is
called deadlock.
Fundamental Causes of Deadlocks
The following four conditions are necessary for deadlocks
to occur in a computer system
 Exclusive Access – Processes request exclusively access
to resources.
 Wait while hold – Processes hold previously acquired
resources while waiting for additional resources.
 No preemption – A resource cannot be preempted from
a process without aborting the process.
 Circular Wait – There exists a set of blocked processes
involved in a circular wait.
A system is prone to deadlocks only if it is satisfies all of
the above four conditions. Note that in most systems ,
the first three conditions are generally desirable.
Exclusive access is required to maintain the integrity of a
shared resources.
Wait while hold is needed to increase resource availability
–instead of claiming all the required resources at once,
wait while hold allows the acquiring of resources as they
are needed.
No preemption is required to avoid wastage of useful
Wait-For-Graph (WFG)
 The state of the system can be modeled by directed
graph, called a wait for graph (WFG).
 In a WFG , nodes are processes and there is a directed
edge from node P1 to node P2 if P1 is blocked and is
waiting for P2 to release some resource.
 A system is deadlocked if and only if there exists a
directed cycle or knot in the WFG.
 Figure 1 shows a WFG, where process P11 of site 1 has
an edge to process P21 of site 1 and P32 of site 2 is
waiting for a resource which is currently held by
process P21.
 At the same time process P32 is waiting on process
P33 to release a resource.
 If P21 is waiting on process P11, then processes P11, P32
and P21 form a cycle and all the four processes are
involved in a deadlock depending upon the request
Deadlock Handling Strategies
 There are three strategies for handling deadlocks, viz., deadlock
prevention, deadlock avoidance, and deadlock detection.
 Handling of deadlock becomes highly complicated in
distributed systems because no site has accurate knowledge of
the current state of the system and because every inter-site
communication involves a finite and unpredictable delay.
 Deadlock prevention is commonly achieved either by having a
process acquire all the needed resources simultaneously before
it begins executing or by preempting a process which holds the
needed resource.
 This approach is highly inefficient and impractical in
distributed systems.
 In deadlock avoidance approach to distributed
systems, a resource is granted to a process if the
resulting global system state is safe (note that a
global state includes all the processes and
resources of the distributed system).
 However, due to several problems, deadlock
avoidance is impractical in distributed systems.
 Deadlock detection requires examination of the
status of process-resource interactions for
presence of cyclic wait.
 Deadlock detection in distributed systems seems
to be the best approach to handle deadlocks in
distributed systems.
Issues in Deadlock Detection
 Deadlock handling using the approach of
deadlock detection entails addressing two basic
issues: First, detection of existing deadlocks and
second resolution of detected deadlocks.
 Detection of deadlocks involves addressing two
issues: Maintenance of the WFG (wait for graph
)and searching of the WFG for the presence of
cycles (or knots).
 Correctness Criteria:
A deadlock detection
satisfy the following two
algorithm must
(i) Progress (No undetected deadlocks):
 The algorithm must detect all existing deadlocks in
finite time.
 In other words, after all wait-for dependencies for a
deadlock have formed, the algorithm should not wait
for any more events to occur to detect the deadlock.
(ii) Safety (No false deadlocks):
 The algorithm should not report deadlocks which do
not exist (called phantom or false deadlocks).
Models of Deadlocks
Distributed systems allow several kinds of resource requests.
The Single –Unit Request Model
 The single-unit request model is the simplest request model since a
process is restricted to requesting only one unit of a resource at a
 Thus , in this model the outdegree of nodes in the WFG is one. A
deadlock in this model corresponds to a cycle in the wait-forgraph, provided that there is only one unit of every resource in the
 Due to its simplicity , this model has been widely used to model
resource acquisition.
The AND Model
 In the AND model, a process can request for more than
one resource simultaneously and the request is satisfied
only after all the requested resources are granted to the
 The out degree of a node in the WFG for AND model
can be more than 1.
 The presence of a cycle in the WFG indicates a deadlock
in the AND model.
 Since in the single-resource model, a process can have at
most one outstanding request, the AND model is more
general than the single-resource model.
 Consider the example WFG described in the
Figure 1.
P11 has two outstanding resource requests. In case
of the AND model, P11 shall become active from
idle state only after both the resources are granted.
There is a cycle P11->P21->P24->P54->P11 which
corresponds to a deadlock situation.
That is, a process may not be a part of a cycle, it
can still be deadlocked. Consider process P44 in
Figure 1.
It is not a part of any cycle but is still deadlocked
as it is dependent on P24which is deadlocked.
The OR Model
 In the OR model, a process can make a request for
numerous resources simultaneously and the request is
satisfied if any one of the requested resources is
Presence of a cycle in the WFG of an OR model does
not imply a deadlock in the OR model.
Consider example in Figure 1: If all nodes are OR
nodes, then process P11 is not deadlocked because
once process P33 releases its resources, P32 shall
become active as one of its requests is satisfied.
After P32 finishes execution and releases its resources,
process P11 can continue with its processing.
In the OR model, the presence of a knot indicates a
The AND-OR Model
 A generalization of the previous two models (OR
model and AND model) is the AND-OR model.
In the AND-OR model, a request may specify any
combination of and and or in the resource request.
For example, in the AND-OR model, a request for
multiple resources can be of the form x and (y or z).
To detect the presence of deadlocks in such a model,
there is no familiar construct of graph theory using
Since a deadlock is a stable property, a deadlock in the
AND-OR model can be detected by repeated
application of the test for OR-model deadlock.
The P-out-of Q Request Model
 In
the P-out-of-Q request model , a process
simultaneously requests Q resources and remains
blocked until it is granted any P of those resources.
 Note that the AND and the OR request model are
special cases of the P-out –of –Q request model.
 When P=Q , it is the AND request model and When P=1,
it is the OR request model
 Atypical example of the P-out-of-Q request model is a
quorum in quorum-based consensus algorithms.
Model of Resources
A resource is an object that is used by a process. It can be a piece of
hardware such as:
 Tape drive
 Disk drive
 Printer
A resource can be a piece of information such as:
 File
 A record within a file
 A shared variable
 A critical section
A computer typically has many different resources. In some cases, there
maybe many instances of a resource of a given type. A process needing
one of these resources can use any one of them. In other cases there may
be only one instance of a resource.
Types of Resources:
 Resources come in two types: Preemptible and Nonpreemptible.
A preemptible resource is one that can be allocated to a given process for a period of time.
Then it can be allocated to another process. Then it can be reallocated to the first process
without any negative effects. Examples of preemptible resources include:
 Memory
 Buffers
 Array processor
A nonpreemptible resource cannot be taken from one process and given to another
without side effects. One example is a printer. A printer cannot be take away from one
process and given to another process in the middle of a print job. Deadlocks usually
involve nonpreemptible resources. The usual sequence of events that occur as a resource is
used is:
1. Request the resource: One of two things can happen when a resource is requested. The
request can be granted immediately if it is available. The request can be postponed or
blocked until a later time.
2. Use the resource: Once the resource has been acquired, it can be used.
3. Release the resource: When the process no longer needs the resource, it releases it.
Usually it is released as soon as possible.
Deadlock Prevention
 We can prevent deadlocks by ensuring that at
least-one of the four necessary conditions for
deadlock cannot occur, If at least one condition is
not satisfied, a deadlock will not occur
Four Necessary and Sufficient Conditions for
1. Mutual Exclusion:
Mutual exclusion condition must hold for non-sharable resources. For
example, only one process can have access to a printer at a time, otherwise the
output is disturbed. Some resources can be made sharable like a read-only file.
Several processes can be granted read only access to a file without interfering
with each other. However, deadlock cannot be prevented by only denying the
mutual exclusion condition because some resources are intrinsically nonsharable.
2. Hold and Wait:
Deadlock can be prevented by denying the hold and wait precondition. This
can be implemented in two different ways.
1. One approach is that a process requests all .the resources that it needs in one
single request at process startup. The system will not grant any resource in the
list until it can grant all the required resources.
2. A less restrictive approach is to allow a process to request resources only
when it is currently holding no resources. If a process needs a new resource, it
must first release all the resources it has and then put the request. It may
include a request for reallocation of a resource it just released.
3. No Preemption:
Preemption of resources means that we take away resources from
processes when they are waiting for other resources. This could work in
the following ways:
1. As soon as a process requests for a resource that is not available, all of
its held resources are released. The process is now waiting for all of
previously held resources plus the resource that it requested. Suppose a
process holds a tape drive and requests a line printer. If line printer is hot
available, the tape drive is taken away and the process is put into a state
of waiting for both a tape drive and a line printer.
2. When a process requests a resource that is available, it gets it.
Otherwise, the system checks those processes that are holding the
requested resource and may be waiting for some more resources. In this
case, the resource is taken away from the waiting process and allocated to
the requesting process. If this cannot happen, the process has to wait.
During the wait, some other process may get some of its resources.
4. Circular Wait:
 We can prevent deadlock by making circular wait
impossible. We can define an order by which
processes get resources to prevent circular wait. For
example, each resource type is assigned a number.
The processes can only get resources in increasing
order of those resource numbers.
 Suppose tape drive has number 1, disk drive has
number 5 and printer has number 12. A process
wants to read the disk drive and print out the results.
It will first need to allocate the disk drive then the
printer. It will be prevented from doing it in reverse
Deadlock Avoidance
 Deadlock avoidance is a technique used to avoid deadlock. It
requires the information about how different processes would
request different resources. If given several processes and
resources, we can allocate the resources in some order to avoid
the deadlock.
Safe State:
 A state is said to be a safe state if the system may allocate the
required resources to each process up to the maximum required
in a particular sequence, without facing deadlock. In safe state,
deadlock cannot occur. If a deadlock is possible, the system is
said to be in an unsafe state.
Safe State - Example
Consider a system with 24 tape drives and three processes: P0,
P1 and P2. Po may require 20 tape-drives during execution, P1
may require 8, and P2 may require up to 18.
Suppose, Po is holding 10 tape drives, P1 holds 4 and P2 holds
4 tape drives. The system is said to be in a safe state, since
there is a safe sequence that avoids the deadlock.
1. Safe Sequence:
 This sequence implies that P1 can instantly get all of its needed
resources. There are 24 total tape drives. P1 already has 4 and needs
4 more. It can get since there are 6 free tape drives. Once it finishes
executing, it releases all 8 resources. At this point, the system has 10
free tape drives. Now P0 can execute since it requires exactly 10
more drives to finish. After it finishes, the system has a total of 20
free tape drives. These can be allocated to P2 and it can proceed
since it needs 14 more drives to complete its task.
2. Unsafe Sequence:
 Suppose at some time P2 requests 2 more resource to make its
holding resources 6. Now the system is in an unsafe state. The
system has now 4 free drives and can only be allocated to P1. After
P1 returns, it will leave 8 free resources. It is not enough for either
P0 or P1 The system enters a deadlock.
Note: All deadlocks are unsafe, but all unsafes are NOT deadlocks
 2. Resource-Allocation Graph Algorithm:
Resource-allocation graph algorithm is used to avoid deadlock. This algorithm uses the
resource-allocation graph by introducing a new edge called claim edge. The claim edge is
used to indicate that a process may request a resource in future. It is represented by a
dashed line. When the process requests that resource, the dashed line is converted to an
assignment edge i.e. a solid line. It indicates that the resource has been allocated to the
process. After the process releases this resource, the assignment edge is again converted to
claim edge. All claim edges of a process are represented in the graph. It requires the
knowledge of all resources to be used by a process, before the execution of the process starts.
When a process requests a resource, it is allocated only if converting the request edge to an
assignment edge does not form a cycle in the graph. If no cycle is formed, the system is in
safe state and the resource will be allocated. But if there is a cycle, the system will, enter an
unsafe state and the process will wait for the resource.
3. Banker's Algorithm:
Banker's algorithm is less efficient than resource allocation graph algorithm. But it can be
implemented in a system with multiple instances of each resource type. It requires that each
new process should declare the maximum number of instances of each required resource
type. When a process requests certain resources, the system determines whether the
allocation of those resources will leave the system in safe state. If it will, the resources are
allocated. Otherwise the process must wait until some other process releases the resources
The "Banker's algorithm
 Allows the following:
Mutual exclusion
Wait and hold
No preemption
 It prevents the following:
Circular wait
Drawbacks of Banker’s Algorithm
 Processes rarely know in advance how many resources they will
 The number of processes changes as time progresses
 Resources once available can disappear
 The algorithm assumes processes will return their resources within
a reasonable time
 Processes may only get their resources after an arbitrarily long delay
 Practical use is therefore rare!
Deadlock Detection
If the system does not use any of the preventive or avoidance
methods, there is a possibility of deadlock occurrence. If a deadlock
occurs in the system, it must be detected and recovered. The system
needs to provide some methods of deadlock detection and recovery.
These methods are overhead for the system.
1 Single Instance of Each Resource Type:
 If each resource type has only one instance (There is only 1 printer, 1
hard drive, 1 tape drive, etc.), we can use a variant of the resourceallocation graph called a wait-for graph. It is done by removing the
nodes of type resource and collapsing the appropriate edges.
 In this graph, processes are not waiting for resources. They are
waiting for other processes that hold these resources. A cycle in the
graph indicates the occurrence of a deadlock in the system.
 This approach requires that a cycle detection algorithm runs from
time to time to detect deadlock. Algorithms that detect cycles are
O(N2). N is the number of vertices in the graph.
2. Detection-Algorithm Usage:
Detection algorithms need to be executed to detect a deadlock.
The frequency and time when we run such algorithm is
dependent on how often we assume deadlocks occur and how.
many processes they may effect.
If deadlocks may happen often, we run the detection often. If it
affects many processes, we may decide to run it often so that
We could run the algorithm on every resource request.
The deadlocks are rare so it is not very efficient use of resources.
We could run the algorithm from time to time like every hour or
at random times during the system execution lifetime.
Deadlock Characterization
Deadlock occurs if the following four conditions take place simultaneously in a system:
1. Mutual Exclusion:
At least one resource must be held in a non-sharable mode. It means that only one
process at a time can use the resource. If another process requests the 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. 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 P1, P2... Pn} of waiting processes must exist such that P1 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.
 All four conditions must hold for a deadlock to occur.
As an example, consider the traffic deadlock in
the following figure
Consider each section of the street as a resource.
 Mutual exclusion condition applies, since only one
vehicle can be on a section of the street at a time.
 Hold-and-wait condition applies, since each vehicle
is occupying a section of the street, and waiting to
move on to the next section of the street.
 No-preemptive condition applies, since a section of
the street that is a section of the street that is
occupied by a vehicle cannot be taken away from it.
 Circular wait condition applies, since each vehicle is
waiting on the next vehicle to move. That is, each
vehicle in the traffic is waiting for a section of street
held by the next vehicle in the traffic.
"It takes money to make money".
You can't get a job without experience; you can't get experience without a job.
The cause of deadlocks: Each process needing what another process has. This results from sharing
resources such as memory, devices, links.
Under normal operation, a resource allocations proceed like this::
Request a resource (suspend until available if necessary ).
Use the resource.
Release the resource.