Resource allocation graphs
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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.
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
اقرأ الدرس خطوة بخطوة
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 copyT1 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 runningT2 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 waitsT1 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)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)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; }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 thereT3 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 freeThe 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: noneCycle 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'] []
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01In a resource allocation graph, what does an arrow from lock A to thread T1 mean?
An assignment arrow runs from the resource to the thread that holds it. A request arrow runs the other way, from a waiting thread to the resource it wants.
02T1 holds A and asks for B; T2 holds B. What does the graph gain, and what happens to T1?
B is held, so pthread_mutex_lock blocks the caller until the mutex becomes available. The graph shows that as a request arrow from T1 to B.
03Every resource has exactly one copy, and the graph has the cycle T1 → B → T2 → A → T1. What can you conclude?
With single-copy resources, every thread in the cycle waits for a resource held by the next one, and nobody outside the cycle can free anything. They wait forever.
04B has two copies, held by T2 and T3. T1 holds A and waits for B; T2 waits for A; T3 waits for nothing. Is there a deadlock?
With several copies a cycle is necessary but not sufficient. The detector lets any thread whose request can be met finish and give back what it holds; here every thread gets to finish.
05Which graph can never contain a deadlock?
Deadlock needs circular wait, which shows up as a cycle. No cycle means no deadlock, however many copies the resources have.
المسرد
- Resource allocation graph
- A directed graph with a node for every thread and every resource, and arrows showing which thread holds and which thread waits for each resource.
- Assignment arrow
- An arrow from a resource to a thread (assignment edge): the thread holds one copy of that resource.
- Request arrow
- An arrow from a thread to a resource (request edge): the thread is blocked, waiting for a copy of that resource.
- Instance
- One copy of a resource type, drawn as a dot in its box. A mutex lock is always a single instance.
- Cycle
- A path that follows the arrows and comes back to where it started, such as T1 → B → T2 → A → T1.
- Deadlock
- A set of threads that each wait for a resource held by another thread in the set, so none of them can ever run again.
- Graph reduction
- A deadlock check: repeatedly let a thread whose request can be met finish and give back its resources. Any threads left at the end are deadlocked.
الملاحظات والمصادر
- The card shortens pthread_mutex_lock(&A) to lock(A) and shows the four calls in the order they happen. In the whole program, T1 and T2 each sleep for a second after their first lock, so this order is almost certain; without the sleep the threads might not deadlock on a given run.
- Textbooks draw both kinds of arrow as plain solid arrows; the direction alone tells them apart. The figure dashes request arrows to make the waiting easier to see.
- A resource type can have several identical copies (instances), such as printers or memory blocks. A plain mutex is always a single copy.
- OSTEP notes that database systems often run a deadlock detector periodically: it builds a resource graph and checks it for cycles. The Linux kernel's lock validator, lockdep, records the order locks are taken in and can report a possible deadlock before it happens.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 32: Common Concurrency Problems (deadlock dependency graph)
- Berkeley CS162 (Fall 2026), Lecture 13: deadlock and resource-allocation graphs
- MIT 6.1810 (2024), lecture notes: Locking (deadlock and lock order)
- POSIX man page: pthread_mutex_lock(3p)
- Linux kernel documentation: Runtime locking correctness validator (lockdep)
اختبار يتاح بعد الخطوة الأخيرة.