Deadlock Detection Nuhara Jabbar Deadlock Detection If a
Deadlock Detection Nuhara Jabbar
Deadlock Detection • If a system does not employ either a deadlockprevention or a deadlock avoidance algorithm, then a deadlock situation may occur. In this environment, the system must provide: • An algorithm that examines the state of the system to determine whether a deadlock has occurred • An algorithm to recover from the deadlock
Single Instance of Each Resource Type • Maintain wait-for graph – Nodes are processes. – Pi Pj if Pi is waiting for Pj. • Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock.
Resource-Allocation Graph and Waitfor Graph Resource-Allocation Graph Corresponding wait-for graph
Several Instances of a Resource Type • The algorithm employs several data structures that are similar to those used in the banker's algorithm • 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 Pi is requesting k more instances of resource type. Rj
Detection Algorithm • The detection algorithm simply investigates every possible allocation sequence of the processes that remain to be completed. The Algorithm: 1. Let Work and Finish be vectors of length m and n, respectively Initialize: (a) Work = Available (b)For i = 1, 2, …, n, if Allocation i 0, then Finish[i] = false; otherwise, Finish[i] = true.
Detection Algorithm 2. Find an index i such that both: (a) Finish[i] == false (b)Requesti Work If no such i exists, go to step 4 3. Work = Work + Allocationi Finish[i] = true go to step 2. 4. If Finish[i] == false, for some i, 1 i n, then the system is in deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked
Example of Detection Algorithm • Five processes P 0 through P 4; three resource types A (7 instances), B (2 instances), and C (6 instances). • Snapshot at time T 0: Allocation Request Available ABC ABC P 0 0 1 0 000 P 1 2 0 0 202 P 2 3 000 P 3 2 1 1 100 P 4 0 0 2 002 • Sequence <P 0, P 2, P 3, P 1, P 4> will result in Finish[i] = true for all i.
Example of Detection Algorithm • P 2 requests an additional instance of type C. Request ABC P 0 0 P 1 2 0 2 P 2 0 0 1 P 3 1 0 0 P 4 0 0 2 • State of system? – Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4
Detection-Algorithm Usage • When should we invoke the detection algorithm? The answer depends on two factors: 1. How often is a deadlock likely to occur? 2. How many processes will be affected by deadlock when it happens?
Detection-Algorithm Usage • Deadlocks occur only when some process makes a request that cannot be granted immediately. This request may be the final request that completes a chain of waiting processes. • We can invoke the deadlock detection algorithm every time a request for allocation cannot be granted immediately. • In this case, we can identify not only the deadlocked set of processes but also the specific process that "caused" the deadlock.
Detection-Algorithm Usage • Of course, if the deadlock-detection algorithm is invoked for every resource request, this will incur a considerable overhead in computation time. • A less expensive alternative is simply to invoke the algorithm at less frequent intervals • For example, once per hour or whenever CPU utilization drops below 40 percent.
Recovery from Deadlock 1. Process Termination 2. Preempting Recourses
Process Termination • we use one of two methods. In both methods, the system reclaims all resources allocated to the terminated processes. 1. 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. 2. Abort one process at a time until the deadlock cycle is eliminated: after each process is aborted, a deadlockdetection algorithm must be invoked to determine whether any processes are still deadlocked.
Process Termination • In which order should we choose to abort? • The question is basically an economic one; we should abort those processes whose termination will incur the minimum cost. Unfortunately, the term minimum cost is not a precise one. Many factors may affect which process is chosen, including: – Priority of the process. – How long process has computed, and how much longer to completion. – Resources the process has used. – Resources process needs to complete.
Resource Preemption • Selecting a victim process – minimize cost. • Rollback – return to some safe state, restart process for that state. • Starvation – same process may always be picked as victim. Clearly, we must ensure 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.
- Slides: 16