"Consider a system with five processes P0 through P4 and three resource types A, B, C. Resource type A has 10 instances, resource type B has 5 instances, and resource type C has 7 instances. Suppose that, at time T0, the following snapshot of the system has been taken:" STATE 0 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 7 4 3 P1 2 0 0 3 2 2 1 2 2 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 =========================================================================== "We claim that the system is currently in a safe state. Indeed, the sequence <P1, P3, P4, P2, P0> satisfies the safety criteria. +++++++++++++++++++++++++++++++++++ Verification of the claim above: The "Need" of P1 is 1 2 2, which nowhere exceeds 3 3 2 = Available. Thus P1 can be given its Need, and made to finish. This will lead to the following state, obtained by subtracting the 2 0 0 allocation of P1, and adding it to the 3 3 2 currently available. (We also add it to the "Need" 1 2 2 of the now finished P1.) STATE 1 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C P0 0 1 0 7 5 3 5 3 2 7 4 3~~P1 0 0 0 3 2 2 3 2 2~~P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 =========================================================================== Now the "Need" of P3 is 0 1 1, which nowhere exceeds 5 3 2 = Available. Thus P3 can be given its Need, and made to finish. This will lead to the following state, obtained by subtracting the 2 1 1 allocation of P3, and adding it to the 5 3 2 currently available. STATE 2 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 7 4 3~~P1 0 0 0 3 2 2 3 2 2~~P2 3 0 2 9 0 2 6 0 0~~P3 0 0 0 2 2 2 2 2 2~~P4 0 0 2 4 3 3 4 3 1 =========================================================================== Now the "Need" of P4 is 4 3 1, which nowhere exceeds 7 4 3 = Available. Thus P4 can be given its Need, and made to finish. This will lead to the following state, obtained by subtracting the 0 0 2 allocation of P4, and adding it to the 7 4 3 currently available. STATE 3 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 5 7 4 3~~P1 0 0 0 3 2 2 3 2 2~~P2 3 0 2 9 0 2 6 0 0~~P3 0 0 0 2 2 2 2 2 2~~~~P4 0 0 0 4 3 3 4 3 3~~=========================================================================== Now the "Need" of P2 is 6 0 0, which nowhere exceeds 7 4 5 = Available. Thus P2 can be given its Need, and made to finish. This will lead to the following state, obtained by subtracting the 3 0 2 allocation of P2, and adding it to the 7 4 5 currently available. STATE 4 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C P0 0 1 0 7 5 3 10 4 7 7 4 3~~P1 0 0 0 3 2 2 3 2 2~~~~P2 0 0 0 9 0 2 9 0 2~~~~P3 0 0 0 2 2 2 2 2 2~~~~P4 0 0 0 4 3 3 4 3 3~~=========================================================================== Now the "Need" of P0 is 7 4 3, which nowhere exceeds 10 4 7 = Available. Thus P0 can be given its Need, and made to finish. This will lead to the following state, obtained by subtracting the 0 1 0 allocation of P0, and adding it to the 10 4 7 currently available. STATE 5 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C~~P0 0 0 0 7 5 3~~10 5 7~~7 5 3~~~~P1 0 0 0 3 2 2 3 2 2~~~~P2 0 0 0 9 0 2 9 0 2~~~~P3 0 0 0 2 2 2 2 2 2~~~~P4 0 0 0 4 3 3 4 3 3~~=========================================================================== end of Verification of the claim +++++++++++++++++++++++++++++++++++ Here again is the current state of the system. We have just finished verifying that this state is safe. STATE 0 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 7 4 3 P1 2 0 0 3 2 2 1 2 2 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 =========================================================================== "Suppose now that process P1 requests one additional instance of resource type A and two instances of resource type C, so Request[1] = (1 0 2). To decide whether this request can be immediately granted, we first check that Request[1] <= Available (that is (1 0 2) <= (3 3 2), which is true. [We also have to check that Request[1] <= Need[1] (that is (1 0 2) <= (1 2 2)). This too is true.] We then pretend that this request has been fulfilled, and we arrive at the following new state:" STATE 6 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C P0 0 1 0 7 5 3 2 3 0 7 4 3 P1 3 0 2 3 2 2 0 2 0 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 =========================================================================== "We must determine whether this new system state is safe. To do so, we execute our safety algorithm and find that the sequence <P1, P3, P4, P0, P2> satisfies our safety requirement. Hence we can immediately grant the request of process P1." +++++++++++++++++++++++++++++++++++ Verification of the findings of the safety algorithm. The "Need" of P0 is 7 4 3, which is too great for the 2 3 0 that is available. Therefore, the safety algorithm would skip over P0 in its search for the first process to have its finish simulated. The "Need" of P1 is 0 2 0, which nowhere exceeds 2 3 0 = Available. Thus P1 can be given its Need, and made to finish. This will lead to the following state, obtained by subtracting the 3 0 2 allocation of P1, and adding it to the 2 3 0 currently available. STATE 7 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C P0 0 1 0 7 5 3 5 3 2 7 4 3~~P1 0 0 0 3 2 2 3 2 2~~P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 =========================================================================== The reader will verify that STATE 7 above is identical to STATE 1 further above. Therefore it is clear that we can simulate the finishing of P3 and P4, just as we did above, and arrive at the following state, STATE 3. Note that P1, P3, P4 is the same order that the safety algorithm would use to simulate finishing these processes, assuming that it searches sequentially for a process by starting at the beginning of the list and NOT starting over at the beginning again when it has to search for another process. STATE 3 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 5 7 4 3~~P1 0 0 0 3 2 2 3 2 2~~P2 3 0 2 9 0 2 6 0 0~~P3 0 0 0 2 2 2 2 2 2~~~~P4 0 0 0 4 3 3 4 3 3~~=========================================================================== If we now continue to follow the actions that the safety algorithm would take, we see that the next process to be checked is P0 (we assume that the search, having reached the end, now goes back to the beginning.) The "Need" of P0 is 7 4 3, which is now less than 7 4 5 = Available. Thus P0 can be given its Need, and made to finish. This will lead to the following state, obtained by subtracting the 0 1 0 allocation of P0, and adding it to the 7 4 5 currently available, and also adding it to the current "Need" 7 4 3 of P0. STATE 8 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C~~P0 0 0 0 7 5 3~~7 5 5~~7 5 3~~~~P1 0 0 0 3 2 2 3 2 2~~P2 3 0 2 9 0 2 6 0 0~~P3 0 0 0 2 2 2 2 2 2~~~~P4 0 0 0 4 3 3 4 3 3~~=========================================================================== Finally, the algorithm would arrive at P2. The "Need" of P2 is 6 0 0, which is less than 7 5 5 = Available. Thus P2 can be given its Need, and made to finish. This will lead to the following state, obtained by subtracting the 3 0 2 allocation of P2, and adding it to the 7 5 5 currently available. STATE 9 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C~~P0 0 0 0 7 5 3~~10 5 7~~7 5 3~~~~P1 0 0 0 3 2 2 3 2 2~~~~P2 0 0 0 9 0 2 9 0 2~~~~P3 0 0 0 2 2 2 2 2 2~~~~P4 0 0 0 4 3 3 4 3 3~~=========================================================================== This completes the verification that STATE 6 is safe, and that <P1, P3, P4, P0, P2> is a safe sequence for STATE 6. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Here again is STATE 6. STATE 6 should now be considered the current state of the system, because the banker's algorithm has told us it is safe to grant the request of P1 and place the system into STATE 6. STATE 6 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C P0 0 1 0 7 5 3 2 3 0 7 4 3 P1 3 0 2 3 2 2 0 2 0 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 =========================================================================== "You should be able to see, however, that when the system is in this state, a request for (3 3 0) by P4 cannot be granted, since the resources are not available. A request for (0 2 0) by P0 cannot be granted, even though the resources are available, since the resulting state is unsafe." +++++++++++++++++++++++++++++++++++ Verification of the above. If we simulate granting a request of 0 2 0 by P0, we get: STATE 10 =========================================================================== pid Allocation Max Available Need ------------ ------------ ------------ ------------ A B C A B C A B C A B C P0 0 3 0 7 5 3 2 1 0 7 2 3 P1 3 0 2 3 2 2 0 2 0 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 =========================================================================== Now the "Need" of EVERY process exceeds what is available in either category A, B or C. Thus the safety algorithm will tell us that the system is not safe. Indeed, if all the processes now request their Need, all the processes will have to wait, and it is clear that they will then all be deadlocked. More complex examples are possible. One may be able to "peel off" a few processes - to find apartialsafe sequence. However it may then become impossible to proceed further. When this happens, it is proof that the system is unsafe. The safety checking algorithm (and the deadlock detection algorithm) are "greedy" - if there is a safe sequence we'll find one with no need to back up and try different choices.