# Bankers Algorithm-Is the system in safe state?

Could somebody please provide a step-through approach to solving the following problem using the Banker's Algorithm?

A system contains 10 units of resource class Ru. The resource requirements of three user processes P1, P2 and P3 are as follows

                      |   P1  |  P2  |   P3   |
Max Requirement       |   8   |  7   |   5    |
Current Allocation    |   3   |  1   |   3    |
Balance Requirement   |   5   |  6   |   2    |
New Request Made      |   1   |  0   |   0    |


Using Banker’s algorithm, determine if the projected allocation state is safe and whether the request of P1 will be granted or not.

My solution:

total recources :10 total allocation :7 available: 10 -7 = 3

Need
P1     5
P2     6
P3     2

3>2 so P3 can run now available is 6
6>5 so P1 can run now availabl 9
9>6 so P2 can run


System is safe state,Hence P1 will be granted.

But I'm not sure if my solution is true.

• What did you try? Where did you get stuck? The point of this kind of exercise is to learn by doing and you're not likely to get much real benefit from somebody else solving it for you. At the same time, we don't know what you're having difficulty with, so it's hard to give any help. – David Richerby Aug 6 '16 at 18:56
• @DavidRicherby I have edited the question with my solution. – ern Aug 7 '16 at 10:44

you can say there is no deadlock if there is a safe sequence for the completion, after $P1$ request is granted there must be a safe sequence where by which every process is completed.

After P1 request is granted
|   P1  |  P2  |   P3   |
Max Requirement       |   8   |  7   |   5    |
Current Allocation    |   4   |  1   |   3    |
Balance Requirement   |   4   |  6   |   2    |
now the resources left over are 10-4-1-3=2


so can we complete with 2 resources? any sequence is possible?

YES

if P3 is given 2 resources it will complete and release all the current allocated resources so our resource count becomes 5 , now we have 5 and we can complete P1 and get back 4 now our resource count is 9 and i need just 6 to complete P2 so a safe sequence is possible. so safe sequence will be

$P3 \rightarrow P1\rightarrow P2$

so $P1$ request is granted

First P3 will be granted as 3>2 , after that the total no of available resource is 6

then P1 will be granted as 6>5 , after that the total no of available resources is 9

then P2 will be granted as 9>6 .

So the safe sequence for the given problem is P3,P1,P2 .

• Although, in end you are getting a safe sequence but this is not the proper way. First the resource is assumed to be granted to P1 (i.e 1 resource is granted to P1). After this available resource count becomes 2. Now, you start finding a safe sequence with 2 available resources. See @Pavan Kumar Munnam's answer. – sameerkn Feb 3 '17 at 6:37