C Program To Implement Banker’s Algorithm
Learn How To Write A C Program To Implement Banker’s Algorithm for avoiding Deadlocks in Operating Systems. Banker’s Algorithm is also popularly known as Deadlock Avoidance Algorithm.
What is Banker’s Algorithm?
The Banker’s Algorithm was designed and developed by a Dutch Computer Scientist, Edsger Djikstra. The Banker’s Algorithm is a Resource Allocation and a Deadlock Avoidance Algorithm.
This algorithm takes analogy of an actual bank where clients request to withdraw cash. The Banking Authorities have some data according to which the cash is lent to the client. The Banker cannot give more cash than the client’s request and the total cash available in the bank.

The Banker’s Algorithm is divided into Two parts:
1. Safety Test Algorithm: This algorithm checks the current state of the system to maintain its Safe State.
2. Resource Request Handling Algorithm: This algorithm verifies if the requested resources, after their allocation to the processes affects the Safe State of the System. If it does, then the request of the process for the resource is denied, thereby maintaining the Safe State.
A State is considered to be Safe if it is possible for all the Processes to Complete its Execution without causing any Deadlocks. An Unsafe State is the one in which the Processes cannot complete its execution.
C Program For Banker’s Algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include <stdio.h> int main() { int count = 0, m, n, process, temp, resource; int allocation_table[5] = {0, 0, 0, 0, 0}; int available[5], current[5][5], maximum_claim[5][5]; int maximum_resources[5], running[5], safe_state = 0; printf("\nEnter The Total Number Of Processes:\t"); scanf("%d", &process); for(m = 0; m < process; m++) { running[m] = 1; count++; } printf("\nEnter The Total Number Of Resources To Allocate:\t"); scanf("%d", &resource); printf("\nEnter The Claim Vector:\t"); for(m = 0; m < resource; m++) { scanf("%d", &maximum_resources[m]); } printf("\nEnter Allocated Resource Table:\n"); for(m = 0; m < process; m++) { for(n = 0; n < resource; n++) { scanf("%d", ¤t[m][n]); } } printf("\nEnter The Maximum Claim Table:\n"); for(m = 0; m < process; m++) { for(n = 0; n < resource; n++) { scanf("%d", &maximum_claim[m][n]); } } printf("\nThe Claim Vector \n"); for(m = 0; m < resource; m++) { printf("\t%d ", maximum_resources[m]); } printf("\n The Allocated Resource Table\n"); for(m = 0; m < process; m++) { for(n = 0; n < resource; n++) { printf("\t%d", current[m][n]); } printf("\n"); } printf("\nThe Maximum Claim Table \n"); for(m = 0; m < process; m++) { for(n = 0; n < resource; n++) { printf("\t%d", maximum_claim[m][n]); } printf("\n"); } for(m = 0; m < process; m++) { for(n = 0; n < resource; n++) { allocation_table[n] = allocation_table[n] + current[m][n]; } } printf("\nAllocated Resources \n"); for(m = 0; m < resource; m++) { printf("\t%d", allocation_table[m]); } for(m = 0; m < resource; m++) { available[m] = maximum_resources[m] - allocation_table[m]; } printf("\nAvailable Resources:"); for(m = 0; m < resource; m++) { printf("\t%d", available[m]); } printf("\n"); while(count != 0) { safe_state = 0; for(m = 0; m < process; m++) { if(running[m]) { temp = 1; for(n = 0; n < resource; n++) { if(maximum_claim[m][n] - current[m][n] > available[n]) { temp = 0; break; } } if(temp) { printf("\nProcess %d Is In Execution \n", m + 1); running[m] = 0; count--; safe_state = 1; for(n = 0; n < resource; n++) { available[n] = available[n] + current[m][n]; } break; } } } if(!safe_state) { printf("\nThe Processes Are In An Unsafe State \n"); break; } else { printf("\nThe Process Is In A Safe State \n"); printf("\nAvailable Vector\n"); for(m = 0; m < resource; m++) { printf("\t%d", available[m]); } printf("\n"); } } return 0; } |
Output

If you have any compilation errors or doubts in this C Program To Implement Banker’s Algorithm for Deadlock Avoidance, let us know about it in the comment section below.
Thanks. Finally this Bankers Algorithm CProgram works. Is the Banker Algorithm used in real time in Banking Systems? Or is there any new algorithm that has replaced Banker’s Algorithm?
Perfect code for Banker’s Algorithm. You explanation and formatting is way too good. Thanks a lot for wproviding us these program codes.
You’re welcome Vivek! 🙂
Best Explanation to Banker’s Algorithms. Great!
Really good explanation to Banker’s Problem! Thanks!
Thanks for this Deadlock Avoidance Program in C Language. This is really good.
The Banker’s Algorithm in Operating System or OS is just too good. The safe state code is finally working. Perfect code! 🙂
It is important to check if the wait for graph contains a cycle or not. If it does not contain any cycle, then there is no deadlock possibility.
I think the Bankers algorithm and Dijkstra’s algorithm has some similarities since its is developed on the basis of Dijkstras algorithm.
The Banker’s Algorithms offers the following conditions:
1. No Preemption
2. Hold and Wait
3. Mutual Exclusion
However, it prevents the processes from going into the circular wait condition.
Whenever the system receives a request for granting resources, it executes the Banker’s algorithm amd displays whether it is safe to grant the request or not.
Easy Deadlock avoidance algorithm. Thanks. and amazing website for C programming tutorials. Loved it.
The Banker algorithm for deadlock avoidance in C programming is I feel very old technique. There are many newer and better algorithms for deadlock avoidance techniques.
Thanks for the output of Banker Algorithm. This code works better.
I wonder if the Banker’s Algorithm is implemented in the banks at the time when Rs. 500 and Rs. 1000 notes are demonitized.
THANK YOU it is very usefull