Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Deadlocks Detection and Recovery

Reference Book
Operating System Concepts 9th Edition
Avi Silberschatz, Peter Baer Galvin and Greg Gagne

Ibrar Afzal
Lecturer
IT Department HU Mansehra

1
Deadlocks Detection
Need an algorithm that determines SINGLE INSTANCE OF A RESOURCE TYPE
if deadlock occurred.  
  • Wait-for graph == remove the resources
Also need a means of recovering from the usual graph and collapse edges.
from that deadlock. • An edge from p(j) to p(i) implies that p(j) is
waiting for p(i) to release.
 

2
Deadlocks Detection
In the diagram, resource 1 and resource 2 have single instances.
There is a cycle
 R1 → P1 → R2 → P2.
 Deadlock is Confirmed.

3
Deadlocks Detection

 If there are multiple instances of resources:


 Detection of the cycle is necessary but not sufficient condition for deadlock detection

 In this case, the system may or may not be in deadlock varies according to

different situations.
 

4
Deadlocks Detection

 Several instances of a resource type

 We need to keep track of:


 
 Available - records how many resources of each type are available.
 Allocation - number of resources of type m allocated to process n.
 Request - number of resources of type m requested by process n.
 
 Let work and finish be vectors of length m and n respectively.

5
Deadlocks Detection

1. Initialize work[ ] = available[ ]


For i = 1,2,...n, if allocation[i] != 0 then // For all n processes
finish[i] = false; otherwise, finish[i] = true;
 
2. Find an i process such that:
finish[i] == false and request[i] <= work
 
If no such i exists, go to step 4.
 
3. work = work + allocation[i]
finish[i] = true
goto step 2
 
4. if finish[i] == false for some i, then the system is in deadlock state.
IF finish[i] == false, then process p[i] is deadlocked.

6
Deadlocks Detection
EXAMPLE
We have three resources, A, B, and C. A has 7 instances, B has 2 instances, and C has 6
instances. At this time, the allocation, etc. looks like this: 

Is there a  Alloc   Req   Avail 


sequence that will A B C A B C A B C
allow deadlock to
be avoided? P0 0 1 0 0 0 0 0 0 0
Is there more than P1 2 0 0 2 0 2
one sequence that P2 3 0 3 0 0 0
will work?
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2

7
Deadlocks Detection

EXAMPLE
 Suppose the Request matrix is changed like this. In other words, the maximum amounts to be allocated
are initially declared so that this request matrix results.
 

Is there now a
sequence that will  Alloc   Req   Avail 
allow deadlock to be
avoided? A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
USAGE OF THIS P1 2 0 0 2 0 2
DETECTION ALGORITHM P2 3 0 3 0 0 1#
 
Frequency of check P3 2 1 1 1 0 0
depends on how often a
deadlock occurs and how P4 0 0 2 0 0 2
many processes will be
affected.

8
Deadlocks Recovery

Deadlocks Recovery can be through Resources or killing the Processes

9
Deadlocks Recovery
Process Termination
Deadlock has occurred?
  Inform the operator that a deadlock has occur and to permit the

operator deal with the deadlock manually.


Allow the system recover from the deadlock automatically

10
Deadlocks Recovery
Process Termination
 Deadlock has occurred , how do we get the resources back and gain forward

progress?

 To remove deadlocks by aborting a process, we can use one of two methods.
 Abort all deadlocked processes.

 Abort one process at a time until the deadlock cycle is eliminated.

 In both methods, the system regain all resources allocated to the terminated

Processes.

11
Deadlocks Recovery
Process Termination
 

Abort all deadlocked processes.


 This method clearly will break the deadlock cycle, but at great expense.

 The deadlocked processes may have computed for a long time, and the
results of these partial computations must be discarded and probably will
have to be recomputed later.

12
Deadlocks Recovery
Process Termination
 
:
 

Abort one process at a time until the deadlock cycle is eliminated.


 This method incurs considerable overhead, since after each process is

aborted, a deadlock-detection algorithm must be invoked to determine


whether any processes are still deadlocked.

13
Deadlocks Recovery
Process Termination
Is this a Simple and straightforward approach?
NO
Why???
  If the process was in the midst of updating a file,
 Terminating it will leave that file in an erroneous state.

 If the process was in the midst of printing data on a printer,


 System must reset the printer to a correct state before printing the next job.

 If the partial termination method is used,


 We must decide which deadlocked process (or processes) supposed to be terminated. This

is a policy decision, similar to CPU-scheduling decisions

14
Deadlocks Recovery
Process Termination

Do we have a criteria to follow for Process Termination?


•What the priority of the process is
•How long the process has computed and how much longer the process
will compute before completing its designated task
•How many more resources the process needs in order to complete
•How many processes will need to be terminated
• Whether the process is interactive or batch

15
Deadlocks Recovery
Resource Preemption

•Resources are preempted from the processes involved in the deadlock


• Preempted resources are allocated to other processes
•So that there is a possibility of recovering the system from deadlock.
Is it possible?

16
Deadlocks Recovery
Resource Preemption

Yes
•But after solving three main issues related with Resource
Preemption
1. Selecting a victim
2. Rollback
3. Starvation.

17
Deadlocks Recovery
Resource Preemption
1. Selecting a victim.
 Which resources and which processes are to be preempted?

 We must determine the order of preemption to minimize cost.


 Cost factors may include such parameters as

 The number of resources a deadlocked process is holding


 Amount of time the process has thus far consumed.

18
Deadlocks Recovery
Resource Preemption
2. Rollback
 If we preempt a resource from a process, what should be done with that process?
 Process cannot carry on with its normal execution because

 It is missing some needed resource.


 We must roll back the process to some safe state and restart it from that state.

 For a safe state is, the simplest solution is a total rollback:


 Abort the process and then restart it.

 It is more effective to roll back the process only to break the deadlock
 But this method want the system to keep more information about the state of all

running processes.

19
Deadlocks Recovery
Resource Preemption
3. Starvation.
•How do we ensure that starvation will not occur?
•Is there a guarantee that same process will preempted for a resource?
•But in a system where victim selection is based mainly on cost factors,
•It may happen that the same process is always picked as a victim.
•As a result, this process never completes its designated task, a starvation
situation any practical system must address.
• Make it sure that a process can be picked as a victim only a (small) finite number of
times.
•The most common solution is to include the number of rollbacks in the cost factor.

20
Thanks

21

You might also like