ENع
←The banker's algorithm
Lesson 27 · Deadlock

Avoiding deadlock: the banker's algorithm

14 min11 steps5 questionsIntermediate
About this lesson

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.

You'll learn to

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

Fig 27 · Interactive
01 / 11

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. Who 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 - has
  3. What 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 check
  4. T1 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    5
  5. Then 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    10
  6. Safe: 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    3
  7. T2 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    2
  8. Nobody 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    4
  9. The 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    3
  10. A 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    2
  11. The 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']

Quiz Unlocks after the last step.