Deadlock
Deadlock
Deadlock
A situation occurs in operating systems when there are two or more processes
that hold some resources and wait for resources held by other(s). For example,
in the below diagram, Process 1 is holding Resource 1 and waiting for resource 2
which is acquired by process 2, and process 2 is waiting for resource 1.
Examples of Deadlock
There are several examples of deadlock. Someof them are mentioned below.
1. The system has 2 tape drives. P0 and P1 each hold one tape drive and each
needs another one.
2. Semaphores A and B, initialized to 1, P0, and P1 are in deadlock as follows:
● P0 executes wait(A) and preempts.
● P1 executes wait(B).
● Now P0 and P1 enter in deadlock.
P0 P1
wait(A); wait(B)
wait(B); wait(A)
3. Assume the space is available for allocation of 200K bytes, and the following
sequence of events occurs.
P0 P1
In deadlock prevention the aim is to not let full-fill one of the required condition of
the deadlock. This can be done by this method:
(i) Mutual Exclusion
We only use the Lock for the non-share-able resources and if the resource is
share- able (like read only file) then we not use the locks here. That ensure that
in case of share -able resource , multiple process can access it at same time.
Problem- Here the problem is that we can only do it in case of share-able
resources but in case of no-share-able resources like printer , we have to use
Mutual exclusion.
(ii) Hold and Wait
To ensure that Hold and wait never occurs in the system, we must guarantee
that whenever process request for resource , it does not hold any other
resources.
● we can provide the all resources to the process that is required for it’s
execution before starting it’s execution . problem – for example if there
are three resource that is required by a process and we have given all
that resource before starting execution of process then there might be a
situation that initially we required only two resource and after one hour
we want third resources and this will cause starvation for the another
process that wants this resources and in that waiting time that resource
can allocated to other process and complete their execution.
● We can ensure that when a process request for any resources that time
the process does not hold any other resources. Ex- Let there are three
resources DVD, File and Printer . First the process request for DVD and
File for the copying data into the file and let suppose it is going to take 1
hour and after it the process free all resources then again request for
File and Printer to print that file.
(iii) No Preemption
If a process is holding some resource and requestion other resources that are
acquired and these resource are not available immediately then the resources
that current process is holding are preempted. After some time process again
request for the old resources and other required resources to re-start.
For example – Process p1 have resource r1 and requesting for r2 that is hold
by process p2. then process p1 preempt r1 and after some time it try to restart by
requesting both r1 and r2 resources.
Problem – This can cause the Live Lock Problem .
Live Lock : Live lock is the situation where two or more processes
continuously changing their state in response to each other without making any
real progress.
Example:
● suppose there are two processes p1 and p2 and two resources r1 and
r2.
● Now, p1 acquired r1 and need r2 & p2 acquired r2 and need r1.
● so according to above method- Both p1 and p2 detect that they can’t
acquire second resource, so they release resource that they are holding
and then try again.
● continuous cycle- p1 again acquired r1 and requesting to r2 p2 again
acquired r2 and requesting to r1 so there is no overall progress still
process are changing there state as they preempt resources and then
again holding them. This the situation of Live Lock.
(iv) Circular Wait:
To remove the circular wait in system we can give the ordering of resources in
which a process needs to acquire.
Ex: If there are process p1 and p2 and resources r1 and r2 then we can fix the
resource acquiring order like the process first need to acquire resource r1 and
then resource r2. so the process that acquired r1 will be allowed to acquire r2 ,
other process needs to wait until r1 is free.
This is the Deadlock prevention methods but practically only fourth method is
used as all other three condition removal method have some disadvantages with
them .
1. Manual Intervention
When a deadlock is detected, one option is to inform the operator and let them
handle the situation manually. While this approach allows for human judgment
and decision-making, it can be time-consuming and may not be feasible in
large-scale systems.
2. Automatic Recovery
3. Process Termination
4. Resource Preemption
● Selecting a Victim
○ Resource preemption involves choosing which
resources and processes should be preempted to break
the deadlock. The selection order aims to minimize the
overall cost of recovery. Factors considered for victim
selection may include the number of resources held by
a deadlocked process and the amount of time the
process has consumed.
● Rollback
○ If a resource is preempted from a process, the process
cannot continue its normal execution as it lacks the
required resource. Rolling back the process to a safe
state and restarting it is a common approach.
Determining a safe state can be challenging, leading to
the use of total rollback, where the process is aborted
and restarted from scratch.
● Starvation Prevention
○ To prevent resource starvation, it is essential to ensure
that the same process is not always chosen as a victim.
If victim selection is solely based on cost factors, one
process might repeatedly lose its resources and never
complete its designated task. To address this, it is
advisable to limit the number of times a process can be
chosen as a victim, including the number of rollbacks in
the cost factor.