Spread Knowledge

CS604 - Operating Systems - Lecture Handout 29

User Rating:  / 0
PoorBest 

Related Content: CS604 - VU Lectures, Handouts, PPT Slides, Assignments, Quizzes, Papers & Books of Operating Systems

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.

Single Instance of Each Resource Type

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 Pi 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
    • Finish[i] = = false
    • 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 a deadlock state. Moreover, if Finish[i] = = false, then Pi is deadlocked.

We show the working of this algorithm with an example. Consider the following system:

P = { P0, P1, P2, P3, P4 }
R = { A, B, C }
A: 7 instances
B: 2 instances
C: 6 instances

The system is currently in the following state. We want to know if the system has a deadlock. We find this out by running the above algorithm with the following state and construct a sequence in which requests for the processes may be granted.

construct a sequence

Finish Sequence: < P0>

Finish Sequence  P0

Finish Sequence: < P0, P2>

Finish Sequence P0, P2

Finish Sequence: < P0, P2, P3>

Finish Sequence  P0, P2, P3

Here is the sequence in which requests of processes P0 through P4 may be satisfied:
< P0, P2, P3, P4, P1>. This is not a unique sequence. A few other possible sequences are the following.

unique sequence

Now let us assume that P2 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. P0’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.

deadlock exists

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
  4. How many resources the process needs in order to complete
  5. How many processes will need to be terminated
  6. Whether the process is interactive or batch

Resource Preemption

To eliminate deadlocks using resource preemption, we successively preempt some resources from processes and give these to other processes until the deadlock cycle is broken. If preemption is required to deal with deadlocks, then three issues need to be addressed:

  1. Selecting a victim: Which resources and which processes are to be preempted?
    As in process termination, we must determine the order of preemption to minimize cost. Cost factors may include such parameters as the number of resources a deadlock process is holding, and the amount of time a deadlocked process has thus far consumed during its execution.
  2. Rollback: If we preempt a resource from a process, what should be done with that process? Clearly, it cannot continue with its normal execution; it is missing some needed resource. We must roll back the process to some safe state and restart it from that state. Since, in general it is difficult to determine what a safe state is, the simplest solution is a total rollback: Abort the process and then restart it. However it is more effective to roll back the process only as far as necessary to break the deadlock. On the other hand, this method requires the system to keep more information about the state of all the running processes.
  3. Starvation: In a system where victim selection is based primarily on cost factors, it may happen that the same process is always picked as the victim. As a result this process never completes its designated task, a starvation situation that needs to be dealt with in any practical system. Clearly, we must ensure that a process is picked as a victim only a finite number of times. The most common solution is to include the number of rollbacks in the cost factor.