Avoiding deadlock: the banker's algorithm
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Preventing deadlock with lock ordering makes a cycle impossible. Avoidance takes a different route: it lets threads ask for resources freely, but refuses any loan that could lead into a deadlock. Edsger Dijkstra's banker's algorithm does this the way a careful banker lends money: every thread states its maximum up front, and before each loan the banker checks that some order still lets every thread finish. Follow 12 units, three threads and two requests, one refused and one granted. The four conditions for deadlock and Resource allocation graphs in this module set the scene.
The banker's idea
An operating system can avoid deadlock the way a careful banker lends money. Each thread states up front its maximum: the most units of a resource it could ever hold. The banker lends a unit only if everyone can still finish.
deadlock threads wait for each other forever
prevention make a cycle impossible (e.g. lock order)
avoidance allow cycles in principle, but refuse any
loan that could lead into one
the banker needs, for every thread:
max the most units it may ever hold (stated up front)
has the units it holds nowاقرأ الدرس خطوة بخطوة
The banker's idea
An operating system can avoid deadlock the way a careful banker lends money. Each thread states up front its maximum: the most units of a resource it could ever hold. The banker lends a unit only if everyone can still finish.
deadlock threads wait for each other forever prevention make a cycle impossible (e.g. lock order) avoidance allow cycles in principle, but refuse any loan that could lead into one the banker needs, for every thread: max the most units it may ever hold (stated up front) has the units it holds nowWho holds what
Here 12 units exist. T0 holds 5 of its maximum 10, T1 holds 2 of 4, T2 holds 2 of 9. So 3 units are free. What each might still ask for is its need: maximum minus held, so 5, 2 and 7.
12 units in all, one kind of resource thread max has need T0 10 5 5 T1 4 2 2 T2 9 2 7 free 3 free = 12 - (5 + 2 + 2) = 3 need = max - hasWhat safe means
A state is safe if there is some order in which every thread can get its whole need, finish, and give all its units back. To check, the banker plays the loans out in its head, starting from the free units.
safety check (one pass per thread that finishes): 1 need = max - has 2 if need <= free: 3 free += has repeat until every thread is done -> SAFE or until no thread fits -> UNSAFE nothing is really lent during the checkT1 can finish
Free is 3. T0 needs 5, too many, but T1 needs only 2. Pretend T1 gets them, finishes, and returns all 4 of its units: free becomes 3 − 2 + 4 = 5.
T0 need 5 <= free 3? no T1 need 2 <= free 3? yes T1 finishes, returns 4 free = 3 - 2 + 4 = 5 thread max has need T0 10 5 5 T1 4 0 done T2 9 2 7 free 5Then T0 can finish
Now free is 5 and T0 needs 5, just enough. Pretend T0 gets them, finishes and returns all 10 of its units, so free becomes 10.
T0 need 5 <= free 5? yes T0 finishes, returns 10 free = 5 + 5 = 10 thread max has need T0 10 0 done T1 4 0 done T2 9 2 7 free 10Safe: T1, T0, T2
T2 needs 7 and 10 are free, so T2 can finish too, and all 12 units are back. The order T1, T0, T2 works, so the state is safe. None of this really happened: it was only a check.
T2 need 7 <= free 10? yes free = 10 + 2 = 12 every thread can finish, in the order T1, T0, T2 the real state is SAFE the real loans are unchanged: thread max has need T0 10 5 5 T1 4 2 2 T2 9 2 7 free 3T2 asks for one more
Back to the real loans. T2 asks for 1 more unit. Before lending, the banker pretends to grant it: free drops to 2, T2 holds 3 and still needs 6. Then it runs the same check.
request T2 wants 1 unit allowed? T2 would hold 3 <= max 9, and 1 <= free 3 pretend it is granted: thread max has need T0 10 5 5 T1 4 2 2 T2 9 3 6 free 2Nobody else can finish
With 2 free, only T1 fits: it finishes and returns 4, so free is 4. Now T0 needs 5 and T2 needs 6. Neither fits, and no order is left. This state would be unsafe.
T0 need 5 <= free 2? no T1 need 2 <= free 2? yes free = 4 T0 need 5 <= free 4? no T2 need 6 <= free 4? no no thread fits: the state would be UNSAFE thread max has need T0 10 5 5 T1 4 0 done T2 9 3 6 free 4The request must wait
So the banker says not yet: the unit stays in the bank and T2 waits. Unsafe is not deadlock yet. T1 could still finish, but then T0 and T2 could each wait forever for units the other holds.
decision refuse for now: T2 waits (checked again when units come back) why unsafe is dangerous: if T0 asks for 5 more and T2 for 6 more, after T1 finishes only 4 units are free: neither can finish, and neither will let go the real loans stay as they were: thread max has need T0 10 5 5 T1 4 2 2 T2 9 2 7 free 3A safe request is granted
Later T1 asks for 1 unit. Pretend: free 2, T1 holds 3 and needs 1. T1 can finish (free 5), then T0 (free 10), then T2. Safe, so the banker lends the unit right away.
request T1 wants 1 unit pretend it is granted, then check: T1 need 1 <= free 2? yes free = 5 T0 need 5 <= free 5? yes free = 10 T2 need 7 <= free 10? yes free = 12 SAFE (T1, T0, T2): the unit is lent for real thread max has need T0 10 5 5 T1 4 3 1 T2 9 2 7 free 2The price of avoiding
It works, but every thread must state its maximum in advance, and every request triggers a full check. General-purpose kernels such as Linux skip it and keep locks in a fixed order instead. The banker suits small, fixed systems.
the algorithm as a Python simulation: MAX = [10, 4, 9] # each thread's stated maximum def safe(free, has): done, order = set(), [] while len(done) < len(has): for t in range(len(has)): if t not in done and MAX[t] - has[t] <= free: free += has[t] # t finishes, returns all done.add(t) order.append(f"T{t}") break else: return None # nobody fits: unsafe return order print(safe(3, [5, 2, 2])) # the real loans print(safe(2, [5, 2, 3])) # if T2 got 1 more print(safe(2, [5, 3, 2])) # if T1 got 1 more prints: ['T1', 'T0', 'T2'] None ['T1', 'T0', 'T2']
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A thread has a maximum of 9 units and holds 2. What is its need?
Need is maximum minus held: 9 - 2 = 7. It is the most the thread might still ask for.
02What does it mean for a state to be safe?
Safe means the banker can still promise that everyone finishes: there is at least one order (here T1, T0, T2) in which each thread's need fits in the free units, counting what earlier threads give back.
0312 units exist. T0 holds 5 (max 10), T1 holds 2 (max 4), T2 holds 2 (max 9). T2 asks for 1 more. What does the banker do?
Pretending to grant it leaves 2 free. T1 fits (need 2), finishes and returns 4, but then neither T0 (need 5) nor T2 (need 6) fits. The state would be unsafe, so T2 waits.
04Is an unsafe state the same as a deadlock?
Unsafe means the banker can no longer guarantee that everyone finishes. A deadlock may or may not follow, depending on what the threads ask for next. The banker avoids the risk by never entering an unsafe state.
05Why do general-purpose kernels such as Linux not use the banker's algorithm?
The algorithm is correct, but it needs every maximum up front and a check on every request. OSTEP calls such avoidance useful only in very limited environments; Linux instead relies on lock ordering, checked by lockdep while testing.
المسرد
- Deadlock avoidance
- Letting threads request resources freely, but refusing (delaying) any request that could lead to a deadlock. Different from prevention, which makes a cycle impossible by design.
- Maximum claim
- The most units of a resource a thread says, up front, it could ever hold at once. The banker's algorithm needs this for every thread.
- Need
- What a thread might still ask for: its maximum minus the units it holds now.
- Safe state
- A state from which there is some order in which every thread can get its whole need, finish and return its units.
- Unsafe state
- A state that is not safe. No thread is stuck yet, but if the threads ask for their maximums, some may wait forever.
- Safety check
- The banker's test: starting from the free units, repeatedly pick a thread whose need fits, pretend it finishes and add its units back. If every thread finishes, the state is safe.
- Lock ordering
- Always taking locks in one fixed global order, so threads can never wait for each other in a cycle. The usual way real systems prevent deadlock.
الملاحظات والمصادر
- The lesson uses one kind of resource, as in Dijkstra's original banker. With several kinds (say, printers and buffers), max, has, need and free become lists with one number per kind, and a thread fits only if its need is no bigger than free for every kind.
- Textbooks often call the banker's scratch copy of the free units Work, and the units held Allocation. The figure moves real-looking coins during the check, but the check only changes the copy: the real loans never move until a request is granted.
- Unsafe does not mean deadlocked. It means the banker can no longer promise that everyone finishes: if the threads go on to ask for their full maximums, some may wait forever.
- One check can scan the list of threads once for every thread that finishes, so with n threads it may compare up to about n × n needs per kind of resource. That cost on every request, plus the need to know every maximum in advance, is why general-purpose systems prevent deadlock with lock ordering instead.
- Linux does not run a banker. Its lockdep checker, used while testing the kernel, records the order in which lock classes are taken and reports an order that could form a cycle.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 32: Common Concurrency Problems (deadlock avoidance via scheduling)
- Berkeley CS162 (Fall 2026), Lecture 13: Scheduling 4, Deadlock (banker's algorithm)
- E. W. Dijkstra, EWD623: The mathematics behind the Banker's Algorithm
- E. W. Dijkstra, EWD108 (1964): Een algorithme ter voorkoming van de dodelijke omarming
- MIT 6.1810 (2024), lecture notes: Locking (deadlock and lock ordering)
- Linux kernel documentation: Runtime locking correctness validator (lockdep)
- Linux man-pages: pthread_mutex_lock(3p)
اختبار يتاح بعد الخطوة الأخيرة.