Bankers Algorithm Explained: 5 Powerful Steps for Deadlock Avoidance [Complete Beginner’s Guide]

On: October 15, 2025
Bankers Algorithm Explained

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:

TermDescription
AvailableNumber of available resources in the system.
MaxMaximum demand of each process.
AllocationNumber of resources currently allocated to each process.
NeedRemaining resource need for each process. (Need = Max – Allocation)
Safe StateThe 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:

  1. Safety Algorithm – Checks whether the system is in a safe state.
  2. 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:

ProcessMaxAllocationAvailable
P1753
P2322
P3902

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 <stdio.h>

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<n;i++)
        for(int j=0;j<m;j++)
            scanf("%d", &alloc[i][j]);

    printf("Enter Max Matrix:\n");
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            scanf("%d", &max[i][j]);

    printf("Enter Available Resources:\n");
    for(int i=0;i<m;i++)
        scanf("%d", &avail[i]);

    int need[n][m];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            need[i][j] = max[i][j] - alloc[i][j];

    int finish[n], safeSeq[n], count = 0;
    for(int i=0;i<n;i++) finish[i] = 0;

    int k=0;
    while(count < n) {
        int found = 0;
        for(int i=0;i<n;i++) {
            if(finish[i] == 0) {
                int j;
                for(j=0;j<m;j++)
                    if(need[i][j] > avail[j])
                        break;

                if(j == m) {
                    for(int y=0;y<m;y++)
                        avail[y] += alloc[i][y];
                    safeSeq[k++] = i;
                    finish[i] = 1;
                    found = 1;
                    count++;
                }
            }
        }
        if(found == 0) {
            printf("System is in an unsafe state!\n");
            return 0;
        }
    }

    printf("System is in a safe state.\nSafe sequence: ");
    for(int i=0;i<n;i++)
        printf("P%d ", safeSeq[i]);
    printf("\n");
    return 0;
}

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.

Leave a Comment

Exit mobile version