Deadlocks in OS
CSE 313
Operating Systems
Deadlock
A Deadlock is a situation where each of the computer process waits for a resource
which is being assigned to some another 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.
Let us assume that there are three processes P1, P2 and P3. There are three different
resources R1, R2 and R3. R1 is assigned to P1, R2 is assigned to P2 and R3 is assigned
to P3.
After some time, P1 demands for R1 which is being used by P2. P1 halts its execution
since it can't complete without R2. P2 also demands for R3 which is being used by P3.
P2 also stops its execution because it can't continue without R3. P3 also demands for
R1 which is being used by P1 therefore P3 also stops its execution.
In this scenario, a cycle is being formed among the three processes. None of the
process is progressing and they are all waiting. The computer becomes unresponsive
since all the processes got blocked.
Necessary conditions for Deadlocks:
Mutual Exclusion
A resource can only be shared in mutually exclusive manner. It implies, if two process
cannot use the same resource at the same time.
Hold and Wait
A process waits for some resources while holding another resource at the same time.
No preemption
The process which once scheduled will be executed till the completion. No other
process can be scheduled by the scheduler meanwhile.
Circular Wait
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.
Strategies for handling Deadlock
1. Deadlock Ignorance
Deadlock Ignorance is the most widely used approach among all the mechanism.
This is being used by many operating systems mainly for end user uses. In this
approach, the Operating system assumes that deadlock never occurs. It simply
ignores deadlock. This approach is best suitable for a single end user system where
User uses the system only for browsing and all other normal stuff.
2. 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.
The idea behind the approach is very simple that we have to fail one of the four
conditions but there can be a big argument on its physical implementation in the
system.
3. 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.
In simple words, The OS reviews each allocation so that the allocation doesn't cause
the deadlock in the system.
4. 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
The various conditions of deadlock occurrence may be violated as:
i) Mutual Exclusion
To violate this condition, all the system resources must be such that they can be used in
a shareable mode.
In a system, there are always some resources which are mutually exclusive by nature.
So, this condition cannot be violated.
ii) Hold and Wait
This condition can be violated in the following ways
Deadlock prevention
Approach-01:
In this approach,
A process has to first request for all the resources it requires for execution.
Once it has acquired all the resources, only then it can start its execution.
This approach ensures that the process does not hold some resources and wait for other resources.
Drawbacks
The drawbacks of this approach are:
It is less efficient.
It is not implementable since it is not possible to predict in advance which resources will be
required during execution.
Deadlock prevention
Approach-02:
In this approach,
A process is allowed to acquire the resources it desires at the current
moment.
After acquiring the resources, it starts its execution.
Now before making any new request, it has to compulsorily release all
the resources that it holds currently.
This approach is efficient and implementable.
Deadlock prevention
Approach-03:
In this approach,
A timer is set after the process acquires any resource.
After the timer expires, a process has to compulsorily release the
resource.
Deadlock prevention
iii) No Preemption
This condition can by violated by forceful preemption.
Consider a process is holding some resources and request other
resources that cannot be immediately allocated to it.
Then, by forcefully preempting the currently held resources, the
condition can be violated.
A process is allowed to forcefully preempt the resources possessed by some other process
only if:
It is a high priority process or a system process.
The victim process is in the waiting state.
Deadlock prevention
iv) Circular Wait
This condition can be violated by not allowing the processes to wait for resources in a
cyclic manner. To violate this condition, the following approach is followed:
A natural number is assigned to every resource.
Each process is allowed to request for the resources either in only increasing or only
decreasing order of the resource number.
In case increasing order is followed, if a process requires a lesser number resource,
then it must release all the resources having larger number and vice versa.
This approach is the most practical approach and implementable.
However, this approach may cause starvation but will never lead to deadlock.
Deadlock Avoidance
This strategy involves maintaining a set of data using which a decision
is made whether to entertain the new request or not.
If entertaining the new request causes the system to move in an unsafe
state, then it is discarded.
This strategy requires that every process declares its maximum
requirement of each resource type in the beginning.
The main challenge with this approach is predicting the requirement of
the processes before execution.
Banker’s Algorithm is an example of a deadlock avoidance strategy.
Deadlock Detection and Recovery
Deadlock Detection
In this approach, The OS doesn't apply any mechanism to avoid or
prevent the deadlocks. Therefore, the system considers that the deadlock
will definitely occur. In order to get rid of deadlocks, The OS
periodically checks the system for any deadlock. In case, it finds any of
the deadlock then the OS will recover the system using some recovery
techniques.
The main task of the OS is detecting the deadlocks. The OS can detect
the deadlocks with the help of Resource allocation graph.
In single instanced resource types, if a cycle is being formed in the system, then there will
definitely be a deadlock. On the other hand, in multiple instanced resource type graph, detecting a
cycle is not just enough. We have to apply the safety algorithm on the system by converting the
resource allocation graph into the allocation matrix and request matrix.
Deadlock Detection and Recovery
Deadlock Recovery
• In order to recover the system from deadlocks, either OS considers resources or processes.
For Resource
• Preempt the resource: We can snatch one of the resources from the owner of the resource (process) and
give it to the other process with the expectation that it will complete the execution and will release this
resource sooner. Well, choosing a resource which will be snatched is going to be a bit difficult.
• Rollback to a safe state: System passes through various states to get into the deadlock state. The
operating system canrollback the system to the previous safe state. For this purpose, OS needs to
implement check pointing at every state.
The moment, we get into deadlock, we will rollback all the allocations to get into the previous safe state.
For Process
• Kill a process: Killing a process can solve our problem but the bigger concern is to decide which process
to kill. Generally, Operating system kills a process which has done least amount of work until now.
• Kill all process: This is not a suggestible approach but can be implemented if the problem becomes very
serious. Killing all process will lead to inefficiency in the system because all the processes will execute
again from starting.
Deadlock Ignorance
This strategy involves ignoring the concept of deadlock and assuming as if it does not exist. This
strategy helps to avoid the extra overhead of handling deadlock. Windows and Linux use this
strategy and it is the most widely used method.
It is also called as Ostrich approach.
Deadlock Ignorance is the most widely used approach among all the mechanism. This is being used by
many operating systems mainly for end user uses. In this approach, the Operating system assumes that
deadlock never occurs. It simply ignores deadlock. This approach is best suitable for a single end user
system where User uses the system only for browsing and all other normal stuff.
If deadlock occur then we restart the computer. Operating system has no particular code for it. We
don’t want to affect the performance. Because, if we add particular code for deadlock prevention then
functionality of OS will be increased. As a result, performance of operating system (speed) will be
reduced. As deadlock never occur daily so it is a rare case that’s why we have no need to increase the
functionality of OS.
Banker’s Algorithm | Deadlock Avoidance
We have discussed-
• In a deadlock state, the execution of multiple processes is blocked.
• Deadlock avoidance strategy involves maintaining a set of data.
• The data is used to make a decision whether to entertain any new request or not.
• If entertaining the new request causes the system to move in an unsafe state, then it is discarded.
Banker’s Algorithm
• Banker’s Algorithm is a deadlock avoidance strategy.
• It is called so because it is used in banking systems to decide whether a loan can be granted or not.
Prerequisite
Banker’s Algorithm requires-
• Whenever a new process is created, it specifies the maximum number of instances of each
resource type that it exactly needs.
Banker’s Algorithm
Data Structures Used
• To implement banker’s algorithm, following four data structures are
used:
Banker’s Algorithm
Data Structure Definition Example
It is a single dimensional array that specifies the Available[R1] = K
Available number of instances of each resource type It means K instances of resource type R1 are currently
currently available. available.
It is a two-dimensional array that specifies the Max[P1][R1] = K
Max maximum number of instances of each resource It means process P1 is allowed to ask for maximum K
type that a process can request. instances of resource type R1.
Allocation[P1][R1] = K
It is a two-dimensional array that specifies the
It means K instances of resource type R1 have been
Allocation number of instances of each resource type that has
allocated to the process P1.
been allocated to the process.
It is a two-dimensional array that specifies the Need[P1][R1] = K
Need number of instances of each resource type that a It means process P1 requires K more instances of
process requires for execution. resource type R1 for execution.
Banker’s Algorithm
Working
Banker’s Algorithm is executed whenever any process puts forward the request for
allocating the resources. It involves the following steps:
Step-01:
Banker’s Algorithm checks whether the request made by the process is valid or not.
•If the request is invalid, it aborts the request.
•If the request is valid, it follows step-02.
Valid Request
A request is considered valid if and only if:
The number of requested instances of each resource type is less than the need declared by the process in the
beginning.
Banker’s Algorithm
Step-02:
• Banker’s Algorithm checks if the number of requested instances of each resource type is less
than the number of available instances of each type.
• If the sufficient number of instances are not available, it asks the process to wait longer.
• If the sufficient number of instances are available, it follows step-03.
Step-03:
• Banker’s Algorithm makes an assumption that the requested resources have been allocated to
the process.
• Then, it modifies its data structures accordingly and moves from one state to the other state.
Available = Available - Request(i)
Allocation(i) = Allocation(i) + Request(i)
Need(i) = Need(i) - Request(i)
Banker’s Algorithm
• Now, Banker’s Algorithm follows the safety algorithm to check
whether the resulting state it has entered in is a safe state or not.
• If it is a safe state, then it allocates the requested resources to the
process in actual.
• If it is an unsafe state, then it rollbacks to its previous state and asks
the process to wait longer.
Safe State
A system is said to be in safe state when-
All the processes can be executed in some arbitrary sequence with the available number of
resources.
Practice Problems Based on Banker’s Algorithm
Problem-01:
A single processor system has three resource types X, Y and Z, which are shared by three processes.
There are 5 units of each resource type. Consider the following scenario, where the column alloc
denotes the number of units of each resource type allocated to each process, and the column request
denotes the number of units of each resource type requested by a process in order to complete
execution. Which of these processes will finish LAST?
• P0
• P1
• P2
None of the above since the system is Alloc
in a deadlock Request
X Y Z X Y Z
P0 1 2 1 1 0 3
P1 2 0 1 0 1 2
P2 2 2 1 1 2 0
Solution
According to question-
• Total = [ X Y Z ] = [ 5 5 5 ]
• Total _Alloc = [ X Y Z ] = [5 4 3]
Now,
• Available = Total – Total_Alloc = [ 5 5 5 ] – [5 4 3] = [ 0 1 2 ]
Step-01:
• With the instances available currently, only the requirement of the process P1
can be satisfied.
• So, process P1 is allocated the requested resources.
• It completes its execution and then free up the instances of resources held by it.
Then,
• Available = [ 0 1 2 ] + [ 2 0 1] = [ 2 1 3 ]
Step-02:
• With the instances available currently, only the requirement of the process P0 can be satisfied.
• So, process P0 is allocated the requested resources.
• It completes its execution and then free up the instances of resources held by it.
Then-
• Available = [ 2 1 3 ] + [ 1 2 1 ] = [ 3 3 4 ]
Step-03:
• With the instances available currently, the requirement of the process P2 can be satisfied.
• So, process P2 is allocated the requested resources.
• It completes its execution and then free up the instances of resources held by it.
Then-
• Available = [ 3 3 4 ] + [ 2 2 1 ] = [ 5 5 5 ]
Thus,
• There exists a safe sequence <P1, P0, P2> in which all the processes can be executed.
• So, the system is in a safe state.
• Process P2 will be executed at last.
Problem-02:
Let us consider the following snapshot for understanding the banker's
algorithm:
Allocation Max Available
Processes
ABC ABC ABC
P0 112 433 210
P1 212 322
P2 401 902
P3 020 753
P4 112 112
1. Calculate the content of the need matrix?
2. Check if the system is in a safe state?
3. Determine the total sum of each type of resource?
Solution
1. The Content of the need matrix can be calculated by using the
formula given below:
Need = Max – Allocation
2. Let us now check for the safe state.
Safe sequence:
A) For process P0, Need = (3, 2, 1) and Available = (2, 1, 0)
Need <=Available = False
So, the system will move to the next process.
B) For Process P1, Need = (1, 1, 0) and Available = (2, 1, 0)
Need <= Available = True
Request of P1 is granted.
Available = Available +Allocation = (2, 1, 0) + (2, 1, 2) = (4, 2, 2) (New Available)
C) For Process P2, Need = (5, 0, 1) and Available = (4, 2, 2)
Need <=Available = False
So, the system will move to the next process.
D) For Process P3, Need = (7, 3, 3) and Available = (4, 2, 2)
Need <=Available = False
So, the system will move to the next process.
E) For Process P4, Need = (0, 0, 0) and Available = (4, 2, 2)
Need <= Available = True
Request of P4 is granted.
Available = Available + Allocation = (4, 2, 2) + (1, 1, 2) = (5, 3, 4) now, (New Available)
F) Now again check for Process P0, Need = (3, 2, 1) and Available = (5, 3, 4)
Need <= Available = True
Request of P0 is granted.
Available = Available + Allocation = (5, 3, 4) + (1, 1, 2) = (6, 4, 6) now, (New Available)
G) Now again check for Process P2, Need = (5, 0, 1) and Available = (6, 4, 6)
Need <=Available = True
The request for P2 is granted.
Available = Available +Allocation = (6, 4, 6) + (4, 0, 1) = (10, 4, 7)
H) Now again check for Process P3, Need = (7, 3, 3) and Available = (10, 4, 7)
Need <= Available = True
So, the request will be granted to P3.
Safe sequence: < P1, P4, P0, P2, P3>
The system allocates all the needed resources to each process. So, we can say that the system is in a safe
3. The total amount of resources will be calculated by the following
formula:
The total amount of resources = sum of columns of allocation +
Available = [10 4 7] + [0 2 0] = [10 6 7]
Resource Allocation Graph | Operating System
Resource Allocation Graph (RAG) is a graph that represents the state of a system pictorially.
Components Of RAG
There are two major components of a Resource Allocation Graph:
• Vertices
• Edges
Vertices
Process Vertices
• Process vertices represent the processes.
• They are drawn as a circle by mentioning the name of process inside the circle.
Resource Vertices
• Resource vertices represent the resources.
• Depending on the number of instances that exists in the system, resource vertices may be single instance or multiple
instances.
• They are drawn as a rectangle by mentioning the dots inside the rectangle.
• The number of dots inside the rectangle indicates the number of instances of that resource existing in the system.
Edges
There are two types of edges in a Resource Allocation Graph:
Assign Edges
• Assign edges represent the assignment of resources to the processes.
• They are drawn as an arrow where the head of the arrow points to the
process and tail of the process points to the instance of the resource.
Request Edges
• Request edges represent the waiting state of processes for the resources.
• They are drawn as an arrow where the head of the arrow points to the
instance of the resource and tail of the process points to the process.
• If a process requires ‘n’ instances of a resource type, then ‘n’ assign
edges will be drawn.
Example Of RAG
The following diagram represents a Resource Allocation Graph.
It gives the following information-
There exist three processes in the system namely P1, P2 and P3.
There exist two resources in the system namely R1 and R2.
There exists a single instance of resource R1 and two instances of resource R2.
Process P1 holds one instance of resource R1 and is waiting for an instance of resource R2.
Process P2 holds one instance of resource R2 and is waiting for an instance of resource R1.
Process P3 holds one instance of resource R2 and is not waiting for anything.
Practice Problems Based on Detecting
Deadlock Using RAG
Problem-01:
Consider the resource allocation graph in the figure:
Find if the system is in a deadlock state otherwise find a safe sequence.
Solution
Method-01:
• The given resource allocation graph is single instance with a cycle contained in it.
• Thus, the system is definitely in a deadlock state.
Method-02:
• Using the given resource allocation graph, we have:
Allocation Need
R1 R2 R1 R2
Process P1 1 0 0 1
Process P2 0 1 1 0
Available = [ R1 R2 ] = [ 0 0 ]
Now,
There are no instances available currently and both the processes require a resource to execute.
Therefore, none of the process can be executed and both keeps waiting infinitely.
Thus, the system is in a deadlock state.
Problem 02:
Consider the resource allocation graph in the figure:
Find if the system is in a deadlock state otherwise find a safe sequence.
Solution:
The given resource allocation graph is multi-instance with a cycle
contained in it.
So, the system may or may not be in a deadlock state.
Using the given resource allocation graph, we have:
Allocation Need
R1 R2 R1 R2
Process P1 1 0 0 1
Process P2 0 1 1 0
Process P3 0 1 0 0
Available = [ R1 R2 ] = [ 0
0]
Step-01:
Since process P3 does not need any resource, so it executes.
After execution, process P3 release its resources.
Then,
Available = [ 0 0 ] + [ 0 1 ] = [ 0 1 ]
Step-02:
With the instances available currently, only the requirement of the process P1 can be satisfied.
So, process P1 is allocated the requested resources.
It completes its execution and then free up the instances of resources held by it.
Then-
Available = [ 0 1 ] + [ 1 0 ] = [ 1 1 ]
Step-03:
With the instances available currently, the requirement of the process P2 can be satisfied.
So, process P2 is allocated the requested resources.
It completes its execution and then free up the instances of resources held by it.
Then-
Available = [ 1 1 ] + [ 0 1 ] = [ 1 2 ]
Thus,
There exists a safe sequence P3, P1, P2 in which all the processes can be executed.
So, the system is in a safe state.
Problem 03:
Consider the resource allocation graph in the figure:
Find if the system is in a deadlock state otherwise find a safe sequence.
Solution:
The given resource allocation graph is multi instance with a cycle
contained in it.
So, the system may or may not be in a deadlock state.
Using the given resource allocation graph, we have:
Allocation Need
R1 R2 R3 R1 R2 R3
Process P0 1 0 1 0 1 1
Process P1 1 1 0 1 0 0
Process P2 0 1 0 0 0 1
Process P3 0 1 0 0 2 0
Available = [ R1 R2 R3 ] = [ 0 0 1 ]
Step-01:
With the instances available currently, only the requirement of the process P2 can be satisfied.
So, process P2 is allocated the requested resources.
It completes its execution and then free up the instances of resources held by it.
Then-
Available = [ 0 0 1 ] + [ 0 1 0 ] = [ 0 1 1 ]
Step-02:
With the instances available currently, only the requirement of the process P0 can be satisfied.
So, process P0 is allocated the requested resources.
It completes its execution and then free up the instances of resources held by it.
Then-
Available = [ 0 1 1 ] + [ 1 0 1 ] = [ 1 1 2 ]
Step-03:
With the instances available currently, only the requirement of the process P1 can be satisfied.
So, process P1 is allocated the requested resources.
It completes its execution and then free up the instances of resources held by it.
Then-
Step-04:
With the instances available currently, the requirement of the process P3
can be satisfied.
So, process P3 is allocated the requested resources.
It completes its execution and then free up the instances of resources
held by it.
Then-
Available = [ 2 2 2 ] + [ 0 1 0 ] = [ 2 3 2 ]
Thus,
There exists a safe sequence P2, P0, P1, P3 in which all the processes
can be executed.
So, the system is in a safe state.