Unit 3.2 Deadlock
Unit 3.2 Deadlock
A deadlock occurs when every member of a set of processes is waiting for an event
that can only be caused by a member of the set. Deadlock is a situation that occurs in the
Operating System when any Process enters a waiting state because another waiting process is
holding the demanded resource. Deadlock is a common problem in multi-processing where
several processes share a specific type of mutually exclusive resource known as a soft lock or
software.
example consider two processes A and B that each want to print a file currently on tape.
1. A has obtained ownership of the printer and will release it after printing one file.
2. B has obtained ownership of the tape drive and will release it after reading one file.
3. A tries to get ownership of the tape drive, but is told to wait for B to release it.
4. B tries to get ownership of the printer, but is told to wait for A to release the printer.
Deadlock Characteristics/Conditions
1. Mutual exclusion
2. Hold and wait
3. No preemption
4. Circular wait.
Example: Only allowing traffic from one direction, will exclude the possibility
of blocking the road.
2. Deadlock avoidance: The Operating system runs an algorithm on requests to check for a
safe state. Any request that may result in a deadlock is not granted.
Example: Checking each car and not allowing any car that can block the road. If there is
already traffic on the road, then a car coming from the opposite direction can cause
blockage.
3. Deadlock detection & recovery: OS detects deadlock by regularly checking the system
state, and recovers to a safe state using recovery techniques. Example: Unblocking the
road by backing cars from one side. Deadlock prevention and deadlock avoidance are
carried out before deadlock occurs.
Resources
The resource is the object granted to a process.
Resource Acquisition
• Prevents Hangs:
Detects and resolves deadlocks, preventing system freezes.
• Optimizes Allocation:
Identifies inefficient resource usage and reallocates for efficiency.
• Dynamic Allocation:
Responds to real-time demands, improving responsiveness.
• Minimizes Impact:
Reduces disruptions, enhancing user experience.
• Predictable Behavior:
Enables better management and predictability.
• Monitors Health:
Provides insights, aids performance optimization.
• Reclaims Resources:
Frees locked resources, increasing availability.
Limitations of Deadlock Detection:
• Detection Overhead:
Adds computational burden due to continuous monitoring.
• Delayed Resolution:
Only responds after deadlock occurs, not preventing it.
• Resource Usage:
Consumes additional system resources for monitoring.
• Complexity:
Requires careful design and tuning, adding complexity.
• Potential False Positives:
Can detect non-deadlock situations as deadlocks.
Deadlock Avoidance in OS
Deadlock Avoidance is used by the Operating System to Avoid Deadlock in the System. The
processes need to specify the maximum resources needed by the Operating System so that the
Operating System can simulate the allocation of available resources to the requesting processes
and check if it is possible to satisfy the need of all the processes' requirements.
But how can an Operating System avoid Deadlock?
The operating system avoids Deadlock by knowing the maximum resource requirements of the
processes initially, and also, the Operating System knows the free resources available at that
time. The operating system tries to allocate the resources according to the process requirements
and checks if the allocation can lead to a safe state or an unsafe state. If the resource allocation
leads to an unsafe state, then the Operating System does not proceed further with the allocation
sequence.
Let's understand the working of Deadlock Avoidance with the help of an intuitive example.
Let's consider three processes P1, P2, P3. Some more information on which the processes tell the
Operating System are :
But only 2 resources are free now. Can P1, P2, and P3 satisfy their requirements? Let's try to
find out.
As only 2 resources are free for now, only P3 can satisfy its need for 2 resources. If P3 takes 2
resources and completes its execution, then P3 can release its 3 (1+2) resources. Now the three
free resources that P3 released can satisfy the need of P2. Now, P2 after taking the three free
resources, can complete its execution and then release 5 (2+3) resources. Now five resources are
free. P1 can now take 4 out of the 5 free resources and complete its execution. So, with 2 free
resources available initially, all the processes were able to complete their execution leading to
a Safe State. The order of execution of the processes was <P3, P2, P1>.
What if initially there was only 1 free resource available? None of the processes would be able to
complete its execution. Thus leading to an unsafe state.
We use two words, safe and unsafe states. What are those states? Let's understand these
concepts.
So, If the Operating System is able to allocate or satisfy the maximum resource
requirements of all the processes in any order then the system is said to be in Safe State.
Unsafe State - If the Operating System is not able to prevent Processes from requesting
resources which can also lead to a Deadlock, then the System is said to be in an Unsafe State.
Unsafe State does not necessarily cause deadlock it may or may not cause deadlock.
So, in the above diagram shows the three states of the System. An unsafe state does not always
cause a Deadlock. Some unsafe states can lead to a Deadlock, as shown in the diagram.
Process R1 R2 R3 R4
P1 1 2 3 1
P2 2 1 0 2
P3 2 0 1 0
R1 R2 R3 R4
7 4 5 4
We can find out the no of available resources for each of P1, P2, P3, P4 by subtracting the
currently allocated resources from total resources.
R1 R2 R3 R4
2 1 1 1
Now, The need for the resources for the processes can be calculated by :
Process R1 R2 R3 R4
P1 2 1 0 1
P2 0 2 1 2
P3 1 1 4 0
The available free resources are <2,1,1,1> of resources of R1, R2, R3, and R4 respectively, which
can be used to satisfy only the requirements of process P1 only initially as process P2 requires
2 R2 resources which are not available. The same is the case with Process P3, which requires
4 R3 resources which is not available initially.
1. Firstly, Process P1 will take the available resources and satisfy its resource need, complete
its execution and then release all its allocated resources. Process P1 is initially
allocated <1,2,3,1> resources of R1, R2, R3, and R4 respectively. Process P1 needs
<2,1,0,1> resources of R1, R2, R3 and R4 respectively to complete its execution. So,
process P1 takes the available free resources <2,1,1,1> resources
of R1, R2, R3, R4 respectively and can complete its execution and then release its current
allocated resources and also the free resources it used to complete its execution. Thus P1
releases <1+2,2+1,3+1,1+1> = <3,3,4,2> resources of R1, R2, R3, and R4 respectively.
2. After step 1 now, available resources are now <3,3,4,2>, which can satisfy the need of
Process P2 as well as process P3. After process P2 uses the available Resources and
completes its execution, the available resources are now <5,4,4,4>.
3. Now, the available resources are <5,4,4,4>, and the only Process left for execution is
Process P3, which requires <1,1,4,0> resources each of R1, R2, R3, and R4. So it can
easily use the available resources and complete its execution. After P3 is executed, the
resources available are <7,4,5,4>, which is equal to the maximum resources or total
resources available in the System.
So, the process execution sequence in the above example was <P1, P2, P3>. But it could also
have been <P1, P3, P2> if process P3 would have been executed before process P2, which was
possible as there were sufficient resources available to satisfy the need of both
Process P2 and P3 after step 1 above.
1. During the time of Processing, this algorithm does not permit a process to change its maximum
need.
2. Another disadvantage of this algorithm is that all the processes must know in advance about the
maximum resource needs.
3. This algorithm permits the requests to be provided in constrained time, but for one year which is a
fixed period.
Example:
Let us consider the following snapshot for understanding the banker's algorithm:
Allocation Max Available
Processes
ABC ABC ABC
P1 212 322
P2 401 902
P3 020 753
P4 112 112
Solution:
1. The Content of the need matrix can be calculated by using the formula given below:
Safe sequence:
Available = (2, 1, 0)
Available = (2, 1, 0)
Need <= Available = True
Request of P1 is granted.
= (2, 1, 0) + (2, 1, 2)
Available = (4, 2, 2)
Available = (4, 2, 2)
Available = (4, 2, 2)
Request of P4 is granted.
= (4, 2, 2) + (1, 1, 2)
Available = (5, 3, 4)
Request of P2 is granted.
Available = Available + Allocation
= (5, 3, 4) + (4, 0, 1)
Available = (9, 3, 5)
= Available (9, 5, 5)
The system allocates all the needed resources to each process. So, we can say that the system is in a
safe state.
= [8 5 7] + [2 1 0] = [10 6 7]
Deadlock Ignorance
Ostrich Algorithm
Stick your head in the sand and pretend there is no problem at all, this method of
solving any problem is called Ostrich Algorithm. This Ostrich algorithm is the most
widely used technique in order to ignore the deadlock and also it used for all the single
end-users uses. If there is deadlock in the system, then the OS will reboot the system
in order to function well. The method of solving any problem varies according to the
people.
Scientists all over the world believe that the most efficient method to deal with deadlock is deadlock prevention.
But the Engineers that deal with the system believe that deadlock prevention should be paid less attention as there
are very less chances for deadlock occurrence.
System failure, compiler error, programming bugs, hardware crashes that occur once a week should be paid more
attention rather than deadlock problem that occur once in years. Therefore, most of the engineers don’t pay much
amount in eliminating the deadlock.
Many operating systems suffers from deadlock that are not even detected and then automatically broke. Just as an
explanation we know that the number of processes is determined by the process table. Now as we know there are
only finite number of slots in the process table and hence when the table is full the fork fails. Now the reasonable
approach for the new fork has to wait and try again when the slot in the process table is empty.
Including UNIX and WINDOWS, all the operating system ignore the deadlock first assuming
that the user is restricted to one process. The deadlock ignorance comes into picture frequently
because by this method deadlock is eliminated for free rather than spending much on other
deadlock prevention methods also putting some inconvenient restrictions on process. Thus we
have to decide between the correctness and convenience between the different methods of
solving the deadlock.
Ignoring the possibility of deadlock in operating systems can have both advantages and
disadvantages.
Advantages:
1. Simplicity: Ignoring the possibility of deadlock can make the design and implementation of
the operating system simpler and less complex.
2. Performance: Avoiding deadlock detection and recovery mechanisms can improve the
performance of the system, as these mechanisms can consume significant system resources.
Disadvantages:
1. Unpredictability: Ignoring deadlock can lead to unpredictable behavior, making the system
less reliable and stable.
2. System crashes: If a deadlock does occur and the system is not prepared to handle it, it can
cause the entire system to crash, resulting in data loss and other problems.
3. Reduced availability: Deadlocks can cause processes to become blocked, which can reduce
the availability of the system and impact user experience.
In general, the disadvantages of ignoring deadlock outweigh the advantages. It is important
to implement effective deadlock prevention and recovery mechanisms in operating systems
to ensure the stability, reliability, and availability of the system.
Here OS does not need to do any additional tasks as it does in deadlock avoidance by running
an algorithm on requests checking for the possibility of deadlock.
A mutual exclusion means that un-shareable resources cannot be accessed simultaneously by processes.
Shared resources do not cause deadlock but some resources can't be shared among processes,
leading to a deadlock.
For Example: read operation on a file can be done simultaneously by multiple processes, but
write operation cannot. Write operation requires sequential access, so, some processes have to
wait while another process is doing a write operation.
It is not possible to eliminate mutual exclusion, as some resources are inherently non-shareable,
For Example Tape drive, as only one process can access data from a Tape drive at a time.
For other resources like printers, we can use a technique called Spooling.
A Printer has associated memory which can be used as a spooler directory (memory that is used
to store files that are to be printed next).
In spooling, when multiple processes request the printer, their jobs ( instructions of the processes
that require printer access) are added to the queue in the spooler directory.
The printer is allocated to jobs on a first come first serve (FCFS) basis. In this way, the process
does not have to wait for the printer and it continues its work after adding its job to the queue.
We can understand the workings of the Spooler directory better with the diagram given below:
Eliminate Hold and Wait: Allocate all required resources to the process before the
start of its execution, this way hold and wait condition is eliminated but it will lead to
low device utilization. for example, if a process requires a printer at a later time and
we have allocated a printer before the start of its execution printer will remain blocked
till it has completed its execution. The process will make a new request for resources
after releasing the current set of resources. This solution may lead to starvation.
Eliminate No Preemption: Preempt resources from the process when resources are
required by other high-priority processes.
Eliminate Circular Wait: Each resource will be assigned a numerical number. A
process can request the resources to increase/decrease order of numbering. For
Example, if the P1 process is allocated R5 resources, now next time if P1 asks for R4,
R3 lesser than R5 such a request will not be granted, only a request for resources more
than R5 will be granted.
Deadlocks – Detection and Recovery
Deadlock Detection
The system maintains a resource allocation graph for detecting deadlocks. The wait-
for graph is a variant of the resource-allocation graph.To detect deadlocks from the
RAG or wait-for graph, it is required to periodically invoke an algorithm that searches
for a cycle in the wait-for graph.
✓ If there is a cycle in the wait-for graph, then it means that there is a deadlock in
the system.
✓ If there are no cycles, then it means that there is no deadlock in the system.
Note: The disadvantage of this method is that it is not suitable for a resource-allocation
system with multiple instances of each resource type.
Deadlock Detection – Several Instances of a Resource Type
In this subsection we learn a deadlock detection algorithm that will detect deadlocks
when there are multiple instances of each resource type.
Let n denote the number of processes and m denote the number of resource types
present in the system.
A B C
2 3 0
In the example shown above, there are three resource types A, B and C. The number
of available instances of resource types A, B and C are 2, 3 and 0 respectively.
ABC
P0 010
P1 200
P2 302
P3 211
In the example shown above, there are three resource types A, B and C and four
processes P0, P1, P2 and P3. The number of instances of each resource type currently
allocated to each process is shown. Process P0 is currently allocated 0 instances of
resource type A, 1 instance of resource type B and 0 instances of resource type C.
Similarly, the current allocation of the other processes P1, P2 and P3 are also shown.
ABC
P0 753
P1 322
P2 902
P3 222
In the example shown above, there are three resource types A, B and C and four
processes P0, P1, P2 and P3. The number of instances of each resource type requested
by each process is shown. Process P0 is requesting 7 instances of resource type A, 5
instances of resource type B and 3 instances of resource type C. Similarly, the request
of the other processes P1, P2 and P3 are also shown.
Example of Detection Algorithm
P1 200 202
P2 303 000
P3 211 100
P4 002 002
P1 200 202
P2 303 000
P3 211 100
P4 002 002
Work = (0,1,0);
Is Request1 ≤ Available? No
Since Request1 is not less than Available, check the next process.
Allocation Request Available
ABC ABC ABC
P1 200 202
P2 303 000
P3 211 100
P4 002 002
Work = (0,1,0);
Is Request2 ≤ Available? Yes
Work = Work + Allocation2 =(0,1,0) + (3,0,3) = (3,1,3) Finish[2] = true , P2 added
to safe sequence < P0, P2>
Allocation Request Available
ABC ABC ABC
P1 200 202
P2 303 000
P3 211 100
P4 002 002
Work = (3,1,3);
Is Request3 ≤ Available? Yes
Work = Work + Allocation3 =(3,1,3) + (2,1,1) = (5,2,4) Finish[3] = true,
P3 added to safe sequence and the safe sequence is now < P0, P2, P3 >
Allocation Request Available
ABC ABC ABC
P1 200 202
P2 303 000
P3 211 100
P4 002 002
Work = (5,2,4);
Is Request4 ≤ Available? Yes
Work = Work + Allocation4 =(5,2,4) + (0,0,2) = (5,2,6) Finish[4] = true,
P4 added to safe sequence and the safe sequence is < P0, P2, P3, P4 >
Now, we check again from the beginning all the other processes that were not added
to the safe sequence.
Allocation Request Available
P1 200 202
P2 303 000
P3 211 100
P4 002 002
Work = (5,2,6);
Is Request1 ≤ Available? Yes
Work = Work + Allocation1 =(5,2,6) + (2,0,0) = (7,2,6) Finish[1] = true,
P1 added to safe sequence and the safe sequence now is < P0, P2, P3, P4, P1 >
Allocation Request Available
P1 200 202
P2 303 000
P3 211 100
P4 002 002
Sequence <P0, P2, P3, P4, P1> now results in Finish[i] = true for all i.
There can be more than one safe sequence, that is there can be correct safe sequences
other than <P0, P2, P3, P1, P4>. We have found one safe sequence. Since there is at
least one safe sequence, the system is in a safe state. There is no deadlock in the system.
Let process P2 now make an additional request for an instance of resource type C.
The Request matrix is changed as shown below, after including the request of an
instance of resource type C by process P2.
Request A B C
P0 000
P1 202
P2 001
P3 100
P4 002
Now, let us check if the system will be in a safe state. The deadlock detection
algorithm is run again.
Allocation Request Available
ABC ABC ABC
P1 200 202
P2 303 001
P3 211 100
P4 002 002
Initially, Work = Available = (0,0,0); Finish[i] = false for i = 0,1,2,3,4 When i = 0,
Check if Request0 ≤ Available? Yes
Work = Work + Allocation0 =(0,0,0) + (0,1,0) = (0,1,0) Finish[0] = true, P0 is added
to safe sequence < P0>
Allocation Request Available
P1 200 202
P2 303 001
P3 211 100
P4 002 002
Since the request of all the processes cannot be allocated, the system is not in a safe
state. Though it possible to reclaim the resources held by process P0, there are
insufficient resources to fulfill other processes’ requests. Thus, a deadlock exists,
consisting of processes P1, P2, P3, and P4.
Recovery from Deadlocks
Recovery Strategies:
• Process Termination:
One way to recover from a deadlock is to terminate one or more of the processes involved
in the deadlock. By releasing the resources held by these terminated processes, the
remaining processes may be able to continue executing. However, this approach should be
used cautiously, as terminating processes could lead to loss of data or incomplete
transactions.
• Resource Preemption:
Resources can be forcibly taken away from one or more processes and allocated to the
waiting processes. This approach can break the circular wait condition and allow the
system to proceed. However, resource preemption can be complex and needs careful
consideration to avoid disrupting the execution of processes.
• Process Rollback:
If a resource is preempted from a process, what to do with the process? The
process cannot continue, because it is missing some needed resource. Therefore,
the process must be rolled back to some safe state and the process must be
restarted from that state. But, the problem is that it is difficult to determine a safe
state. Therefore, a total rollback might have to be done or the process must be
aborted and restarted again.
• Wait-Die and Wound-Wait Schemes:
As mentioned in the Deadlock Detection in OS section, these schemes can also be used for
recovery. Older processes can preempt resources from younger processes (Wound-Wait),
or younger processes can be terminated if they try to access resources held by older
processes (Wait-Die).
• Kill the Deadlock:
In some cases, it might be possible to identify a specific process that is causing the deadlock
and terminate it. This is typically a last resort option, as it directly terminates a process
without any complex recovery mechanisms.
Recovery from Deadlock: Process Termination
When there are many processes involved in the deadlock, it is necessary to choose a
process to terminate first. In which order should we choose a process to abort? There
are many factors based on which the process to be aborted can be chosen:
To recover from deadlocks, resources can be preempted from the deadlocked processes.
When resources are preempted, it is necessary to consider certain issues. The issues are
discussed below:
1. Selecting a victim
It is necessary to select the victim process from which the resources are to be
preempted. The victim process has to be selected such that the cost is minimized.
Cost factors may include the number of resources a deadlock process is holding
Advantages of Deadlock Recovery:
• Resumes Processes:
Terminates deadlock-involved processes, allowing others to continue.
• Reclaims Resources:
Releases resources, improving allocation.
• Minimizes Downtime:
Swiftly resolves deadlocks, reducing system downtime.
• User Transparent:
Shields users from deadlock complexities.
• Ensures Stability:
Prevents prolonged hangs, enhances system stability.
• Optimizes Utilization:
Redistributes resources for efficient use.
• Automated Resolution:
Swift, automated recovery from deadlocks.
• Boosts Fault Tolerance:
Contributes to system reliability and fault tolerance.
• Process Termination:
May terminate processes, affecting user tasks.
• Resource Waste:
Terminated processes release resources, causing waste.
• User Impact:
Interruption to users due to process termination.
• Unfairness:
Selecting processes to terminate may seem arbitrary.
• Automated Risks:
Automated recovery decisions might not be optimal.