Banker’s Algorithm in Operating System [Example]
โก Smart Summary
Banker’s Algorithm is a deadlock-avoidance method that tests whether allocating resources keeps the system in a safe state. Named after banking, it grants a request only if enough resources remain to satisfy every process.

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 in 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 grants the car loan only if the remaining money still exceeds G, so all account holders can withdraw G at any time.
Banker’s Algorithm Notations
Here is some important notation used in the Banker’s algorithm:
- X: Indicates the total number of processes in the system.
- Y: Indicates the total number of resources present in the system.
Available
[1:Y] indicates how many instances of each resource type are available.
Max
[1:X, 1:Y]: Expresses the maximum number of resources of type j that process i can request.
Allocation
[1:X, 1:Y]: Indicates the resources of type j currently allocated to process i.
Need
Expresses how many more resources of each type process i still needs to complete its task.
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 |
| S | 1 | 1 | 0 | 1 |
| Total | 4 | 2 | 2 | 3 |
Here, the allocated resources are 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 | Hard disk |
|---|---|---|---|---|
| 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
The Resource request algorithm enables you to represent the system behavior when a specific process makes a resource request.
Let us understand this by the following steps:
Step 1) When the total requested instances of all resources are lesser than the process, move to step 2.
Step 2) When the requested instances of each and every resource type are lesser compared to the available resources of each type, it will be processed to the next step. Otherwise, the process needs to wait because of the unavailability of sufficient resources.
Step 3) The 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 are fewer resources available after allocation.
Characteristics of Banker’s Algorithm
Here are the important characteristics of the banker’s algorithm:
- Keeps 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 may need to wait.
- The system has a limited number of resources.
- It offers an advanced feature for maximum resource allocation.
Disadvantage of Banker’s algorithm
Here are the cons/drawbacks of using the banker’s algorithm:
- It does not allow the process to change its Maximum need while processing.
- It allows all requests to be granted in a restricted time, but one year is a fixed period for that.
- All processes must know and state their maximum resource needs in advance.
