Docsity

Deadlock Detection-Operating Systems-Lecture Notes, Study notes of Operating Systems

Toggle sidebar
bg1
144
Operating Systems Lecture No. 29
Operating Systems
Lecture No. 29
Reading Material
Chapter 8 of the textbook
Lecture 29 on Virtual TV
Summary
Deadlock detection: resources with single and multiple instances
Recovery from deadlocks
Process termination
Resource preemption
Deadlock Detection
If a system does not employ either a deadlock prevention or a deadlock avoidance
algorithm then a deadlock may occur. In this environment, the system must provide:
An algorithm that examines (perhaps periodically or after certain events) the state
of the system to determine whether a deadlock has occurred
A scheme to recover from deadlocks
Single Instance of Each Resource Type
If all resources have only a single instance, then we can define a deadlock detection
algorithm that uses a variant of the resource allocation graph, called a wait-for graph.
We obtain this graph from the resource allocation graph by removing the nodes of type
resource and collapsing the appropriate edges. More precisely, an edge from Pi to Pj in a
wait-for graph implies that process Pi is waiting for process Pj to release a resource that Pi
needs. An edge Pi  Pj exists in a wait-for graph exists if and only if the corresponding
resource allocation graph contains two edges for Pi  Rq and Rq  Pj some resource Rq.
As before, a deadlock exists in the system if and only if the wait for graph contains a
cycle. To detect deadlocks the system needs to maintain the wait-for graph and
periodically to invoke an algorithm that searches for a cycle in the graph. The following
diagram shows a resource allocation graph and the corresponding wait-for graph. The
system represented by the given wait-for graph has a deadlock because the graph contains
a cycle.
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Deadlock Detection-Operating Systems-Lecture Notes and more Study notes Operating Systems in PDF only on Docsity!

Operating Systems Lecture No. 29

Operating Systems

Lecture No. 29

Reading Material

 Chapter 8 of the textbook  Lecture 29 on Virtual TV

Summary

 Deadlock detection: resources with single and multiple instances  Recovery from deadlocks  Process termination  Resource preemption

Deadlock Detection

If a system does not employ either a deadlock prevention or a deadlock avoidance algorithm then a deadlock may occur. In this environment, the system must provide:  An algorithm that examines (perhaps periodically or after certain events) the state of the system to determine whether a deadlock has occurred  A scheme to recover from deadlocks

Single Instance of Each Resource Type If all resources have only a single instance, then we can define a deadlock detection algorithm that uses a variant of the resource allocation graph, called a wait-for graph. We obtain this graph from the resource allocation graph by removing the nodes of type resource and collapsing the appropriate edges. More precisely, an edge from Pi to Pj in a wait-for graph implies that process Pi is waiting for process Pj to release a resource that Pi needs. An edge Pi  Pj exists in a wait-for graph exists if and only if the corresponding resource allocation graph contains two edges for Pi  Rq and Rq  Pj some resource R (^) q. As before, a deadlock exists in the system if and only if the wait for graph contains a cycle. To detect deadlocks the system needs to maintain the wait-for graph and periodically to invoke an algorithm that searches for a cycle in the graph. The following diagram shows a resource allocation graph and the corresponding wait-for graph. The system represented by the given wait-for graph has a deadlock because the graph contains a cycle.

Several Instances of a Resource Type The wait for graph scheme is not applicable to a resource allocation system with multiple instances of each resource type. The deadlock detection algorithm described next is applicable to such a system. It uses the following data structures:

 Available: A vector of length m indicates the number of available resources of each type.  Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process.  Request: An n x m matrix indicates the current request of each process. If Request[i,j] = = k, then process P (^) i is requesting k more instances of resource type Rj.

The algorithm is:

  1. Let Work and Finish be vectors of length m and n respectively. Initialize Work=Available. For i=1, 2,… , n if Allocation[i]  0 the Finish[i]=false; otherwise Finish[i]=true
  2. Find an index i such that both a. Finish[i] = = false b. Request (^) i  Work c. If no such i exists go to step 4.
  3. Work=Work + Allocationi a. Finish[i]=true b. Go to step 2.
  4. If Finish[i] = = false, for some i, 1 i n, then the system is in a deadlock state. Moreover, if Finish[i] = = false, then Pi is deadlocked.

Resource-Allocation Graph Corresponding wait-for graph

Allocation Request Work A B C A B C A B C P 0 0 1 0 0 0 0 0 0 0 P 1 2 0 0 2 0 2 0 1 0 P 2 3 0 2 0 0 0 3 1 2 P 3 2 1 1 1 0 0 5 2 3 P 4 0 0 2 0 0 2 5 2 5 Here is the sequence in which requests of processes P 0 through P 4 may be satisfied: < P0, P2, P3, P4, P 1 >. This is not a unique sequence. A few other possible sequences are the following. < P0, P2, P3, P1, P4,> < P0, P2, P4, P1, P 3 > < P0, P2, P4, P3, P 1 > Now let us assume that P 2 requests an additional instance of C. Do we have a finish sequence? The work below shows that if this request is granted, the system will enter a deadlock. P 0 ’s request can be satisfied with currently available resources, but request for no other process can be satisfied after that. Thus, a deadlock exists, consisting of processes P1, P2, P3, and P4.

Process Request A B C P 0 0 0 0 P 1 2 0 2 P 2 0 0 1 P 3 1 0 0 P 4 0 0 2

Allocation Request Work A B C A B C A B C P 0 0 1 0 0 0 0 0 0 0 P 1 2 0 0 2 0 2 P 2 3 0 2 0 0 1 P 3 2 1 1 1 0 0 P 4 0 0 2 0 0 2

Allocation Request Work A B C A B C A B C P 0 0 1 0 0 0 0 0 0 0 P 1 2 0 0 2 0 2 0 1 0 P 2 3 0 2 0 0 1 P 3 2 1 1 1 0 0 P 4 0 0 2 0 0 2

Detection Algorithm Usage When should we invoke the deadlock detection algorithm? The answer depends n two factors:

  1. How often is a deadlock likely to occur?
  2. How many processes will be affected by deadlock when it happens? Hence the options are:  Every time a request for allocation cannot be granted immediately—expensive but process causing the deadlock is identified, along with processes involved in deadlock  Periodically, or based on CPU utilization  Arbitrarily—there may be many cycles in the resource graph and we would not be able to tell which of the many deadlocked processes “caused” the deadlock.

Recovery from Deadlock When a deadlock detection algorithm determines that a deadlock exists, several alternatives exist. One possibility is to inform the operator that a deadlock has occurred, and to let the operator deal with the deadlock manually. The other possibility is to let the system recover from the deadlock automatically. There are two options for breaking a deadlock. One solution is simply to abort one or more processes to break the circular wait. The second option is to preempt some resources from one or more of the deadlocked processes.

Process Termination To eliminate deadlocks by aborting a process, we use one of two methods. In both methods the system reclaims all resources allocated to the terminated process.  Abort all deadlocked processes: This method clearly will break the deadlock cycle, but at a great expense; these processes may have computed for a long time, and the results of these partial computations must be discarded and probably recomputed later.  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. Aborting a process may not be so easy. If a process was in the midst of updating a file, terminating it will leave the system in an inconsistent state. If the partial termination method is used, then given a set of deadlocked processes, we must determine which process should be terminated in an attempt to break the deadlock. This determination is a policy decision similar to CPU scheduling problems. The question is basically an economic one, we should abort those processes the termination of which will incur the minimum cost. Unfortunately, the term minimum cost is not a precise one. Many factors determine which process is chosen, including:

  1. What the priority of the process is
  2. How long the process has computed, and how much longer the process will compute before completing its designated task.
  3. How many and what type of resources the process has used