Banker's Algorithm
First Calculate the Need Matrix
Need [i] = Max [i] - Allocation [I]
Need for P0: (4, 3, 2, 6) – (1, 2, 0, 2) = (3, 1, 2, 4)
Need for P1: (2, 4, 1, 4) – (0, 1, 1, 2) = (2, 3, 0, 2)
Need for P2: (3, 6, 5, 1) – (0, 2, 4, 0) = (3, 4, 1, 1)
Need for P3: (2, 6, 2, 4) – (1, 2, 0, 1) = (1, 4, 2, 3)
Need for P4: (3, 1, 1, 3) – (1, 0, 0, 2) = (2, 1, 1, 3)
Need Matrix looks like in the following manner:
![Need Matrix](https://i.imgur.com/00dh7dY.png)
1] For Available Resources = (4, 2, 5, 3)
Now check if each type of resource request is available for each process or not.
Step 1: For Process P0:
Need <= Available
3, 1, 2, 4 <= 4, 2, 5, 3 condition is False.
Step 2: For Process P1:
Need <= Available
2, 3, 0, 2 <= 4, 2, 5, 3 condition is False.
Step 3: For Process P2:
Need <= Available
3, 4, 1, 1 <= 4, 2, 5, 3 condition is False.
Step 4: For Process P3:
Need <= Available
1, 4, 2, 3 <= 4, 2, 5, 3 condition is False.
Step 5: For Process P4:
Need <= Available
2, 1, 1, 3 <= 4, 2, 5, 3 condition is True.
New available = available + Allocation
(4, 2, 5, 3) + (1, 0, 0, 2) = >5, 2, 5, 5
Now, again examine each type of resource request for processes P0, P1, P2, P3.
Step 6: For Process P0:
P0 Need <= Available
3, 1, 2, 4 <= 5, 2, 5, 5 condition is True.
New available = available + Allocation
(5, 2, 5, 5) + (1, 2, 0, 2) = > 6, 4, 5, 7
Step 7: For Process P1:
P1 Need <= Available
2, 3, 0, 2 <= 6, 4, 5, 7 condition is True.
New available = available + Allocation
(6, 4, 5, 7) + (0, 1, 1, 2) => 6, 5, 6, 9
Step 8: For Process P2:
P2 Need <= Available
3, 4, 1, 1<= 6, 5, 6, 9 condition is True.
New available = available + Allocation
(6, 5, 6, 9) + (0, 2, 4, 0) => 6, 7, 10, 9
Step 9: For Process P3:
P3 Need <= Available
1, 4, 2, 3 <= 6, 7, 10, 9 condition is True.
New available = available + Allocation
(6, 7, 10, 9) + (1, 2, 0, 1) => 7, 9, 10, 10
Hence, execute the banker's algorithm to find the safe state and the safe sequence like P4, P0, P1, P2, and P3 for available resources (4, 2, 5, 3)
Order of Safe Sequence = P4, P0, P1, P2, P3
The system allocates all the needed resources to each process. So, we can say that the system is in a safe state.
2] For Available Resources = (1, 5, 0, 2)
Now check if each type of resource request is available for each process or not.
Step 1: For Process P0:
Need <= Available
3, 1, 2, 4 <= 1, 5, 0, 2 condition is False.
Step 2: For Process P1:
Need <= Available
2, 3, 0, 2 <= 1, 5, 0, 2 condition is False.
Step 3: For Process P2:
Need <= Available
3, 4, 1, 1 <= 1, 5, 0, 2 condition is False.
Step 4: For Process P3:
Need <= Available
1, 4, 2, 3 <= 1, 5, 0, 2 condition is False.
Step 5: For Process P4:
Need <= Available
2, 1, 1, 3 <= 1, 5, 0, 2 condition is False.
Here, not a single process fulfills the required condition of the banker’s algorithm that indicates the system is in an unsafe condition.
The system cannot allocate all the needed resources to each process. So, we can say that the system is in an unsafe state condition and can enter into deadlock condition