2. Methods for handling deadlock
There are three ways to handle deadlock
1) Deadlock prevention or avoidance:
The idea is to not let the system into deadlock state.
One can zoom into each category individually, Prevention
is done by negating one of above mentioned necessary
conditions for deadlock.
Avoidance is kind of futuristic in nature. By using strategy
of “Avoidance”, we have to make an assumption. We need
to ensure that all information about resources which
process WILL need are known to us prior to execution of
the process.
2) Deadlock detection and recovery: Let deadlock occur,
then do preemption to handle it once occurred.
3) Ignore the problem all together: If deadlock is very
rare, then let it happen and reboot the system. This is the
approach that both Windows and UNIX take.
3. Deadlock Detection
A deadlock can be detected by a resource scheduler as it keeps
track of all the resources that are allocated to different
processes. After a deadlock is detected, it can be resolved using
the following methods.
All the processes that are involved in the deadlock are
terminated. This is not a good approach as all the progress
made by the processes is destroyed.
Resources can be preempted from some processes and given to
others till the deadlock is resolved.
If resources have single instance:
In this case for Deadlock detection we can run an algorithm to
check for cycle in the Resource Allocation Graph. Presence of
cycle in the graph is the sufficient condition for deadlock.
In the diagram, resource 1 and resource 2 have single instances.
There is a cycle R1 → P1 → R2 → P2. So, Deadlock is Confirmed.
4. Deadlock Detection
If there are multiple instances of resources:
Detection of the cycle is necessary but not sufficient
condition for deadlock detection, in this case, the
system may or may not be in deadlock varies
according to different situations.
5. Deadlock Recovery
A traditional operating system such as Windows doesn’t deal
with deadlock recovery as it is time and space consuming
process. Real-time operating systems use Deadlock recovery.
Recovery method
Killing the process: killing all the process involved in the
deadlock. Killing process one by one. After killing each process
check for deadlock again keep repeating the process till
system recover from deadlock.
Resource Preemption: Resources are preempted from the
processes involved in the deadlock, preempted resources are
allocated to other processes so that there is a possibility of
recovering the system from deadlock. In this case, the system
goes into starvation.
6. Deadlock Avoidance
Deadlock avoidance merely works to avoid deadlock; it does not totally
prevent it. The basic idea here is to allocate resources only if the resulting
global state is a safe state.
In other words, unsafe states are avoided, meaning that deadlock is avoided
as well.
• When a process requests a resource, even if it is available, it is not
immediately allocated to the process. Instead the
systems assumes (pretends) it is so allocated.
• With this assumption, along with advance knowledge of all the resources
needed for all the processes, the system performs some analysis to
decide whether or not granting the request is safe.
• If the request is safe, the resource is granted to the requesting process.
Otherwise, the resource is not given to the process at this time.
This type of algorithm requires each site to have access to a global state
(requiring too much storage and communication). Also the checking of the
involved data structures must be done in a mutually exclusive fashion. With
many sites and resources, it just takes too long.
7. Deadlock avoidance can be done with Banker’s Algorithm.
Banker’s Algorithm
Bankers’s Algorithm is resource allocation and deadlock avoidance
algorithm which test all the request made by processes for resources, it
checks for the safe state, if after granting request system remains in the
safe state it allows the request and if there is no safe state it doesn’t allow
the request made by the process.
Inputs to Banker’s Algorithm:
• Max need of resources by each process.
• Currently allocated resources by each process.
• Max free available resources in the system.
The request will only be granted under the below condition:
• If the request made by the process is less than equal to max need to that
process.
• If the request made by the process is less than equal to the freely
available resource in the system.
8. Deadlock Prevention
It is very important to prevent a deadlock before it
can occur. So, the system checks each transaction
before it is executed to make sure it does not lead to
deadlock. If there is even a slight chance that a
transaction may lead to deadlock in the future, it is
never allowed to execute
9. Deadlock Prevention
This strategy involves designing a system that
violates one of the four necessary conditions
required for the occurrence of deadlock.
This ensures that the system remains free from the
deadlock.
The various conditions of deadlock occurrence may
be violated as-
• Eliminate Mutual Exclusion
• Eliminate Hold and Wait
• Eliminate No Pre-emption
• Eliminate Circular Wait
10. Deadlock Prevention
1- Eliminate 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 can not be violated.
11. Deadlock Prevention
2- Eliminate Hold and Wait
This condition can be violated in the following ways-
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.
12. 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 start 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.
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.
13. Deadlock Prevention
3-Eliminate No Pre-emption
This condition can by violated by forceful pre-emption.
• Consider a process is holding some resources and
request other resources that can not be immediately
allocated to it.
• Then, by forcefully pre-empting the currently held
resources, the condition can be violated.
• A process is allowed to forcefully pre-empt 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.
14. Deadlock Prevention
4-Eliminate 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-
Approach
A numerical 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.