Deadlocks
Deadlocks
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.
Example
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 R2 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.
Difference between starvation
and deadlock
Deadlock can arise if the following four conditions hold simultaneously (Necessary
Conditions)
Mutual Exclusion
Hold and Wait
No Preemption
Circular Wait
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.
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.
Deadlock Handling Techniques
There are different deadlock handling methods,
and with using them deadlock problems can be
avoided in operating system. Here, explain each
one:
Deadlock Ignorance
Deadlock prevention
Deadlock avoidence
Dedlock detection and Recovery
Deadlock Ignorance
This strategy involves ignoring the concept of
deadlock and assuming as if it does not exist.
This approach is best suitable for a single end user
system where User uses the system only for browsing
and all other normal stuff.
The ostrich algorithm is a method for dealing with
deadlocks in computer systems that's based on the
idea of "deadlock ignorance". The algorithm ignores
deadlocks, assuming that they won't happen, and
reboots the system if a deadlock does occur. The
ostrich algorithm is used in many operating systems,
including Linux and Windows.
This is done because, in some systems, the
overall cost of handling a deadlock is much higher
than the cost of ignoring it, especially if deadlocks
occur very infrequently.
The ostrich algorithm is based on the behavior of
ostriches, which bury their heads in the ground
during storms to act as if nothing is wrong. The
algorithm is used when the cost of preventing
deadlocks is high, and they don't happen often.
Deadlock prevention
(Prevention is better than cure)
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.
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.
Eliminate Mutual Exclusion: It is not possible
to dis-satisfy the mutual exclusion because
some resources, such as the tape drive and
printer, are inherently non-shareable.
• Eliminate Hold and Wait
Require process to request and be allocated all
its resources before it begins execution, or
allow process to request resources only when
the process has none
Low resource utilization; starvation possible
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.
Deadlock Avoidance
• It is better to avoid a deadlock rather than take measures
after the deadlock has occurred. The wait for graph can be
used for deadlock avoidance. This is however only useful
for smaller databases as it can get quite complex in larger
databases.
• It needs additional information, like how resources should
be used. Deadlock avoidance is the simplest and most useful
model that each process declares the maximum number of
resources of each type that it may need.
• Banker’s Algorithm is an example of a deadlock avoidance
strategy.
Safe State