0% found this document useful (0 votes)
25 views20 pages

Deadlocks

Preventing

Uploaded by

shivansh1479s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views20 pages

Deadlocks

Preventing

Uploaded by

shivansh1479s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Deadlock

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.

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.
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

• When a process requests an available resource,


system must decide if immediate allocation leaves the
system in a safe state
• System is in safe state if there exists a sequence <P1,
P2, …, Pn> of ALL the processes in the systems such
that for each Pi, the resources that Pi can still request
can be satisfied by currently available resources +
resources held by all the Pj, with j < I
That is:
• If Pi resource needs are not immediately
available, then Pi can wait until all Pj have
finished
• When Pj is finished, Pi can obtain needed
resources, execute, return allocated resources,
and terminate
• When Pi terminates, Pi +1 can obtain its
needed resources, and so on
Basic Facts
• If a system is in safe state ⇒ no deadlocks
• If a system is in unsafe state ⇒ possibility of
deadlock
• Avoidance ⇒ ensure that a system will never
enter an unsafe state.
Avoidance algorithms

• Single instance of a resource type => Use a


resource-allocation graph
• Multiple instances of a resource type => Use
the banker’s algorithm
Deadlock Detection and Recovery
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.

You might also like