1need = max - has2if need <= free:3 free += hasBANKfree3T0T1max10has5needT2max4has2needmax9has2need
Back: the safety check and the bank of free unitsFront: threads T0, T1, T2 with one slot per unit of their maximumCoins: units lent and returned; ✓ finished in the check, clock waitingAt the back, the safety check on a card and the bank with the free units as coins. In front, three threads with one slot per unit of their stated maximum, and their max, has and need. The banker plays the loans out: a thread whose need fits gets the coins, finishes and returns everything. T1, T0, T2 works, so the state is safe. T2's request for one more would leave no order, so it waits; T1's request is safe and 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.

The banker

  1. 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.

The safety check

Granting requests

What happens

;; step 1/11 · banker
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

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