Banker’s Algorithm in Operating Systems
The Banker’s Algorithm is a deadlock avoidance method used in operating systems. It ensures that
resources are allocated safely to processes without leading to a deadlock situation. The algorithm
checks before allocation whether the system will remain in a safe state.
It is called the Banker’s Algorithm because it works like a banker: a banker will give a loan to a
customer only if he is sure that the loan can be repaid in the future. Similarly, the operating system
grants resources to a process only if it is safe to do so.
■ Important Definitions
1. Available Definition: A vector that shows the number of available instances of each type of
resource in the system. Example: If the system has 10 printers and 7 are already allocated, then
Available = 3.
2. Max Definition: A matrix that defines the maximum demand of each process for every resource
type during its execution. Example: If process P1 may need up to 3 printers, then Max for printers =
3.
3. Allocation Definition: A matrix that shows the number of resources currently allocated to each
process. Example: If P1 is already holding 1 printer, then Allocation = 1.
4. Need Definition: A matrix that represents the remaining resource needs of each process.
Formula: Need[i][j] = Max[i][j] - Allocation[i][j] Example: If P1’s maximum demand is 3 printers and
its current allocation is 1, then Need = 2.
■ Safe State
Definition: A system is in a safe state if there exists a sequence of all processes (called a safe
sequence) such that each process can obtain the required resources, finish execution, and then
release resources for the next process. If such a sequence does not exist, the system is unsafe and
may lead to deadlock.
■ Steps of Banker’s Algorithm
1. Check Request – When a process requests resources, verify if: - Request ≤ Need - Request ≤
Available If not, deny the request.
2. Pretend to Allocate – Temporarily allocate the requested resources and update Available,
Allocation, and Need.
3. Safety Check – Use the safety algorithm to see if the system is still in a safe state. - If yes →
Grant the request. - If no → Rollback (do not grant the request).
■ Example
System with 3 types of resources (A, B, C) and 5 processes (P0–P4). Available = [3, 3, 2]
Process Max (A,B,C) Allocation (A,B,C) Need (A,B,C)
P0 7, 5, 3 0, 1, 0 7, 4, 3
P1 3, 2, 2 2, 0, 0 1, 2, 2
P2 9, 0, 2 3, 0, 2 6, 0, 0
P3 2, 2, 2 2, 1, 1 0, 1, 1
P4 4, 3, 3 0, 0, 2 4, 3, 1
■ Safety Check
1. Start with Available = [3, 3, 2] 2. P1’s need [1,2,2] ≤ [3,3,2] → P1 finishes, releases [2,0,0],
Available = [5,3,2]. 3. P3’s need [0,1,1] ≤ [5,3,2] → P3 finishes, Available = [7,4,3]. 4. P4’s need
[4,3,1] ≤ [7,4,3] → P4 finishes, Available = [7,4,5]. 5. P0’s need [7,4,3] ≤ [7,4,5] → P0 finishes,
Available = [7,5,5]. 6. P2’s need [6,0,0] ≤ [7,5,5] → P2 finishes.
■ All processes can finish. ■ Safe sequence = P1 → P3 → P4 → P0 → P2. ■ System is in a safe
state.
■ Summary
- Banker’s Algorithm avoids deadlock by carefully checking before allocating resources. - Key
definitions: Available, Max, Allocation, and Need. - It ensures that the system always stays in a safe
state. - If a resource request makes the system unsafe, it is delayed until it is safe.