Deaad
Deaad
7 Deadlock Detection
only a single instance of each resource type, as well as to systems with several instances of each
resource type. At this point, however, we note that a
detection algorithm but also the potential losses inherent in recovering from
a deadlock.
If all resources have only a single instance, then we can define a deadlockdetection algorithm that
uses a variant of the resource-allocation graph, called
T3
T5
T4
T1 T2
R2
R1 R3 R4
R5
T3
T5
T4
T1 T2
(a) (b)
graph contains two edges Ti → Rq and Rq → Tj for some resource Rq. In Figure
graph.
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 waitfor graph and
periodically invoke an algorithm that searches for a cycle in the
The BCC toolkit described in Section 2.10.4 provides a tool that can
by inserting probes which trace calls to the pthread mutex lock() and
pthread mutex unlock() functions. When the specified process makes a call
locks in that process, and reports the possibility of deadlock if it detects a cycle
in the graph.
with multiple instances of each resource type. We turn now to a deadlockdetection algorithm that is
applicable to such a system. The algorithm employs
several time-varying data structures that are similar to those used in the
of each type.
Although Java does not provide explicit support for deadlock detection, a
snapshot of the states of all threads in a Java application. Java thread dumps
waiting to acquire. When a thread dump is generated, the JVM searches the
Ctrl-Break (Windows)
In the source-code download for this text, we provide a Java example of the
program shown in Figure 8.1 and describe how to generate a thread dump
resource type Rj
The ≤ relation between two vectors is defined as in Section 8.6.3. To simplify notation, we again treat
the rows in the matrices Allocation and Request
. The detection algorithm described here simply investigates every possible allocation sequence
for the threads that remain to be completed. Compare this algorithm with the
a. Finish[i] == false
b. Requesti ≤ Work
Go to step 2.
You may wonder why we reclaim the resources of thread Ti (in step 3) as
soon as we determine that Requesti ≤ Work (in step 2b). We know that Ti is
complete its task; it will thus soon return all currently allocated resources to
the system. If our assumption is incorrect, a deadlock may occur later. That
invoked.
through T4 and three resource types A, B, and C. Resource type A has seven
instances, resource type B has two instances, and resource type C has six
instances. The following snapshot represents the current state of the system:
T1 200 202
T2 303 000
T3 211 100
T4 002 002
our algorithm, we will find that the sequence <T0, T2, T3, T1, T4> results in
Suppose now that thread T2 makes one additional request for an instance
ABC
T0 000
T1 202
T2 001
T3 100
T4 002
We claim that the system is now deadlocked. Although we can reclaim the
resources held by thread T0, the number of available resources is not sufficient
to fulfill the requests of the other threads. Thus, a deadlock exists, consisting
When should we invoke the detection algorithm? The answer depends on two
factors:
Deadlocks occur only when some thread makes a request that cannot be
granted immediately. This request may be the final request that completes a
chain of waiting threads. In the extreme, then, we can invoke the deadlockdetection algorithm every
time a request for allocation cannot be granted
immediately. In this case, we can identify not only the deadlocked set of threads
but also the specific thread that “caused” the deadlock. (In reality, each of the
deadlocked threads is a link in the cycle in the resource graph, so all of them,
jointly, caused the deadlock.) If there are many different resource types, one
request may create many cycles in the resource graph, each cycle completed
by the most recent request and “caused” by the one identifiable thread.
example, once per hour or whenever CPU utilization drops below 40 percent.
resource graph may contain many cycles. In this case, we generally cannot tell