Chapter 6
Deadlock


A set T of threads is said to be deadlocked if each thread in T cannot proceed until an action is performed by some other thread in T.

Deadlock can occur any time threads acquire multiple resources, perform work, and then release their resources.


Example.

Two processes A and B, with two semaphores a and b, both of which are initialized to 1.

A:                  B:
P(a);                  P(b);
P(b);                  P(a);
V(b);                  V(a);
V(a);                  V(b);


Necessary Conditions for deadlock:


Solutions to the deadlock problem fall into three general categories: Deadlock avoidance algorithms check resource requests and availability to avoid deadlock.

Deadlock prevention algorithms make deadlock logically impossible. May lead to inefficient use of resources.

Deadlock detection algorithms look for instances of deadlock when processes stop making progress. If a deadlock is found, one or more processes must be killed. This method is usually not practical, but may be the only solution available in a distributed system.


Example: Deadlock Avoidance

The Banker's Algorithm


Example: Deadlock Prevention

Ordering resources

>hr>

Deadlock avoidance and prevention must eliminate one of the four necessary conditions for deadlock.