You are on page 1of 18

What is Banker's Algorithm?

Banker's Algorithm is used majorly in the banking system to avoid deadlock. It


helps you to identify whether a loan will be given or not.

This algorithm is used to test for safely simulating the allocation for determining
the maximum amount available for all resources. It also checks for all the possible
activities before determining whether allocation should be continued or not.

For example, there are X number of account holders of a specific bank, and the
total amount of money of their accounts is G.

When the bank processes a car loan, the software system subtracts the amount of
loan granted for purchasing a car from the total money ( G+ Fixed deposit +
Monthly Income Scheme + Gold, etc.) that the bank has.

It also checks that the difference is more than or not G. It only processes the car
loan when the bank has sufficient money even if all account holders withdraw the
money G simultaneously.

Why Banker’s algorithm is named so?


Banker’s algorithm is named so because it is used in banking system to check
whether loan can be sanctioned to a person or not. Suppose there are n number of
account holders in a bank and the total sum of their money is S. If a person applies
for a loan then the bank first subtracts the loan amount from the total money that
bank has and if the remaining amount is greater than S then only the loan is
sanctioned. It is done because if all the account holders comes to withdraw their
money then the bank can easily do it.

In other words, the bank would never allocate its money in such a way that it can
no longer satisfy the needs of all its customers. The bank would try to be in safe
state always.

Banker's Algorithm Notations


Following Data structures are used to implement the Banker’s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of
resources types.
Available : 
 It is a 1-d array of size ‘m’ indicating the number of available resources of each
type.
 Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max :
 It is a 2-d array of size ‘n*m’ that defines the maximum demand of each
process in a system.
 Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource
type Rj.
Allocation :
 It is a 2-d array of size ‘n*m’ that defines the number of resources of each type
currently allocated to each process.
 Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of
resource type Rj
Need :
  It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of
each process.
 Need [ i,   j ] = k means process Pi currently need ‘k’ instances of resource
type Rj
for its execution.
 Need [ i,   j ] = Max [ i,   j ] – Allocation [ i,   j ]

Allocationi specifies the resources currently allocated to process Pi and


Needi specifies the additional resources that process Pi may still request to complete
its task.
Banker’s algorithm consists of Safety algorithm and Resource request algorithm

Safety Algorithm:
The algorithm for finding out whether or not a system is in a safe state can be described as
follows:
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi  <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state
Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k
instances of resource type Rj. When a request for resources is made by process Pi, the
following actions are taken:
1) If Requesti <= Needi
Goto step (2) ; otherwise, raise an error condition, since the process has exceeded
its maximum claim.
2) If Requesti <= Available
Goto step (3); otherwise, Pi must wait, since the resources are not available.
3) Have the system pretend to have allocated the requested resources to process Pi
by modifying the state as
follows:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi– Requesti
Example:
Considering a system with five processes P0 through P4 and three resources of
type A, B, C. Resource type A has 10 instances, B has 5 instances and type C
has 7 instances. Suppose at time t0 following snapshot of the system has been
taken:

Question1. What will be the content of the Need matrix?


Need [i, j] = Max [i, j] – Allocation [i, j]
So, the content of Need Matrix is:

Question2.  Is the system in a safe state? If Yes, then what is the safe
sequence?
Applying the Safety algorithm on the given system,
Question3. What will happen if process P1 requests one additional instance of
resource type A and two instances of resource type C?

We must determine whether this new system state is safe. To do so, we again
execute Safety algorithm on the above data structures.
Hence the new system state is safe, so we can immediately grant the request for
process  P1 .

Example of Banker's algorithm


Assume that we have the following resources:

 5 Pen drives
 2 Printers
 4 Scanners
 3 Hard disks

Here, we have created a vector representing total resources: Available = (5, 2, 4,


3).

Assume there are four processes. The available resources are already allocated as
per the matrix table below.

Process Name Pen Drives Printer Scanner Hard disk

P 2 0 1 1

Q 0 1 0 0

R 1 0 1 1
Process Name Pen Drives Printer Scanner Hard disk

S 1 1 0 1

Total 4 2 2 3

Here, the allocated resources is the total of these columns:

Allocated = (4, 2, 2, 3).

We also create a Matrix to display the number of each resource required for all
the processes. This matrix is called Need=(3,0,2,2)

Process Name Pen Drives Printer Scanner Ha

P 1 1 0 0

Q 0 1 1 2

R 2 1 0 0

S 0 0 1 0

The available vector will be :

Available=Available- Allocated

= (5, 2, 4, 3) -(4, 2, 2, 3)

=(1, 0, 2, 0)

Resource Request Algorithm


Resource request algorithm enables you to represent the system behavior when
a specific process makes a resource request.

Let understand this by the following steps:

Step 1) When a total requested instance of all resources is lesser than the
process, move to step 2.

Step 2) When a requested instance of each and every resource type is lesser
compared to the available resources of each type, it will be processed to the
next step. Otherwise, the process requires to wait because of the unavailability
of sufficient resources.

Step 3) Resource is allocated as shown in the below given Pseudocode.

Available = Available – Request (y)


Allocation(x) = Allocation(x) + Request(x)
Need(x) = Need(x) - Request(x)

This final step is performed because the system needs to assume that resources
have been allocated. So that there should be less resources available after
allocation.

Characteristics of Banker's Algorithm


Here are important characteristics of banker's algorithm:

 Keep many resources that satisfy the requirement of at least one client
 Whenever a process gets all its resources, it needs to return them in a
restricted period.
 When a process requests a resource, it needs to wait
 The system has a limited number of resources
 Advance feature for max resource allocation

Disadvantage of Banker's algorithm


Here, are cons/drawbacks of using banker's algorithm

 Does not allow the process to change its Maximum need while processing
 It allows all requests to be granted in restricted time, but one year is a
fixed period for that.
 All processes must know and state their maximum resource needs in
advance.

Summary:
 Banker's algorithm is used majorly in the banking system to avoid
deadlock. It helps you to identify whether a loan will be given or not.
 Notations used in banker's algorithms are 1) Available 2) Max 3) Allocation
4) Need
 Resource request algorithm enables you to represent the system
behavior when a specific process makes a resource request.
 Banker's algorithm keeps many resources that satisfy the requirement of
at least one client
 The biggest drawback of banker's algorithm this that it does not allow the
process to change its Maximum need while processing.

PRACTICE PROBLEMS BASED ON BANKER’S


ALGORITHM-
 

Problem-01:
 
A single processor system has three resource types X, Y and Z, which are shared by
three processes. There are 5 units of each resource type. Consider the following
scenario, where the column alloc denotes the number of units of each resource type
allocated to each process, and the column request denotes the number of units of each
resource type requested by a process in order to complete execution. Which of these
processes will finish LAST?
1. P0
2. P1
3. P2
4. None of the above since the system is in a deadlock
 

Alloc Request

X Y Z X Y Z

P0 1 2 1 1 0 3

P1 2 0 1 0 1 2

P2 2 2 1 1 2 0

Solution-
 
According to question-
 Total = [ X Y Z ] = [ 5 5 5 ]
 Total _Alloc = [ X Y Z ] = [5 4 3]
 
Now,
Available
= Total – Total_Alloc
= [ 5 5 5 ] – [5 4 3]
=[012]
 
Step-01:
 
 With the instances available currently, only the requirement of the process P1 can be
satisfied.
 So, process P1 is allocated the requested resources.
 It completes its execution and then free up the instances of resources held by it.
 
Then,
Available
= [ 0 1 2 ] + [ 2 0 1]
=[213]
 
Step-02:
 
 With the instances available currently, only the requirement of the process P0 can be
satisfied.
 So, process P0 is allocated the requested resources.
 It completes its execution and then free up the instances of resources held by it.
 
Then-
Available
= [ 2 1 3 ] + [ 1 2 1 ]
=[334]
 
Step-03:
 
 With the instances available currently, the requirement of the process P2 can be
satisfied.
 So, process P2 is allocated the requested resources.
 It completes its execution and then free up the instances of resources held by it.
 
Then-
Available
= [ 3 3 4 ] + [ 2 2 1 ]
=[555]
 
Thus,
 There exists a safe sequence P1, P0, P2 in which all the processes can be executed.
 So, the system is in a safe state.
 Process P2 will be executed at last.
 
Thus, Option (C) is correct.
 

Problem-02:
 
An operating system uses the banker’s algorithm for deadlock avoidance when
managing the allocation of three resource types X, Y and Z to three processes P0, P1
and P2. The table given below presents the current system state. Here, the Allocation
matrix shows the current number of resources of each type allocated to each process
and the Max matrix shows the maximum number of resources of each type required by
each process during its execution.

Allocation Max

X Y Z X Y Z

P0 0 0 1 8 4 3

P1 3 2 0 6 2 0
P2 2 1 1 3 3 3

 
There are 3 units of type X, 2 units of type Y and 2 units of type Z still available. The
system is currently in safe state. Consider the following independent requests for
additional resources in the current state-
 
REQ1: P0 requests 0 units of X, 0 units of Y and 2 units of Z
REQ2: P1 requests 2 units of X, 0 units of Y and 0 units of Z
 
Which of the following is TRUE?
1. Only REQ1 can be permitted
2. Only REQ2 can be permitted
3. Both REQ1 and REQ2 can be permitted
4. Neither REQ1 nor REQ2 can be permitted
 

Solution-
 
According to question,
Available = [ X Y Z ] = [ 3 2 2 ]
Now,
Need = Max – Allocation
So, we have-
 

Allocation Max Need

X Y Z X Y Z X Y Z

P0 0 0 1 8 4 3 8 4 2

P1 3 2 0 6 2 0 3 0 0

P2 2 1 1 3 3 3 1 2 2
 
Currently, the system is in safe state.
(It is given in question. If we want, we can check)
 
Checking Whether REQ1 Can Be Entertained-
 
 Need of P0 = [ 0 0 2 ]
 Available = [ 3 2 2 ]
 
Clearly,
 With the instances available currently, the requirement of REQ1 can be satisfied.
 So, banker’s algorithm assumes that the request REQ1 is entertained.
 It then modifies its data structures as-
 

Allocation Max Need

X Y Z X Y Z X Y Z

P0 0 0 3 8 4 3 8 4 0

P1 3 2 0 6 2 0 3 0 0

P2 2 1 1 3 3 3 1 2 2

 
Available
=[322]–[002]
=[320]
 
 Now, it follows the safety algorithm to check whether this resulting state is a safe state
or not.
 If it is a safe state, then REQ1 can be permitted otherwise not.
 
Step-01:
 
 With the instances available currently, only the requirement of the process P1 can be
satisfied.
 So, process P1 is allocated the requested resources.
 It completes its execution and then free up the instances of resources held by it.
 
Then-
Available
= [ 3 2 0 ] + [ 3 2 0 ]
=[640]
 
Now,
 It is not possible to entertain any process.
 The system has entered the deadlock state which is an unsafe state.
 Thus, REQ1 will not be permitted.
 
Checking Whether REQ2 Can Be Entertained-
 
 Need of P1 = [ 2 0 0 ]
 Available = [ 3 2 2 ]
 
Clearly,
 With the instances available currently, the requirement of REQ1 can be satisfied.
 So, banker’s algorithm assumes the request REQ2 is entertained.
 It then modifies its data structures as-
 

Allocation Max Need

X Y Z X Y Z X Y Z

P0 0 0 1 8 4 3 8 4 2
P1 5 2 0 6 2 0 1 0 0

P2 2 1 1 3 3 3 1 2 2

 
Available
=[322]–[200]
=[122]
 
 Now, it follows the safety algorithm to check whether this resulting state is a safe state
or not.
 If it is a safe state, then REQ2 can be permitted otherwise not.
 
Step-01:
 
 With the instances available currently, only the requirement of the process P1 can be
satisfied.
 So, process P1 is allocated the requested resources.
 It completes its execution and then free up the instances of resources held by it.
 
Then-
Available
= [ 1 2 2 ] + [ 5 2 0 ]
=[642]
 
Step-02:
 
 With the instances available currently, only the requirement of the process P2 can be
satisfied.
 So, process P2 is allocated the requested resources.
 It completes its execution and then free up the instances of resources held by it.
 
Then-
Available
= [ 6 4 2 ] + [ 2 1 1 ]
=[853]
 
Step-03:
 
 With the instances available currently, the requirement of the process P0 can be
satisfied.
 So, process P0 is allocated the requested resources.
 It completes its execution and then free up the instances of resources held by it.
 
Then-
Available
= [ 8 5 3 ] + [ 0 0 1 ]
=[854]
 
Thus,
 There exists a safe sequence P1, P2, P0 in which all the processes can be executed.
 So, the system is in a safe state.
 Thus, REQ2 can be permitted.
 
Thus, Correct Option is (B).
 

Problem-03:
 
A system has 4 processes and 5 allocatable resource. The current allocation and
maximum needs are as follows-

Allocated Maximum

A 1 0 2 1 1 1 1 2 1 3

B 2 0 1 1 0 2 2 2 1 0

C 1 1 0 1 1 2 1 3 1 1
D 1 1 1 1 0 1 1 2 2 0

 
If Available = [ 0 0 X 1 1 ], what is the smallest value of x for which this is a safe state?
 

Solution-
 
Let us calculate the additional instances of each resource type needed by each process.
We know,
Need = Maximum – Allocation
 
So, we have-

Need

A 0 1 0 0 2

B 0 2 1 0 0

C 1 0 3 0 0

D 0 0 1 1 0

 
Case-01: For X = 0
 
If X = 0, then-
Available
=[00011]
 
 With the instances available currently, the requirement of any process can not be
satisfied.
 So, for X = 0, system remains in a deadlock which is an unsafe state.
 
Case-02: For X = 1
 
If X = 1, then-
Available
=[00111]
 
Step-01:
 
 With the instances available currently, only the requirement of the process D can be
satisfied.
 So, process D is allocated the requested resources.
 It completes its execution and then free up the instances of resources held by it.
 
Then-
Available
= [ 0 0 1 1 1 ] + [ 1 1 1 1 0 ]
=[11221]
 
 With the instances available currently, the requirement of any process can not be
satisfied.
 So, for X = 1, system remains in a deadlock which is an unsafe state.
 
Case-02: For X = 2
 
If X = 2, then-
Available
=[00211]
 
Step-01:
 
 With the instances available currently, only the requirement of the process D can be
satisfied.
 So, process D is allocated the requested resources.
 It completes its execution and then free up the instances of resources held by it.
 
Then-
Available
= [ 0 0 2 1 1 ] + [ 1 1 1 1 0 ]
=[11321]
 
Step-02:
 
 With the instances available currently, only the requirement of the process C can be
satisfied.
 So, process C is allocated the requested resources.
 It completes its execution and then free up the instances of resources held by it.
 
Then-
Available
= [ 1 1 3 2 1 ] + [ 1 1 0 1 1 ]
=[22332]
 
Step-03:
 
 With the instances available currently, the requirement of both the processes A and B
can be satisfied.
 So, processes A and B are allocated the requested resources one by one.
 They complete their execution and then free up the instances of resources held by it.
 
Then-
Available
= [ 2 2 3 3 2 ] + [ 1 0 2 1 1 ] + [ 2 0 1 1 0 ]
=[52653]
 
Thus,
 There exists a safe sequence in which all the processes can be executed.
 So, the system is in a safe state.
 Thus, minimum value of X that ensures system is in safe state = 2.
 

You might also like