Deadlock
Deadlock
UNIT-III
DEADLOCKS
DEADLOCKS
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. The events with which we are mainly
concerned here are resource acquisition and resource release.
SYSTEM MODEL
A system consists of a finite number of resources to be distributed among a number of
competing processes.
Resources are categorized into two types: Physical resources and Logical resources
Physical resources: Printers, Tape drives, DVD drives, memory space and CPU cycles
Logical resources: Semaphores, Mutex locks and files.
Each resource type consists of some number of identical instances. (i.e.) If a system has two
CPU’s then the resource type CPU has two instances.
A process may utilize a resource in the following sequence under normal mode of operation:
1. Request: The process requests the resource. If the resource is being used by another
process then 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.
Example: If the resource is a printer, the process can print on the printer.
3. Release: The process releases the resource.
System calls for requesting and releasing resources:
Device System calls: request( ) and release( )
Semaphore System calls: wait( ), signal( )
Mutex locks: acquire( ), release( ).
Memory System Calls: allocate( ) and free( )
File System calls: open( ), close( ).
A System Table maintains the status of each resource whether the resource is free or
allocated. For each resource that is allocated, the table also records the process to which it is
allocated. If a process requests a resource that is currently allocated to another process, it can
be added to a queue of processes waiting for this resource.
FOUR NECESSARY CONDITIONS OF DEADLOCK
A deadlock situation can arise if the following 4 conditions hold simultaneously in a system:
1. Mutual exclusion. Only one process at a time can use the resource. If other 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. If a process holding a resource and the resource cannot be preempted
until the process has completed its task.
90
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
The resource allocation graph is used for identification of deadlocks in the system.
A System Resource-Allocation Graph G={V,E} is a directed graph that consists of a set of
vertices V and a set of edges E.
The set of vertices V is partitioned into two types of nodes: Processes and Resources.
1. Process set P= {P1, P2, ..., Pn} consisting of all the active processes in the system.
2. Resource set R= {R1, R2, ..., Rm} consisting of all resource types in the system.
The set of Edges E is divided into types: Request Edge and Assignment Edge.
1. Request Edge (Pi → Rj): It signifies that process Pi has requested an instance of
resource type Rj and Pi is currently waiting for the resource Rj.
2. Assignment edge (Rj → Pi): It signifies that an instance of resource type Rj has been
allocated to process Pi.
Processes can be represented in Circles and Resources can be represented in Rectangles.
Instance of resource can be represented by a Dot.
Note:
1. When process Pi requests an instance of resource type Rj, a request edge is inserted in the
Resource-allocation graph.
2. When this request can be fulfilled, the request edge is transformed to an assignment edge.
3. When the process no longer needs access to the resource, it releases the resource and the
assignment edge is deleted.
Resource allocation graph shows three situations:
1. Graph with No deadlock
2. Graph with a cycle and deadlock
3. Graph with a cycle and no deadlock
Resource Allocation Graph without Deadlock
The below graph consists of three sets: Process P, Resources R and Edges E.
91
Consider the above graph, with processes and Resources and have some edges:
P1 → R1 → P2 → R3 → P3 → R2 → P1
P2 → R3 → P3 → R2 → P2
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.
Process P1 is waiting for process P2 to release resource R1.
Hence the Processes P1, P2 and P3 are deadlocked.
Resource Allocation Graph with a Cycle and No Deadlock
93
No Preemption
To ensure that No preemption condition does not hold, we can use the following protocol:
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 (i.e.) resources are implicitly released.
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
resources that it is requesting.
Note: This protocol is often applied to resources whose state can be easily saved and restored
later such as CPU registers and memory space. It cannot be applied to resources such as
mutex locks and semaphores.
Circular Wait
One way to ensure that circular wait 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.
Consider the set of resource types R={R1, R2, ..., Rm} and N be the set of natural numbers.
we define a one-to-one function F: R → N.
The function assigns each resource type to a unique integer number, which allows us to
compare two resources and to determine whether one resource precedes another resource
in our ordering.
Example: If the set of resource types R includes tape drives, disk drives and printers, then the
function F: R → N might be defined as follows:
F (Tape drive) = 1 (F: Tape drive → 1)
F (Disk drive) = 5 (F: Disk drive → 1)
F (Printer) = 12 (F: Printer → 1)
We can 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 Ri.
After that, the process can request instances of resource type Rj iff F(Rj) > F(Ri)
Example: A process that wants to use the tape drive and printer at the same time must
first request the tape drive and then request the printer.
Alternatively, we can require that a process requesting an instance of resource type Rj
must have released any resources Ri such that F(Ri) ≥ F(Rj).
Note: If several instances of the same resource type are needed, a single request for all of
them must be issued.
Disadvantage of Deadlock Prevention
Deadlock-prevention algorithms leads to low resource utilization and the system throughput
will be reduced.
94
DEADLOCK AVOIDANCE
In Deadlock avoidance the processes first informs the operating system about their maximum
allocation of resources to be requested and used during its life time.
With this information, the operating system can decide for each request whether the
resource will be granted immediately or the process should wait for resources.
To take this decision about decision about the resource allocation, the operating system
must consider the resources currently available, the resources currently allocated to each
process and the future requests and releases of each process.
Algorithms for Deadlock Avoidance
A deadlock-avoidance algorithm dynamically examines the Resource-Allocation State to
ensure that a circular-wait condition can never exist.
The Resource Allocation State is defined by the number of available resources and allocated
resources and the maximum demands of the processes.
There are three algorithms are designed for deadlock avoidance:
1. Safe State
2. Resource Allocation Graph Algorithm
3. Bankers Algorithm
Safe State Algorithm
If the system can allocate resources to each process up to its maximum in some order and still
avoid a deadlock then the state is called Safe state.
A system is in a safe state only if there exists a Safe sequence.
A sequence of processes <P1, P2, ..., Pn> is a safe sequence for the current allocation
state, if for each process 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 Pj have finished its task, Pi can obtain all of its needed resources and after
completing its designated task Pi can 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.
Note:
1. A safe state is not a deadlocked state and a deadlocked state is an unsafe state.
2. An unsafe state may lead to a deadlock but not all unsafe states are deadlocks.
3. As long as the state is safe, the operating system can avoid unsafe and deadlocked states.
4. In an unsafe state, operating system cannot prevent processes from requesting resources
in such a way that a deadlock occurs. Behavior of the processes controls unsafe states.
95
Example: Consider a system with 12 magnetic tape drives and 3 processes: P0, P1 and P2.
Process Maximum Needs Current Needs
P0 10 5
P1 4 2
P2 9 2
The above table describes as follows:
Process P0 requires 10 tape drives, P1 needs 4 tape drives and P2 need 9 tape drives.
At time t0, process P0 is holding 5 tape drives, P1 and P2 is holding 2 tape drives each.
Now there are 3 free tape drives.
At time t0, the system is in a safe state. <P1, P0, P2> sequence satisfies the safety condition.
Process P1 can immediately be allocated all its tape drives and then return all 4 resources.
(i.e.) P1 currently holding 2 tape drives and out of 3 free tape drives 2 tape drives will be
given to P1. Now P1 is having all 4 resources. Hence P1 will use all of its resources and
after completing its task P1 releases all 4 resources and then returns to the system. Now
the system is having 5 available tape drives.
Now process P0 needs 5 tape drives and the system has 5 available tape drives. Hence P0
can get all its tape drives and it reaches its maximum 10 tape drives. After completing its
task P0 returns the resources to the system. Now system has 10 available tape drives.
Now the process P2 needs 7 additional resources and system have 10 resources available.
Hence process P2 can get all its tape drives and return them. Now the system will have
all 12 tape drives available.
Problem: Low Resource utilization
If a process requests a resource that is currently available, it may still have to wait. Hence
there exist a low resource utilization is possible.
Resource-Allocation-Graph Algorithm
In this algorithm we use three edges: request edge, assignment edge and a claim edge.
Claim edge Pi → Rj indicates that process Pi may request resource Rj at some time in
the future.
Claim edge resembles a request edge in direction but is represented by 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.
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.
96
97
98
The Available vector can be calculated by subtractring total no of resources from the sum of
resources allocated to each process.
Available resources of A= Total resources of A – Sum of resources allocated to Process P1 to P4
By using the banker’s algorithm we can decide whether the state is safe or not.
After solving the above problem by using bankers algorithm we will get to a safe state with
safe sequence <P1,P3,P4,P0,P2>.
Now we get a safe state, the resources will be granted immediately for requested process P1.
DEADLOCK DETECTION ALGORITHM
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.
Deadlock Detection in Single Instance of Each Resource Type
If all resources have only a single instance then we can define a Deadlock-Detection
algorithm that uses a variant of the resource-allocation graph called a wait-for graph.
We obtain wait-for graph 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 → Pj for some resource Rq .
99
100
Initially the system is not in Deadlock State. If we apply the Deadlock Detection algorithm
we will find the sequence < P0, P2, P3, P1, P4 > results in Finish[i] == true for all i.
The system is in safe state hence there is no deadlock.
RECOVERY FROM DEADLOCK
There are two options for breaking a deadlock.
1. Process termination
2. Resource Preemption
Process Termination
To eliminate deadlocks by aborting a process, we use one of two methods. In both methods,
the system reclaims all resources allocated to the terminated processes.
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.
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.
Many factors may affect which process is chosen for preempting includes:
1. Priority of the process.
2. How long the process has computed and how much longer the process will compute
before completing its designated task.
3. How many and what types of resources the process has used.
4. How many more resources the process needs in order to complete.
5. How many processes will need to be terminated?
6. Whether the process is Interactive or Batch.
Resource Preemption
To eliminate deadlocks using resource preemption, we successively preempt some resources
from processes and give these resources to other processes until the deadlock cycle is broken.
There are 3 issues related to Resource Preemption:
101
102