Professional Documents
Culture Documents
Unit V: Deadlocks
Unit V: Deadlocks
Deadlocks
11/10/2014
ChonuChinnus
ChonuChinnus
11/10/2014
ChonuChinnus
4. If(found)
{
Current_Avail=Current_Avail + Alloci
Marked[i]=true
Save the process no in SafeString[ ]
Goto step 3
}
5. If(Marked[i]==true) for all processes,
then system is in safe state
6. Print SafeString. Otherwise, System is
not in safe state and is in deadlock
11/10/2014
ChonuChinnus
Av=Av-Reqi
1. If(ReqiNeedi)
Alloci=Alloci + Reqi
Needi=Needi Reqi
2. elseif(ReqiAvi)
11/10/2014
ChonuChinnus
Example
Consider a system with the following information and determine whether the
system is in safe state
Total_Res
R1
15
R2
8
Max
Process
11/10/2014
R3
8
Alloc
R1
R2
R3
R1
R2
R3
P1
P2
P3
P4
P5
ChonuChinnus
Deadlock Detection
Due to practical issues, Deadlock Avoidance cannot be implemented.
So, if a system cannot implement Deadlock Prevention or Deadlock Avoidance
then it may lead to deadlock situation.
It is necessary to detect whether there is a deadlock in the system.
If there is a deadlock, then which process is causing it.
It is better to find deadlock process, as it helps in recovering from deadlock.
11/10/2014
ChonuChinnus
Deadlock Detection:
In fact, RAG is optimized by eliminating the resource nodes to get the wait-for
graph.
11/10/2014
ChonuChinnus
Deadlock Detection:
P2
P3
R1
R2
R3
P1
P4
P3
P4
P2
Wait-for Graph
ChonuChinnus
10
Deadlock Detection:
When there are multiple instance of resource types, then cycle detection is not
sufficient to detect deadlock.
So, a algorithm similar to bankers algorithm is needed which checks the system
state with some data structures maintained and signal whether the system is in
deadlock.
As in deadlock avoidance algorithm, Deadlock Detection also has two phases:
In the first phase, the data structures needed are defined and maintained.
In the second phase, the algorithm detects the process which causes the
deadlock if any.
11/10/2014
ChonuChinnus
11
Deadlock Detection:
Av[i]=j
Av[i]=Total_Res[i]-allprocesses Alloc[i], where i is the resource type Ri
4. Request of each process: Stores the current request of each process of each process in the form of
p*r matrix, where p is the number of processes and r is the number of resource types.
11/10/2014
ChonuChinnus
12
Deadlock Detection:
4. If(found)
Current_Avail=Current_Avail + Alloci
1. Current_Avail=Av
Marked[i]=false
goto to step 3
if(Alloci==0)
11/10/2014
ChonuChinnus
13
Deadlock Recovery
The consequent action from deadlock is recovery from it.
The aim of the deadlock detection is to find out which process has caused the
deadlock and resolve it, so that the system can resume its work.
Imagine two cars approaching each other on a narrow bridge, through which
only one car can pass through at a time. If no car is ready to move back, then of
course there will be deadlock situation forever.
This is same as in a non pre-emption condition. So, the only solution for
resolving the deadlock is for one of the cars to move back (rollback) and allow the
other car to pass through the bridge, thereby resolving the deadlock.
An Operating System handles the deadlock in the same way.
There should be pre-emption of resources from one process, so that the other
process can continue and deadlock is removed.
11/10/2014
ChonuChinnus
14
Deadlock Recovery
Another solution is to abort the process which cause the deadlock.
Rollback and aborting a process cannot ne implemented always, as there is a
cost incurred in each solution.
Some recovery methods are:
Resource Pre-emption
Rollback
Aborting a process
ChonuChinnus
15
Deadlock Recovery
The decision to choose a process for pre-emption is driven by following cost
factors:
Number and type of resource a deadlock process is holding: If the number of
resources is less compared to the requirement of other processes to break the
deadlock, or if the process is not holding the desired type of resource, then those
resources need not be pre-empted.
Execution span of a process: Process with the longest execution span should be
pre-empted first. A process, whose execution has just started and requires many
resources to complete, will be the right victim for pre-emption.
Starvation of Process: Based on the above discussed factors, it may be possible
that the same process is always chosen for resource pre-emption, resulting in a
starvation situation. Thus, it is important to ensure that the process should not
starve. This can be done by fixing the number of times a process can be chosen
for resources pre-emption
11/10/2014
ChonuChinnus
16
Deadlock Recovery
Rollback: Checkpoints are good solution to recover from deadlock, apart from
using it in the resource pre-emption. The checkpoints save the state of the
system at specified point in time. The resource state is saved in a separate file,
along with the memory image. When a deadlock is detected, the state is analyzed
and the deadlock process is detected. The process, holding that resources rolled
back to safe checkpoint. After allocating the resource to the desired process, the
system continues, thereby, recovering from the deadlock. The process, which
was rolled back, restarts from the checkpoint.
Abort the Process: Aborting a process to resolve the deadlock is a costly solution
in terms of process computation. After aborting a process, run the deadlock
detection algorithm to confirm whether the deadlock still exists. As in resource
pre-emption, a process has to be chosen for aborting. But that aborted process
should least affect the system and should run without any further deadlocks.
Aborting a process depends on: Number and type of resources a deadlock
process holds, Execution span of a process, Process Priority, Type of Resources
11/10/2014
ChonuChinnus
17