Bankers Algorithm Program in C

Banker's Algorithm in OS (with C Program)
Imagine an operating system that hands out memory, printers or database locks the way an over-eager bank hands out loans: first come, first served, no questions asked. Sooner or later two processes each hold half of what the other needs, and neither can move. That standoff is a deadlock, and banker's algorithm in OS is the classic technique for making sure the system never lends itself into that corner in the first place.
This page covers the algorithm itself with a fully worked numerical example, the safety and resource-request logic behind it, and a complete C program that implements it, output included, so you can check your own trace against a real run rather than trusting a diagram alone.:::
What Is Banker's Algorithm in OS?
Banker's algorithm is a deadlock-avoidance algorithm that grants a resource request only if, after simulating the allocation, the system remains in a safe state, meaning at least one order exists in which every process can still get everything it needs and finish.
Four pieces of bookkeeping make this possible, and every table on this page is built from them:
• Available — how many instances of each resource type are currently free.
• Max — the maximum number of instances of each resource type a process may ever claim, declared up front.
• Allocation — how many instances of each resource type a process currently holds.
• Need — Max minus Allocation, what a process still might ask for before it's done.
Why Is It Called the Banker's Algorithm?
Because it behaves like a cautious banker: never commit so much cash that you couldn't cover every customer's full credit line if they all asked for it at once. Each process declares a maximum claim up front, like a credit limit, and the OS only approves a request if it can still guarantee every process a path to finishing, even in the worst case. It's credited to Edsger W. Dijkstra, developed during the design of the THE operating system.
Worth the honest caveat up front: this only works because every process must declare its maximum resource claim in advance, and hold that promise for its entire lifetime. Real general-purpose operating systems rarely know that number ahead of time, which is exactly why banker's algorithm shows up constantly in OS courses and rarely in the OS running on your laptop.
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreSafe State vs Unsafe State vs Deadlock
| State | Definition | Safe sequence exists? | Deadlock guaranteed? | System action |
|---|---|---|---|---|
| Safe | At least one order lets every process finish | Yes | No | Request can be granted |
| Unsafe | No such order currently exists | No | Not guaranteed, but now possible | Request is denied, rolled back |
| Deadlock | A circular wait has actually occurred | No | Yes, already happened | Recovery, not avoidance, is needed |
An unsafe state is not a deadlock. It's a state from which deadlock becomes possible, which is precisely the gap banker's algorithm exists to catch before it turns into the real thing.
Safety Algorithm and Resource-Request Algorithm
The Safety Algorithm
Starting from a copy of Available (called Work) and every process marked unfinished, the safety algorithm repeatedly looks for any unfinished process whose Need is fully covered by Work. When it finds one, that process is assumed to run to completion and release everything it holds back into Work, then gets marked finished. If a full pass finds no process it can satisfy, the state is unsafe. If every process eventually gets marked finished, the order they finished in is a safe sequence.
The Resource-Request Algorithm
When a process asks for more resources, three checks run in order. First, does the request exceed what the process declared it might still need? Second, are the resources actually available right now? Third, and this is the check that does the real work, pretend to grant the request, then re-run the safety algorithm on that hypothetical state. If it's still safe, the allocation is confirmed. If not, everything is rolled back and the process waits.
Working on Bankers' algorithm
Let us assume that there are 5 processes, P1, P2, P3, P4, and P5 have resources A, B, and C allocated to each process. Their maximum need is provided and we need to use Banker's algorithm to find the remaining need and get a sequence of processes that is in a safe state. Total resources available are A=10, B=5 and C=7.
Let's see how the algorithm works by finding the available resources and filling in the table below.
Here, Remaining need = (Max need- Allocation)
| Processes | Allocation | Max Need | Available | Remaining need |
|---|---|---|---|---|
| A B C | A B C | A B C | A B C | |
| P1 | 0 1 0 | 7 5 3 | 7 4 3 | |
| P2 | 2 0 0 | 3 2 2 | 1 2 2 | |
| P3 | 3 0 2 | 9 0 2 | 6 0 0 | |
| P4 | 2 1 1 | 4 2 2 | 2 1 1 | |
| P5 | 0 0 2 | 5 3 3 | 5 3 1 |
We have to find the Available resources, Available resources = (Total - Allocation)
For A, Total= [0+2+3+2+0]=7 Available= [10-7]=3 For B, Total= [1+0+0+1+0]=2 Available= [5-2]=3 For C, Total= [0+0+2+1+2]=5 Available= [7-5]=2
For 1st iteration,
| Processes | Allocation | Max Need | Available | Remaining need |
|---|---|---|---|---|
| A B C | A B C | A B C | A B C | |
| P1 | 0 1 0 | 7 5 3 | 3 3 2 | 7 4 3 |
| P2 | 2 0 0 | 3 2 2 | 1 2 2 | |
| P3 | 3 0 2 | 9 0 2 | 6 0 0 | |
| P4 | 2 1 1 | 4 2 2 | 2 1 1 | |
| P5 | 0 0 2 | 5 3 3 | 5 3 1 |
Now we have to check which processes can utilize the available resources. Compare the Remaining needs of each process with the available resources.
P1 needs 7, 4, and 3 but available are 3, 3, 2. It can't use the resources. P2 needs 1, 2, and 2, and available are 3, 3, and 2. It can be used. So add P2 as the first process in the sequence < P2 >
and find newly available from P2 and perform the above steps again. new available= allocation of P2+ available (2+3),(0+3),(0+2)=[ 5, 3, 2 ]
2nd iteration
| Processes | Allocation | Max Need | Available | Remaining need |
|---|---|---|---|---|
| A B C | A B C | A B C | A B C | |
| P1 | 0 1 0 | 7 5 3 | 3 2 2 | 7 4 3 |
| P2 | 2 0 0 | 3 2 2 | 5 3 2 | 1 2 2 |
| P3 | 3 0 2 | 9 0 2 | 6 0 0 | |
| P4 | 2 1 1 | 4 2 2 | 2 1 1 | |
| P5 | 0 0 2 | 5 3 3 | 5 3 1 |
P3 needs 6, 0, 0 but available is 5, 3, 2. So move to P4 P4 needs 2, 1, 1, and available are 5, 3, 2. P4 can use the resources. Add P4 to the sequence < P2, P4 >
Now available using P4= (5+2),(3+1),(2+1)=7, 4, 3
3rd iteration
| Processes | Allocation | Max Need | Available | Remaining need |
|---|---|---|---|---|
| A B C | A B C | A B C | A B C | |
| P1 | 0 1 0 | 7 5 3 | 3 2 2 | 7 4 3 |
| P2 | 2 0 0 | 3 2 2 | 5 3 2 | 1 2 2 |
| P3 | 3 0 2 | 9 0 2 | 7 4 3 | 6 0 0 |
| P4 | 2 1 1 | 4 2 2 | 2 1 1 | |
| P5 | 0 0 2 | 5 3 3 | 5 3 1 |
P5 needs 5, 3, 1, and available are 7,4,3 so P5 can utilize the resources.
Add P5 to the sequence < P2, P4, P5 >
Now available using P5=(0+7),(0+4),(2+3)=(7,4,5)
4th iteration
| Processes | Allocation | Max Need | Available | Remaining need |
|---|---|---|---|---|
| A B C | A B C | A B C | A B C | |
| P1 | 0 1 0 | 7 5 3 | 3 2 2 | 7 4 3 |
| P2 | 2 0 0 | 3 2 2 | 5 3 2 | 1 2 2 |
| P3 | 3 0 2 | 9 0 2 | 7 4 3 | 6 0 0 |
| P4 | 2 1 1 | 4 2 2 | 7 4 5 | 2 1 1 |
| P5 | 0 0 2 | 5 3 3 | 5 3 1 |
For P1, available in 7,4,5, and required is 7, 4, 3. Hence P1 can utilize the resources.
Add P1 to the sequence. < P2, P4, P5, P1 >
Now available using P1=(0+7),(1+4),(0+5)=(7,5, 5)
5th iteration
| Processes | Allocation | Max Need | Available | Remaining need |
|---|---|---|---|---|
| A B C | A B C | A B C | A B C | |
| P1 | 0 1 0 | 7 5 3 | 3 2 2 | 7 4 3 |
| P2 | 2 0 0 | 3 2 2 | 5 3 2 | 1 2 2 |
| P3 | 3 0 2 | 9 0 2 | 7 4 3 | 6 0 0 |
| P4 | 2 1 1 | 4 2 2 | 7 4 5 | 2 1 1 |
| P5 | 0 0 2 | 5 3 3 | 7 5 5 | 5 3 1 |
For P3 available is 7, 5, 5, and required is 6, 0, 0. Hence, it can use the resources. Add P3 to the sequence. < P2, P4, P5, P1, P3 >
Hence, the order of processes to occur to avoid deadlock is < P2, P4, P5, P1, P3 >.
Note: We have to always check current availability >= Remaining need. If the condition is false, there is a deadlock, and is in the unsafe state. If the condition is true, resources can be utilized and iterated to the next process.
Algorithm
We consider n= number of processes and m=number of resource types.
Allocation:
- Specifies the number of resources currently allocated to a process.
- 2-D array of size n*m.
- Allocation[i,j]=k, ie, 'k' instances of a resource Rj is allocated to process Pi.
Max need:
- Specifies the number of resources needed by a process.
- 2-D array of size n*m.
- Maxneed[i,j]=Max[i,j]-Allocation[i,j].
Available:
- Specifies the number of available resources for a process.
- 1-D array of size m.
- Available[j]=k, ie, resource type Rj has k instances.
Remaining need(Max):
- Specifies the maximum demand of each process in a system.
- 2-D array of size n*m.
- Max[i,j]=k, Resource type Rj requests at most k instances of process Pi.
Banker's Algorithm follows two algorithms.
- Safety algorithm - This algorithm checks if a system is in a safe state.
- Resource-Request Algorithm - This algorithm performs actions based on requests.
Safety Algorithm
Resource-Request Algorithm
Bankers Algorithm Program in C
Output:
Time Complexity
The safety algorithm scans up to n processes on every pass, needs up to n passes in the worst case, and does m work per comparison, giving O(n² × m), where n is the number of processes and m is the number of resource types. The resource-request algorithm calls the safety algorithm once, so it shares the same bound.
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Space Complexity
O(n × m), for the Allocation, Max and Need matrices, each sized n × m, plus O(n + m) for the Work and Finish arrays used during the safety check, which doesn't change the dominant term.
Deadlock Avoidance vs Prevention vs Detection
| Approach | When it acts | What it requires | Resource utilisation | Overhead | Used in practice? |
|---|---|---|---|---|---|
| Prevention | Before the fact, structurally | Breaking one of the four Coffman conditions | Often low, resources sit reserved | Low, runtime, high design cost | Rarely, in general-purpose OSes |
| Avoidance | At runtime, per request | Max claims known in advance | Better than prevention | Per-request safety check | Mostly academic; some embedded/real-time systems |
| Detection & recovery | After a deadlock has occurred | Periodic graph checks | Best, nothing held back | Recovery cost when it fires | Databases, some distributed systems |
| Ostrich algorithm | Never, ignores the problem | Nothing | Best, no overhead at all | None, until it happens | Most general-purpose OSes, in practice |
Advantages of Banker's Algorithm
• Avoids deadlock outright, a request is only granted if the resulting state is still provably safe.
• Guarantees every process a path to completion, since a safe sequence proves at least one valid order exists.
• Uses resources more efficiently than blanket prevention, which often reserves resources it never actually uses.
• Tests “what if” before committing, the pretend-allocate step catches a bad request before any real resource changes hands.
• Extends cleanly to multiple resource types with multiple instances each, which is exactly the case the running example above covers.
Real-World Applications and Limitations
A few settings where declaring a maximum claim in advance is genuinely realistic: bank credit-line management (the algorithm's own namesake), cloud resource-quota managers allocating a known ceiling per tenant, database transaction managers where locks are declared ahead of time, and embedded or real-time systems with a fixed, known set of resources.
The limitations are the same fact stated four ways. Every process must know and declare its maximum resource claim before it starts, which most everyday applications simply can't promise. The number of processes has to stay fixed while the algorithm runs, ruling out systems where processes come and go freely. The total resource pool has to stay fixed too, no resource can quietly go down for maintenance. And every safety check costs O(n² × m), which adds up if requests are frequent and n or m is large. This is exactly why the technique shows up constantly in OS coursework and rarely in the kernel running on your laptop, general-purpose systems can't make the up-front promises it needs.
These limitations sit alongside the broader menu of classic process synchronization problems a course covers, banker's algorithm solves one specific corner of a much larger space.
FAQs
Turn Learning into Career Growth
What do you mean by banker's algorithm?
A deadlock-avoidance algorithm that grants a resource request only if, after simulating the allocation, the system remains in a safe state, meaning at least one sequence exists in which every process can acquire its maximum needs and complete.
Why is it called the banker's algorithm?
Because it behaves like a banker who never commits so much cash that they could not honour every customer's full credit line. Each process declares a maximum claim, like a credit limit, and the OS only lends what it can always cover.
What is a safe sequence?
An ordering of processes in which each one can obtain its remaining Need from the currently available resources, finish, and release everything it holds. In the worked example above, <P1, P3, P4, P2, P0> is a safe sequence. More than one may exist.
What is the difference between deadlock avoidance and deadlock prevention?
Prevention structurally breaks one of the four Coffman conditions, often wasting resources. Avoidance allows all four conditions but examines each request at runtime and denies any that would lead to an unsafe state. Banker's algorithm is avoidance.
What are the 4 types of deadlock in OS?
The question usually refers to the four necessary conditions for deadlock: mutual exclusion, hold and wait, no preemption, and circular wait. All four must hold simultaneously for deadlock to occur.
What are real-world examples of banker's algorithm?
Bank credit-line management, cloud resource-quota managers, database transaction managers where locks are declared in advance, and embedded or real-time systems with fixed, known resource sets. General-purpose OSes rarely use it because maximum claims are unknown.
What problem is solved by Dijkstra's banker's algorithm?
It solves deadlock avoidance: deciding safely, at runtime, whether granting a resource request could leave the system unable to let every process finish.
Conclusion
Banker's algorithm earns its place in every OS course not because real kernels run it, most don't, but because it's the cleanest illustration of what deadlock avoidance actually requires: knowing enough about future demand to test a decision before committing to it. The safety check, Need compared against Work, one process at a time, releasing what it holds, is the entire idea. Everything else, the Available derivation, the two request walkthroughs, the C program, is that one check applied consistently.
The honest limitation, needing every process's maximum claim declared in advance, is also the reason it stays mostly theoretical outside banking, cloud quotas and a handful of fixed-resource systems. Understanding where it applies is as useful as understanding how it runs.
Working through a full deadlock-avoidance trace by hand, matrices, safety checks, granted and denied requests, is exactly the kind of structured problem-solving CS fundamentals are built on. Scaler Academy pairs that kind of foundational rigor with mentor-led practice for anyone looking to go deeper than a single worked example.