ENع
←Resource allocation graphs
Lesson 25 · Deadlock

Resource allocation graphs

12 min10 steps5 questionsIntermediate
About this lesson

Deadlock is easier to see when you draw it. A resource allocation graph shows threads as circles and resources as boxes, with arrows for who holds what and who waits for what. Follow two threads that lock A and B in opposite orders until their arrows close a loop, then give B a second copy and watch the same loop break. The lesson on the four conditions for deadlock explains circular wait; here you learn to spot it, and to tell when a cycle really means deadlock.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 25 · Interactive
01 / 10

Circles, boxes and dots

A resource allocation graph is a picture of who holds what and who waits for what. Threads are circles. Resources, here two locks, are boxes, with one dot for each copy. Locks A and B have one copy each.

thread      a circle         T1, T2
resource    a box            lock A, lock B
copy        a dot in a box   one copy each (a lock is one copy)

arrows (directed edges) come next:
  resource -> thread   assignment: the thread holds a copy
  thread -> resource   request: the thread waits for a copy
Loading figure…
or ← →
Read the lesson step by step
  1. Circles, boxes and dots

    A resource allocation graph is a picture of who holds what and who waits for what. Threads are circles. Resources, here two locks, are boxes, with one dot for each copy. Locks A and B have one copy each.

    thread      a circle         T1, T2
    resource    a box            lock A, lock B
    copy        a dot in a box   one copy each (a lock is one copy)
    
    arrows (directed edges) come next:
      resource -> thread   assignment: the thread holds a copy
      thread -> resource   request: the thread waits for a copy
  2. T1 locks A

    Line 1: T1 locks A. A is free, so T1 gets it at once. We draw an assignment arrow from A to T1. It points at the holder. T1 keeps running.

    line 1   T1: lock(A)        pthread_mutex_lock(&A) in C
    A        free -> held by T1
    edge     A -> T1   assignment
    T1       running
  3. T2 locks B

    Line 2: T2 locks B, which is also free. Another assignment arrow, from B to T2. So far nobody waits: each thread holds one lock and keeps running.

    line 2   T2: lock(B)
    B        free -> held by T2
    edge     B -> T2   assignment
    edges    A -> T1,  B -> T2      no thread waits
  4. T1 asks for B

    Line 3: T1 asks for B, but T2 holds it, so T1 must wait (it blocks). We draw a request arrow from T1 to B, dashed here. A request arrow means: this thread is stuck until that resource is free.

    line 3   T1: lock(B)
    B        held by T2 -> T1 blocks
             pthread_mutex_lock(3p): "the calling thread shall
             block until the mutex becomes available"
    edge     T1 -> B   request
    T1       waiting (still holds A)
  5. T2 asks for A

    Line 4: T2 asks for A, which T1 holds. T2 waits too, with a request arrow from T2 to A. Now both threads are blocked, and each one waits for the lock the other one holds.

    line 4   T2: lock(A)
    A        held by T1 -> T2 blocks
    edge     T2 -> A   request
    T1       waits for B (held by T2)
    T2       waits for A (held by T1)
  6. Follow the arrows

    Start at T1 and follow the arrows: T1 to B, B to T2, T2 to A, and A back to T1. That closed loop is a cycle. When every resource has one copy, a cycle means deadlock: these threads wait forever.

    path     T1 -> B -> T2 -> A -> T1      a cycle
    copies   A: 1   B: 1
    rule     one copy each + a cycle = deadlock
    
    the whole program (C99, POSIX threads):
    
    #define _POSIX_C_SOURCE 200809L
    #include <pthread.h>
    #include <stdio.h>
    #include <unistd.h>
    
    static pthread_mutex_t A = PTHREAD_MUTEX_INITIALIZER;
    static pthread_mutex_t B = PTHREAD_MUTEX_INITIALIZER;
    
    static void *t1(void *arg) {
        (void)arg;
        pthread_mutex_lock(&A);   /* line 1 */
        sleep(1);                 /* let T2 lock B */
        pthread_mutex_lock(&B);   /* line 3: waits for T2 */
        puts("T1 got A and B");
        pthread_mutex_unlock(&B);
        pthread_mutex_unlock(&A);
        return NULL;
    }
    
    static void *t2(void *arg) {
        (void)arg;
        pthread_mutex_lock(&B);   /* line 2 */
        sleep(1);
        pthread_mutex_lock(&A);   /* line 4: waits for T1 */
        puts("T2 got B and A");
        pthread_mutex_unlock(&A);
        pthread_mutex_unlock(&B);
        return NULL;
    }
    
    int main(void) {
        pthread_t x, y;
        pthread_create(&x, NULL, t1, NULL);
        pthread_create(&y, NULL, t2, NULL);
        pthread_join(x, NULL);    /* never returns: deadlock */
        pthread_join(y, NULL);
        puts("done");
        return 0;
    }
  7. Give B a second copy

    A new story: B has two copies, and a third thread, T3, holds the second one. Every arrow from before is still there, so the same cycle is still there. Is this still a deadlock?

    copies   A: 1   B: 2
    edges    A -> T1,  B -> T2,  B -> T3   (assignment)
             T1 -> B,  T2 -> A             (request)
    cycle    T1 -> B -> T2 -> A -> T1      still there
  8. T3 is not waiting

    T3 has no request arrow: it waits for nothing. So it can finish its work and unlock its copy of B. Its arrow disappears, and B now has a free copy.

    T3       no request arrow -> it can run to the end
    T3       unlocks its copy of B
    B        copies 2, held 1 -> 1 free
  9. The cycle breaks

    The free copy goes to T1, so its request arrow turns into an assignment arrow. T1 finishes and unlocks A and B, then T2 gets A. Here the cycle didn't mean deadlock.

    T1       request T1 -> B granted -> B -> T1
    T1       finishes, unlocks A and B
    T2       request T2 -> A granted -> A -> T2
    cycle    gone          deadlocked threads: none
  10. Cycle versus deadlock

    No cycle: no deadlock. One copy of each resource: a cycle means deadlock. Several copies: a cycle is needed but not enough, so a detector checks whether some thread can still finish.

    the detector as a Python simulation (graph reduction):
    
    def stuck(copies, holds, wants):
        left = set(holds) | set(wants)
        while True:
            used = {r: sum(holds.get(t, []).count(r) for t in left) for r in copies}
            ready = [t for t in sorted(left)
                     if t not in wants or used[wants[t]] < copies[wants[t]]]
            if not ready:
                return sorted(left)   # nobody can move: deadlocked
            left.remove(ready[0])     # it runs, finishes, unlocks all
    
    wants = {"T1": "B", "T2": "A"}
    print(stuck({"A": 1, "B": 1}, {"T1": ["A"], "T2": ["B"]}, wants))
    print(stuck({"A": 1, "B": 2}, {"T1": ["A"], "T2": ["B"], "T3": ["B"]}, wants))
    
    prints:
    ['T1', 'T2']
    []

Quiz Unlocks after the last step.