CS 537 Notes, Section #12: Deadlock


OSTEP, Chapter 32


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:

  1. No sharing of resources. They can be used by only one process at a time.
  2. No preemption. Once given, a resource cannot be taken away.
  3. Multiple independent requests. Processes do not ask for resources all at once.
  4. Circularity in the Resource Graph. The Resource Graph describes who has what and who wants what. Draw graph showing processes as circles, resources as squares, arrows from process to resource waited for, from resource to owning process.

    Circular graph

    Now, as an exercise, try to draw the Resource Graph for the intersection diagram shown at the start of this lecture. The hardest part is to identify the shared resources.


    The Dining Philosophers Problem (due to Edsger Dijkstra and Tony Hoare, 1965)


    Dining Philosophers diagram

    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. 298 (8th edition).

    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?).



    Copyright © 2013, 2018 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.