Banker’s Algorithm -The Safety Algorithm
Given,
Resource Type Instances
A 13
B 9
C 8
Processes Allocation
A B C
Max
A B C
Available
A B C
Need
A B C
P0 3 0 1 10 7
4
6 2 2 7 7 3
P1 1 2 0 8 5 3 7 3 3
P2 2 1 3 6 3 2 4 2 1
P3 0 3 0 9 6 3 9 3 3
P4 1 1 2 7 4 5 6 3 3
Table:
3.
Step 1:
We know,Work = Available = 6 2 2
Finish = False
Step 2:
Need (i) < = Work
For P0, 7 7 3 <= 6 2 2 That’s false.
For P1, 7 7 3 <= 6 2 2 That’s false.
For P2, 4 2 1 <= 6 2 2 That’s true.
So, Work = Work + Allocation (P2) = 6 2 2 + 2 1 3 = 8 3
5
For P3, 9 3 3 <= 8 3 5 That’s False.
For P4, 6 3 3 <= 8 3 5 That’s True.
4.
So, Work =Work + Allocation (P4) = 8 3 5 + 1 1 2 = 9 4 7
For P0, 7 7 3 <= 9 4 7 That’s False.
For P1, 7 3 3 <= 9 4 7 That’s True.
So, Work = Work + Allocation (P1) = 9 4 7 + 1 2 0 = 10 6 7
For P3, 9 3 3 <= 10 6 7 That’s True.
So, Work = Work + Allocation (P3) = 10 6 7 + 0 3 0 = 10 9 7
For P0, 7 7 3 <= 10 9 7 That’s True.
So, Work = Work + Allocation (P0) = 10 9 7 + 3 0 1 = 13 9 8
The OS is in Safe State since, <P2, P4, P1, P3, P0> is a Safe Sequence.
5.
Now, at sometimeP1
requests an additional 2 A
instances and 1 C instance.
That means request from
P1 is (2, 0, 1).
6.
Then the tablewill be like bellow:
Processes Allocation
A B C
Max
A B C
Available
A B C
Need
A B C
P0 3 0 1 10 7 4 4 2 1 7 7 3
P1 3 2 1 8 5 3 5 3 2
P2 2 1 3 6 3 2 4 2 1
P3 0 3 0 9 6 3 9 3 3
P4 1 1 2 7 4 5 6 3 3
7.
Step 1:
We know,Work = Available= 4 2 1
Finish = False
Step 2:
Need (i) <= Work
For P0, 7 7 3 <= 4 2 1 That is False.
For P1, 5 3 2 <= 4 2 1 That is False.
For P2, 4 2 1 <= 4 2 1 That’s True.
So, Work = Work + Allocation (P2) = 4 2 1 + 2 1 3 = 6 3 4
For P3, 9 3 3 <= 6 3 4 That’s False.
For P4, 6 3 3 <= 6 3 4 That’s True.
So, Work = Work + Allocation (P4) = 6 3 4 + 1 1 2 = 7 4 6
8.
For P0, 77 3 <= 7 4 6 That’s False.
For P1, 5 3 2 <= 7 4 6 True.
So, Work = Work + Allocation (P1) = 7 4 6 + 3 2 1 = 10 6 7
For P3, 9 3 3 <= 10 6 7 That’s True.
So, Work = Work + Allocation (P3) = 10 6 7 + 0 3 0 = 10 9 7
For P0, 7 7 3 <= 10 9 7 That’s True.
So, Work = Work + Allocation (P0) = 10 9 7 + 3 0 1 = 13 9 8
The OS is in Safe State since, <P2, P4, P1, P3, P0> is a Safe Sequence.