Bankers Algorithm solved problem
Bankers Algorithm solved problem
Bankers' algorithm
Let us assume that there are 5 processes, P1, P2, P3, P4, and P5 have resources A, B, and
C allocated to each process. Their maximum need is provided and we need to use Banker's
algorithm to find the remaining need and get a sequence of processes that is in a safe
state. Total resources available are A=10, B=5 and C=7.
Let's see how the algorithm works by finding the available resources and filling in the table
below.
Now we have to check which processes can utilize the available resources. Compare the
Remaining needs of each process with the available resources.
P1 needs 7, 4, and 3 but available are 3, 3, 2. It can't use the resources. P2 needs 1, 2, and 2,
and available are 3, 3, and 2. It can be used.
So add P2 as the first process in the sequence < P2 >
and find newly available from P2 and perform the above steps again. new available=
allocation of P2+ available (2+3),(0+3),(0+2)=[ 5, 3, 2 ]
2nd iteration
Allocation Max Need Available Remaining need
Processes
ABC ABC ABC ABC
P1 010 753 322 743
P2 200 322 532 122
P3 302 902 600
P4 211 422 211
P5 002 533 531
3rd iteration
Allocation Max Need Available Remaining need
Processes
ABC ABC ABC ABC
P1 010 753 322 743
P2 200 322 532 122
P3 302 902 743 600
P4 211 422 211
P5 002 533 531
4th iteration
For P1, available in 7,4,5, and required is 7, 4, 3. Hence P1 can utilize the resources.
5th iteration
Allocation Max Need Available Remaining need
Processes
ABC ABC ABC ABC
P1 010 753 322 743
P2 200 322 532 122
P3 302 902 743 600
P4 211 422 745 211
P5 002 533 755 531
For P3 available is 7, 5, 5, and required is 6, 0, 0. Hence, it can use the resources. Add P3 to
the sequence. < P2, P4, P5, P1, P3 >
Hence, the order of processes to occur to avoid deadlock is < P2, P4, P5, P1, P3 >.
Note: We have to always check current availability >= Remaining need. If the condition is
false, there is a deadlock, and is in the unsafe state. If the condition