Banker's algorithm calculator
The banker's algorithm grants a resource only if every process could still finish afterwards. Enter the Allocation and Max matrices and Available from your exercise to get the Need matrix, the safety check step by step and a safe sequence, then test a request or check your own answer.
| Allocation | Max | Need | Finishes | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| A | B | C | A | B | C | A | B | C | ||
| P0 | 1 | 0 | 2 | 5 | 3 | 4 | 4 | 3 | 2 | |
| P1 | 2 | 1 | 1 | 3 | 2 | 2 | 1 | 1 | 1 | |
| P2 | 0 | 1 | 0 | 7 | 2 | 3 | 7 | 1 | 3 | |
| P3 | 3 | 0 | 1 | 4 | 1 | 2 | 1 | 1 | 1 | |
| P4 | 0 | 0 | 2 | 2 | 2 | 4 | 2 | 2 | 2 | |
Work now: (2, 2, 2) · next: is P0's Need (4, 3, 2) ≤ Work?
- –Work starts as Available, (2, 2, 2). Press Next check to test each process in turn.
How to use the calculator
Paste the Allocation and Max matrices from your exercise, one row per process starting with P0, numbers separated by spaces; row labels such as "P0:" are ignored. Put Available on the last line, or switch to "Total resources" if your exercise gives the totals, and press Calculate. "Next check" runs the safety algorithm one test at a time, with Work after each process finishes.
- Textbooks scan processes in different orders and so print different safe sequences. Choose the order your course uses, or type your own sequence under "Check your safe sequence" to see whether it is valid.
- "Test a request" applies the resource-request algorithm; if the request is granted, "Apply it" makes it the new state, so you can follow a chain of requests.
What is the banker's algorithm?
The banker's algorithm is a deadlock-avoidance algorithm for a system with n processes and m types of resource. Each process declares in advance the most of each resource it will ever hold, its Max. The system tracks what each holds now, its Allocation, and what is free, Available. A request is granted only if the system stays in a safe state: one from which some order exists in which every process can get its full Max, finish and release everything.
The name comes from a bank that never lends out its cash in a way that would leave it unable to satisfy all its customers' credit limits, provided each repays once it has drawn its full line.
The safety algorithm
- Compute Need = Max − Allocation for every process. Set Work = Available and mark every process unfinished.
- Find an unfinished process whose Need is at most Work in every resource type.
- If there is one, assume it runs to completion: add its Allocation to Work, mark it finished, and go back to step 2.
- If none is left that can run, the state is safe exactly when every process has finished; the order they finished in is a safe sequence.
Step 2 does not say which process to pick when several qualify, and the choice changes the sequence but never the verdict. For the default exercise above, scanning from P0 again after each finish gives ⟨P1, P0, P3, P2, P4⟩, carrying on from the next process gives ⟨P1, P3, P4, P0, P2⟩, and the state has 25 valid safe sequences in total, counted by trying every order. If your answer differs from the book's, check it here: it may be just as correct.
Granting a request
When process Pi asks for a vector Request:
- If Request exceeds Pi's Need in any resource, it is an error: Pi has exceeded its declared maximum.
- If Request exceeds Available, Pi must wait.
- Otherwise, pretend to grant it: Available −= Request, Allocation += Request, Need −= Request, and run the safety algorithm. If the new state is safe, the request is granted; if not, Pi waits and the old state is restored.
All four outcomes from the default state, found by the model:
| Outcome | Request | Result |
|---|---|---|
| Granted | P0 asks (1, 0, 0) | Pretend to grant it: Available becomes (1, 2, 2). The new state is safe, so the request is granted. |
| Unsafe, so wait | P0 asks (2, 0, 0) | Pretend to grant it: Available becomes (0, 2, 2). The new state is unsafe: none of P0, P1, P2, P3, P4 can be sure of getting its full Need. So P0 must wait, and the old state is restored. |
| Not available, so wait | P0 asks (3, 0, 0) | P0 asks for (3, 0, 0) but only (2, 2, 2) is available, so P0 must wait. |
| Exceeds maximum claim | P0 asks (0, 0, 3) | P0 asks for (0, 0, 3) but its remaining Need is (4, 3, 2): it has exceeded its maximum claim, which is an error. |
Unsafe is not deadlocked
A safe state guarantees that deadlock cannot happen whatever the processes ask for, within their maxima. An unsafe state only loses that guarantee. With the same Allocation and Max as the default exercise but Available reduced to (1, 0, 0), no process's remaining Need fits, so the state is unsafe and has 0 safe sequences. Yet nothing is deadlocked: every process may still finish if it happens to ask for less than its maximum. The banker's algorithm refuses to enter such states at all, which makes it conservative: it sometimes makes a process wait when granting the request would have worked out.
In practice
General-purpose operating systems do not use the banker's algorithm. Processes do not declare their maximum needs in advance, they come and go, resources are added and removed, and running the safety check on every request costs about m × n² operations. Real systems prevent deadlock instead, for example by acquiring locks in a fixed global order, or detect it after the fact, as databases do by finding cycles in the wait-for graph and aborting one transaction. The algorithm survives in courses because it shows exactly what a safe state is, and in specialised systems where demands are known ahead of time.
History
Edsger Dijkstra developed the algorithm in the 1960s while designing the THE multiprogramming system, and described it in a Dutch manuscript, EWD108, whose title translates as "An algorithm for the prevention of the deadly embrace". It was first stated for a single resource type; A. Nico Habermann generalised it to several types in 1969. Coffman, Elphick and Shoshani's 1971 survey "System Deadlocks" set it alongside prevention and detection as the three approaches to deadlock.
Banker's algorithm questions and answers
How do you find a safe sequence with the banker’s algorithm?
Compute Need = Max − Allocation. Set Work = Available. Repeatedly find a process that has not finished and whose Need is at most Work in every resource; let it finish and add its Allocation to Work. If every process finishes, the state is safe and the order is a safe sequence. In this page’s example the check finds ⟨P1, P0, P3, P2, P4⟩.
Can there be more than one safe sequence?
Usually, yes. Any order in which each process’s Need fits in the Work available at its turn is valid, and textbooks print only the one their scanning order happens to find. The example state has 25 safe sequences; scanning from P0 again after each process finds ⟨P1, P0, P3, P2, P4⟩, and carrying on from the next process finds ⟨P1, P3, P4, P0, P2⟩. Both are correct, and this page can check any sequence you type.
How is a resource request handled?
Three checks. If the request is more than the process’s remaining Need, it is an error: the process exceeded its maximum claim. If it is more than Available, the process waits. Otherwise the system pretends to grant it, subtracting it from Available and Need and adding it to Allocation, and runs the safety algorithm: if the new state is safe the request is granted, and if not the process waits and the old state is restored.
Is an unsafe state a deadlock?
No. An unsafe state is one from which the system cannot guarantee that every process finishes, if each one asks for its full maximum. Deadlock may follow, but processes might finish anyway by asking for less. The banker’s algorithm avoids deadlock by never entering an unsafe state, which is more cautious than necessary.
What is the difference between deadlock avoidance, prevention and detection?
Prevention removes one of the four necessary conditions for good, for example by making every process take locks in one global order. Avoidance, like the banker’s algorithm, allows all conditions but grants requests only when the result is safe, which requires knowing each process’s maximum in advance. Detection lets deadlocks happen, finds cycles of waiting, and recovers by aborting or preempting a process.
Why is the banker’s algorithm rarely used in real operating systems?
It needs every process to declare its maximum demand for every resource before it starts, a fixed number of processes and resources, and a safety check costing about m × n² operations on every request. General-purpose operating systems have none of those, so they rely on prevention by lock ordering and on detection instead.
Further reading
- Dijkstra, EWD108Een algorithme ter voorkoming van de dodelijke omarmingThe original manuscript, in Dutch, in the E. W. Dijkstra Archive.
- Coffman, Elphick and Shoshani, Computing Surveys, 1971System deadlocksThe four necessary conditions, and prevention, avoidance and detection.
- Arpaci-Dusseau, Operating Systems: Three Easy PiecesCommon concurrency problemsA free chapter on deadlock and how real systems avoid it.