Unit - 3 Part II
Unit - 3 Part II
Deadlock is a situation where a set of processes are blocked because each process is holding a
resource and waiting for another resource acquired by some other process.
In this situation, none of the process gets executed since the resource it needs, is held by some
other process which is also waiting for some other resource to be released.
Deadlock is a common problem in multi-processing where several processes share a specific type
of mutually exclusive resource.
Every process needs some resources to complete its execution. However, the resource is granted in
a sequential order.
The process requests for some resource.
Resources cannot be taken from the process because resources can be released only voluntarily by
In the diagram below, Process 2 cannot preempt Resource 1 from Process 1. It will only be
All the processes must be waiting for the resources in a cyclic manner so that the last process is
waiting for the resource which is being held by the first process.
For example: Process 1 is allocated Resource2 and it is requesting Resource 1. Similarly, Process 2
is allocated Resource 1 and it is requesting Resource 2. This forms a circular wait loop.
Resource-Allocation Graph(RAG)
A set of vertices V and a set of edges E.
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
Pi requests instance of Rj
Pi is holding an instance of Rj
Example
Resource Allocation Graph With A Deadlock
Graph With A Cycle But No Deadlock
Deadlock handling Strategies
Deadlock prevention
Deadlock happens only when Mutual Exclusion, hold and wait, No preemption and circular
wait holds simultaneously.
If it is possible to violate one of the four conditions at any time then the deadlock can never
occur in the system.
Deadlock avoidance
In deadlock avoidance, the operating system checks whether the system is in safe state or in
unsafe state at every step which the operating system performs.
The process continues until the system is in safe state. Once the system moves to unsafe state,
the OS has to backtrack one step.
Deadlock detection and recovery
This approach let the processes fall in deadlock and then periodically check whether deadlock
occur in the system or not.
If it occurs then it applies some of the recovery methods to the system to get rid of deadlock
Deadlock prevention
Deadlocks can be prevented by preventing at least one of the four required conditions:
Mutual Exclusion:
Mutual Exclusion from the resource point of view is the fact that a resource can never be used by
more than one process simultaneously
If a resource could have been used by more than one process at the same time then the process would
have never been waiting for any resource.
if we can be able to violate resources behaving in the mutually exclusive manner then the
deadlock can be prevented.
Unfortunately some resources, such as printers and tape drives, require exclusive access by a single
process.
Hold and Wait:
Hold and wait condition lies when a process holds a resource and waiting for some other resource to complete its task.
we have to find out some mechanism by which a process either doesn't hold any resource or doesn't wait.
That means, a process must be assigned all the necessary resources before the execution starts.
A process must not wait for any resource once the execution has been started.
This can be wasteful of system resources if a process needs one resource early in its execution and doesn't need some
other resource until much later.
Require that processes holding resources must release them before requesting new resources, and then re-acquire the
released resources along with the new ones in a single new request.
This can be a problem if a process has partially completed an operation using a resource and then fails to get it re-
allocated after releasing it.
Either of the methods described above can lead to starvation if a process requires one or more popular resources.
No Preemption:
Deadlock arises due to the fact that a process can't be stopped once it starts.
However, if we take the resource away from the process which is causing deadlock then we
can prevent deadlock.
To ensure that this 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.
This is not a good approach at all since if we take a resource away which is being used by the
process then all the work which it has done till now can become inconsistent.
Circular Wait:
One way to avoid circular wait is to number all resources, and to require that processes request
In other words, in order to request resource Rj, a process must first release all Ri such that i >= j.
One big challenge in this scheme is determining the relative ordering of the different resources
Among all the methods, violating Circular wait is the only approach that can be implemented
practically.
Deadlock Avoidance
The deadlock-avoidance algorithm helps you to dynamically assess the resource-allocation state so
that there can never be a circular-wait situation.
assume that the request is granted, – update the system state accordingly,
Resource-allocation state is defined by the number of available and allocated resources, and the
maximum demands of the processes
Banker's Algorithm FOR Deadlock Avoidance
Advantages
Following are the essential characteristics of the Banker's algorithm:
• It contains various resources that meet the requirements of each process.
• Each process should provide information to the operating system for
upcoming resource requests, the number of resources, and how long the
resources will be held.
• It helps the operating system manage and control process requests for each
type of resource in the computer system.
• The algorithm has a Max resource attribute that represents each process can
hold the maximum number of resources in a system.
Banker's Algorithm FOR Deadlock Avoidance
Disadvantages
• It requires a fixed number of processes, and no additional processes
can be started in the system while executing the process.
• The algorithm does no longer allows the processes to exchange its
maximum needs while processing its tasks.
• Each process has to know and state their maximum resource
requirement in advance for the system.
Banker's Algorithm for Deadlock Avoidance
• Available: It is an array of length 'm' that defines each type of resource available in the system.
When Available[j] = K, means that 'K' instances of Resources type R[j] are available in the system.
• Max: It is a [n x m] matrix that indicates each process P[i] can store the maximum number of
resources R[j] (each type) in a system.
• Allocation: It is a matrix of m x n orders that indicates the type of resources currently allocated to
each process in the system. When Allocation [i, j] = K, it means that process P[i] is currently
allocated K instances of Resources type R[j] in the system.
• Need: It is an M x N matrix sequence representing the number of remaining resources for each
process. When the Need[i] [j] = k, then process P[i] may require K more instances of resources
type Rj to complete the assigned work.
Nedd[i][j] = Max[i][j] - Allocation[i][j].
• Finish: It is the vector of the order m. It includes a Boolean value (true/false) indicating whether
the process has been allocated to the requested resources, and all resources have been released
after finishing its task.
The Banker's Algorithm is the combination of the safety algorithm and the resource request
algorithm to control the processes and avoid deadlock in a system:
Safety Algorithm
It is a safety algorithm used to check whether or not a system is in a safe state or follows the safe
sequence in a banker's algorithm:
1. There are two vectors Work and Finish of length m and n in a safety algorithm.
Initialize: Work = Available
Finish[i] = false; for I = 0, 1, 2, 3, 4… n - 1.
2. Check the availability status for each type of resources [i], such as:
Need[i] <= Work
Finish[i] == false
If the i does not exist, go to step 4.
3. Work = Work +Allocation(i) // to get new resource allocation
Finish[i] = true
Go to step 2 to check the status of resource availability for the next process.
4. If Finish[i] == true; it means that the system is safe for all processes.
Resource Request Algorithm
A resource request algorithm checks how a system will behave when a process
makes each type of resource request in a system as a request matrix.
Let create a resource request array R[i] for each process P[i]. If the Resource
Requesti [j] equal to 'K', which means the process P[i] requires 'k' instances of
Resources type R[j] in the system.
Apply the Banker’s Safty Algorithm, When the resource allocation state is safe, its
resources are allocated to the process P(i). And if the new state is unsafe, the
Process P (i) has to wait for each type of Request R(i) and restore the old
resource-allocation state.
Deadlock Detection
The OS periodically checks if there is any existing deadlock in the system and take measures to
remove the deadlocks.
In the above wait-for graph P1 is waiting for resource currently in use by P2 and P2 is waiting for
resource currently in use by P3 and so on… P5 is waiting for resource currently in use by P2.
Available
Vector of length m
Allocation
Request
Inform the system operator, and allow him/her to take manual intervention.
Preempt resources.
Process Termination:
the deadlocked processes may have computed for a long time, and the results of these partial
computations must be discarded
How long the process has computed and how much longer the process will compute before
How many and what types of resources the process has used (for example, whether the resources