Deadlock in Operating System
Deadlock in Operating System
Every process needs some resources to complete its execution. However, the
resource is granted in a sequential order.
1. The process requests for some resource.
2. OS grant the resource if it is available otherwise let the process waits.
3. The process uses it and release on the completion.
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.
PlayNext
Unmute
Duration 18:10
Loaded: 2.94%
Â
Fullscreen
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.
4 The requested resource is blocked by the other The requested resource is continuousl
process. used by the higher priority processes.
5 Deadlock happens when Mutual exclusion, hold It occurs due to the uncontrolled priority
and wait, No preemption and circular wait resource management.
occurs simultaneously.
Necessary conditions for Deadlocks
1. 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.
2. Hold and Wait
A process waits for some resources while holding another resource at the
same time.
3. No preemption
The process which once scheduled will be executed till the completion. No
other process can be scheduled by the scheduler meanwhile.
4. 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.
There is always a tradeoff between Correctness and performance. The operating
systems like Windows and Linux mainly focus upon performance. However, the
performance of the system decreases if it uses deadlock handling mechanism all the
time if deadlock happens 1 out of 100 times then it is completely unnecessary to use
the deadlock handling mechanism all the time.
In these types of systems, the user has to simply restart the computer in the case of
deadlock. Windows and Linux are mainly using this approach.
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.
We will discuss it later in detail.
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.
We will discuss Deadlock avoidance later in detail.
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
If we simulate deadlock with a table which is standing on its four legs then we can
also simulate four legs with the four conditions which when occurs simultaneously,
cause the deadlock.
However, if we break one of the legs of the table then the table will fall definitely. The
same happens with deadlock, if we can be able to violate one of the four necessary
conditions and don't let them occur together then we can prevent the deadlock.
Let's see how we can prevent each of the conditions.
1. Mutual Exclusion
Mutual section from the resource point of view is the fact that a resource can never
be used by more than one process simultaneously which is fair enough but that is
the main reason behind the deadlock. 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.
However, if we can be able to violate resources behaving in the mutually exclusive
manner then the deadlock can be prevented.
Spooling
For a device like printer, spooling can work. There is a memory associated with the
printer which stores jobs from each of the process into it. Later, Printer collects all
the jobs and print each one of them according to FCFS. By using this mechanism,
the process doesn't have to wait for the printer and it can continue whatever it was
doing. Later, it collects the output when it is produced.
Among all the methods, violating Circular wait is the only approach that can be
implemented practically.
Deadlock avoidance
In deadlock avoidance, the request for any resource will be granted if the resulting
state of the system doesn't cause deadlock in the system. The state of the system
will continuously be checked for safe and unsafe states.
In order to avoid deadlocks, the process must tell OS, the maximum number of
resources a process can request to complete its execution.
The simplest and most useful approach states that the process should declare the
maximum number of resources of each type it may ever need. The Deadlock
avoidance algorithm examines the resource allocations so that there can never be a
circular wait condition.
Safe and Unsafe States
The resource allocation state of a system can be defined by the instances of
available and allocated resources, and the maximum instance of the resources
demanded by the processes.
A state of a system recorded at some random time is shown below.
Resources Assigned
A 3 0 2 2
B 0 0 1 1
C 1 1 1 0
D 2 1 4 0
Resources still needed
A 1 1 0 0
B 0 1 1 2
C 1 2 1 0
D 2 1 1 2
1. E = (7 6 8 4)
2. P = (6 2 8 3)
3. A = (1 4 0 1)
Above tables and vector E, P and A describes the resource allocation state of a
system. There are 4 processes and 4 types of the resources in a system. Table 1
shows the instances of each resource assigned to each process.
Table 2 shows the instances of the resources, each process still needs. Vector E is
the representation of total instances of each resource in the system.
Vector P represents the instances of resources that have been assigned to
processes. Vector A represents the number of resources that are not in use.
A state of the system is called safe if the system can allocate all the resources
requested by all the processes without entering into deadlock.
If the system cannot fulfill the request of all processes then the state of the system is
called unsafe.
The key of Deadlock avoidance approach is when the request is made for resources
then the request must only be approved in the case if the resulting state is also a
safe state.