Learn what is Bankers Algorithm in Operating System, its working, steps, example, and advantages in an easy and beginner-friendly way.
Introduction to Bankers Algorithm
In an Operating System (OS), managing resources like memory, CPU, and I/O devices efficiently is crucial. When multiple processes compete for resources, the system may face a deadlock — a state where processes wait endlessly for each other’s resources.
To prevent such deadlocks, operating systems use the Bankers Algorithm, a deadlock avoidance algorithm.
The name comes from a banking system analogy, where a banker ensures that all loans (resources) can be safely granted without running out of money (system resources).
Definition of Bankers Algorithm
Banker’s Algorithm is a deadlock avoidance algorithm used in operating systems to decide whether to grant a resource request immediately or make the process wait until it’s safe to do so. Just like the Round Robin Scheduling Algorithm, it plays a crucial role in efficient CPU and resource management.
It checks if the system will remain in a safe state after allocating resources.
If yes → resources are allocated.
If not → the process must wait.
Why is it Called the Bankers Algorithm?
The algorithm was designed by Edsger Dijkstra, inspired by how a banker lends money.
A banker never lends all his money to customers. He ensures that even after lending, there are enough funds left so that every customer can withdraw their maximum demand safely.
Similarly, the operating system ensures that resources can be allocated safely without causing a deadlock.
Key Concepts in Bankers Algorithm
Before understanding how it works, let’s go through a few important terms:
| Term | Description |
|---|---|
| Available | Number of available resources in the system. |
| Max | Maximum demand of each process. |
| Allocation | Number of resources currently allocated to each process. |
| Need | Remaining resource need for each process. (Need = Max – Allocation) |
| Safe State | The system is in a safe state if there exists at least one sequence of processes that can finish without leading to a deadlock. |
Working of Banker’s Algorithm
The Banker’s Algorithm works in two main parts:
- Safety Algorithm – Checks whether the system is in a safe state.
- Resource Request Algorithm – Checks if a process’s request can be safely granted.
Step-by-Step Explanation
Step 1: Calculate the Need Matrix
For each process,
Need[i][j] = Max[i][j] - Allocation[i][j]
Step 2: Check Request
When a process requests resources (Request[i]), the system checks:
- Request[i] ≤ Need[i]
- Request[i] ≤ Available
If both are true, proceed; otherwise, the process waits.
Step 3: Pretend to Allocate
Temporarily allocate the requested resources:
Available = Available - Request[i]
Allocation[i] = Allocation[i] + Request[i]
Need[i] = Need[i] - Request[i]
Step 4: Check Safety State
Run the Safety Algorithm to verify whether the system remains in a safe state after the allocation.
Step 5: Decision
- If the system is safe, the request is granted.
- If the system becomes unsafe, the request is denied, and the process must wait.
Example of Bankers Algorithm
Let’s take a simple example:
| Process | Max | Allocation | Available |
|---|---|---|---|
| P1 | 7 | 5 | 3 |
| P2 | 3 | 2 | 2 |
| P3 | 9 | 0 | 2 |
Now,
Need = Max - Allocation
The algorithm will check for each process if it can complete with the available resources.
If a safe sequence exists, such as P2 → P1 → P3, the system is in a safe state.
If no such sequence exists, it’s an unsafe state, meaning potential deadlock risk.
Bankers Algorithm in C
Here’s a simple C program to demonstrate Banker’s Algorithm logic:
#include
int main() {
int n, m;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter number of resources: ");
scanf("%d", &m);
int alloc[n][m], max[n][m], avail[m];
printf("Enter Allocation Matrix:\n");
for(int i=0;i avail[j])
break;
if(j == m) {
for(int y=0;y Advantages of Bankers Algorithm
- Prevents deadlocks effectively.
- Ensures the system always stays in a safe state.
- Provides predictable and controlled resource allocation.
Limitations of Bankers Algorithm
- Complex for large systems with many processes.
- Requires prior knowledge of maximum resource needs.
- May delay processes unnecessarily to maintain safety.
- Not suitable for real-time systems.
Key Takeaways
- Banker’s Algorithm is a deadlock avoidance algorithm.
- It ensures safe resource allocation by checking system safety.
- Works using Available, Max, Allocation, and Need matrices.
- Guarantees the system remains deadlock-free when correctly applied.
Frequently Asked Questions (FAQ) : Bankers Algorithm
1. What is the main purpose of Banker’s Algorithm?
To prevent deadlock by ensuring the system remains in a safe state.
2. Who invented Banker’s Algorithm?
It was introduced by Edsger Dijkstra.
3. What is a safe state?
A state in which the system can allocate resources to every process in some order and still avoid deadlock.
4. Is Banker’s Algorithm used in real systems?
Rarely, because it requires knowing the maximum demand in advance.
5. What is the time complexity of Banker’s Algorithm?
The time complexity is O(n²) where n is the number of processes.
Conclusion
The Banker’s Algorithm plays a vital role in understanding deadlock avoidance in operating systems. It provides a safe way to allocate resources and ensures that the system always operates in a stable and secure state.
While it’s mostly used for academic and conceptual understanding, mastering it helps you build a strong foundation in OS resource management.
Mr. Raj Kumar is a highly experienced Technical Content Engineer with 7 years of dedicated expertise in the intricate field of embedded systems. At Embedded Prep, Raj is at the forefront of creating and curating high-quality technical content designed to educate and empower aspiring and seasoned professionals in the embedded domain.
Throughout his career, Raj has honed a unique skill set that bridges the gap between deep technical understanding and effective communication. His work encompasses a wide range of educational materials, including in-depth tutorials, practical guides, course modules, and insightful articles focused on embedded hardware and software solutions. He possesses a strong grasp of embedded architectures, microcontrollers, real-time operating systems (RTOS), firmware development, and various communication protocols relevant to the embedded industry.
Raj is adept at collaborating closely with subject matter experts, engineers, and instructional designers to ensure the accuracy, completeness, and pedagogical effectiveness of the content. His meticulous attention to detail and commitment to clarity are instrumental in transforming complex embedded concepts into easily digestible and engaging learning experiences. At Embedded Prep, he plays a crucial role in building a robust knowledge base that helps learners master the complexities of embedded technologies.












