CS 537 Notes, Section #12: Deadlock


Intersection Deadlock

Deadlock is one area where there is a strong theory, but it is almost completely ignored in practice. Reason: solutions are expensive and/or require predicting the future.

A simple deadlock example with semaphores, for two processes:


    Initialization
    --------------
    sem1 = new Semaphore(1);
    sem2 = new Semaphore(1);

    Process A        Process B
    ---------        ---------
     P(sem1)          P(sem2)
     P(sem2)          P(sem1)

Define deadlock: a situation wherein each of a collection of processes is waiting for something from other processes in the collection. Since all are waiting, none can provide any of the things being waited for.

Deadlock can occur over separate resources, as in above example, or even over separate copies of a single resource. Block building contest: suppose each of a collection of processes is trying to produce a result by acquiring a number of resources one after the other.

Will the processes all be able to complete their jobs?


Blocks and Allocation

These are relatively simple-minded cases. Things may be much more complicated:

In general, there are four conditions for deadlock:


Circular graph

Now, draw the resource graph for the intersection diagram at the start of this lecture. The hardest part is to identify the shared resources.


Solutions to the deadlock problem fall into two general categories:

Deadlock prevention: must find a way to eliminate one of the four necessary conditions for deadlock:


Dijkstra's Banker's Algorithm is an approach to trying to give processes as much as is possible, while guaranteeing no deadlock. For a complete explanation, see Silberschatz and Galvin, p. 256.

In general, prevention of deadlock is expensive and/or inefficient. Detection is also expensive and recovery is seldom possible (what if process has things in a weird state?).


IBM OS/360 solutions:



Copyright © 1997, 2002, 2008 Barton P. Miller
Non-University of Wisconsin students and teachers are welcome to print these notes their personal use. Further reproduction requires permission of the author.