Related Content: CS604 - VU Lectures, Handouts, PPT Slides, Assignments, Quizzes, Papers & Books of Operating Systems
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:
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.
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:
The algorithm is:
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.
Finish Sequence: < P0>
Finish Sequence: < P0, P2>
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.
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.
When should we invoke the deadlock detection algorithm? The answer depends n two factors:
Hence the options are:
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.
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.
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:
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: