The Banker’s algorithm, also known as the Banker’s Safety Algorithm, is a resource allocation and deadlock algorithm. It was developed by Edsger Dijkstra. In this article, we will explore this algorithm in depth in C language.
What is Banker’s Algorithm?
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm. It is used in operating systems to manage the allocation of resources to multiple designs to ensure that a system can avoid entering a deadlocked state. This algorithm was developed by Edsger Dijkstra.

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
Working of Banker’s algorithm
Let’s understand how a banker’s algorithm works. Suppose there are five processes, P0, P1, P2, P3, and P4, with resources A, B, and C allocated to each process. We have their maximum need, and we need to use the Banker’s algorithm to find the remaining need and get a sequence of processes that is in a safe state.
The following table describes the Banker’s Algorithm:
Maximum:
| Process | A | B | C |
| P0 | 7 | 5 | 3 |
| P1 | 3 | 2 | 2 |
| P2 | 9 | 0 | 2 |
| P3 | 2 | 2 | 2 |
| P4 | 4 | 3 | 3 |
Allocation
| Process | A | B | C |
| P0 | 0 | 1 | 0 |
| P1 | 2 | 0 | 0 |
| P2 | 3 | 0 | 2 |
| P3 | 2 | 1 | 1 |
| P4 | 0 | 0 | 2 |
Need( calculated as Maximum – Allocation)
| Process | A | B | C |
| P0 | 7 | 4 | 3 |
| P1 | 1 | 2 | 2 |
| P2 | 6 | 0 | 0 |
| P3 | 0 | 1 | 1 |
| P4 | 4 | 3 | 1 |
Need (calculated Maximum -Allocation)
| Process | A | B | C |
| P0 | 7 | 4 | 3 |
| P1 | 1 | 2 | 2 |
| P2 | 6 | 0 | 0 |
| P3 | 0 | 1 | 1 |
| P4 | 4 | 3 | 1 |
Banker’s Algorithm Program in C
Program
#include <stdio.h>
#include <stdbool.h>
#define N 5 // Number of processes
#define M 3 // Number of resources
void calculateNeed(int need[N][M], int max[N][M], int alloc[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];
}
}
}
bool isSafe(int processes[], int avail[], int max[][M], int alloc[][M]) {
int need[N][M];
calculateNeed(need, max, alloc);
bool finish[N] = {false};
int safeSeq[N];
int work[M];
for (int i = 0; i < M; i++)
work[i] = avail[i];
int count = 0;
while (count < N) {
bool found = false;
for (int p = 0; p < N; p++) {
if (finish[p] == false) {
int j;
for (j = 0; j < M; j++)
if (need[p][j] > work[j])
break;
if (j == M) {
for (int k = 0; k < M; k++)
work[k] += alloc[p][k];
safeSeq[count++] = p;
finish[p] = true;
found = true;
}
}
}
if (found == false) {
printf("The system is not in a safe state\n");
return false;
}
}
printf("The system is in a safe state.\nSafe sequence is: ");
for (int i = 0; i < N; i++)
printf("%d ", safeSeq[i]);
printf("\n");
return true;
}
int main() {
int processes[] = {0, 1, 2, 3, 4};
int avail[] = {3, 3, 2};
int max[N][M] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}
};
int alloc[N][M] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}
};
isSafe(processes, avail, max, alloc);
return 0;
}Advantages of Banker’s Algorithm
In this section, we will see the advantages of the banker’s algorithm.
| Advantages | Description |
| Deadlock Avoidance | This algorithm helps in avoiding deadlocks by ensuring that resource allocation always leaves the system in a safe state. |
| Optimal Resource Utilisation | It maximises resource utilisation by allocating resources in a way that guarantees system safety |
| Fairness | This ensures all processes get their required resources eventually, provided the system is in a safe state. |
| Suitable for Systems with Fixed Resources | This is ideal for a system where the resources and processes are fixed and known in advance |

82.9%
of professionals don't believe their degree can help them get ahead at work.
Disadvantages of Banker’s Algorithm
Let’s see the disadvantages of a banker’s algorithm.
| Disadvantages | Description |
| High Complexity | Banker’s algorithm has high time complexity O(N2* M). This makes it less efficient for large systems with many processes and resource type |
| Static Allocation | It assumes a static number of processes and resources, which may not be suitable for dynamic environments where processes and resource demands change frequently |
| Resource Overhead | Maintaining and updating several data structures, such as allocation, maximum, and need matrices, adds overhead in terms of memory and processing. |
| Scalability Issues | Efficiency decreases as the number of process and resource types increases, making it less suitable for large-scale systems. |
Conclusion
In this article, we learned about the Banker’s Algorithm in C language. It is essential to understand advanced concepts in operating system design. This algorithm is particularly useful in managing resource allocation and preventing deadlock scenarios. It stands out for its ability to ensure system safety by carefully managing resource requests based on current allocation, maximum demands, and available resources. The Banker’s Algorithm remains foundational for developers and system architects, offering invaluable insights into optimising resource utilisation and maintaining system stability in operating systems.
Why is the Banker’s Algorithm important?
What are the advantages of using the Banker’s Algorithm?
What is the real-life application of the Bankers algorithm?
Is the Banker’s Algorithm used in modern operating systems?
Updated on September 26, 2024
