Home | Projects | Notes > Operating Systems > Deadlock Detection
Deadlock prevention strategies are very conservative; they solve the problem of deadlock by limiting access to resources and by imposing restrictions on processes.
At the opposite extreme, deadlock detection strategies do not limit resource access or restrict process actions. With deadlock detection, requested resources are granted to processes whenever possible. Periodically, the OS performs an algorithm that allows it to detect the circular wait.
A check for deadlock can be made as frequently as each resource request, or less frequently, depending on how likely it is for a deadlock to occur.
Consider a system of
Resource vector - total amount of each resource in the system
Available vector - total amount of each resource not allocated to any process
Request matrix -
Allocation matrix-
Start off by making processes that are not part of a deadlocked set. Initially, all processes are unmarked. Then the following steps are performed:
Mark each process that has a row in the allocation matrix
Initialize a temporary vector
LOOP:
Find an indes
If such row is found, mark process
A deadlock exists if and only if there are unmarked processes at the end of the algorithm. The set of unmarked rows corresponds precisely to the set of deadlocked processes.
The strategy in this algorithm is to find a process whose resource requests can be satisfied with the available resources, then assume those resources are granted and the process runs to completion and releases all of its resources. The algorithm then looks for another process to satisfy.
This algorithm does not guarantee to prevent deadlock; that will depend on the order in which future requests are granted. All it does is determine if deadlock exists.
Once deadlock has been detected, some strategy is needed for recovery. The following are possible approaches, listed in the order of increasing sophistications:
Abort all deadlocked processes. (One of the most common, solutions adopted in operating systems.)
Back up each deadlocked process to some previously defined checkpoint, and restart all processes.
This requires that rollback and restart mechanisms be built into the system. The risk in this approach is that the original deadlock may recur. However, the nondeterminacy of concurrent processing may ensure that this does not happen.
Successively abort deadlocked processes until deadlock no longer exist.
The order in which processes are selected for abortion should be on the basis of some criterion of minimum cost. After each abortion, the detection algorithm must be reinvoked to see whether deadlock still exists.
Successively preempt resources until deadlock no longer exists.
As in (3), a cost-based selection should be used, and re-invocation of the detection algorithm is required after each preemption. A process that has a resource preempted from it must be rolled back to a point prior to its acquisition of that resource.
For (3), and (4), the process selection criteria could be one of the following.
least amount of processor time consumed so far
least amount of output produced so far
most estimated time remaining
least total resources allocated so far
lowest priority
It leads to early detection.
The algorithm is relatively simple because it is based on incremental changes to the state of the system.
Frequent checks consume considerable processor time.
Stallings, W. (2018). Operating Systems: Internals and Design Principles (9th ed.). Pearson Education, Inc.