Bankers Algorithm Program in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The Banker's Algorithm, a key deadlock avoidance and detection tool, strategically allocates resources by simulating their distribution to predetermined maximums. This process includes an 's-state' safety check to decide if resource allocation can proceed without leading to a deadlock condition.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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)

ProcessesAllocationMax NeedAvailableRemaining need
A B CA B CA B CA B C
P10 1 07 5 37 4 3
P22 0 03 2 21 2 2
P33 0 29 0 26 0 0
P42 1 14 2 22 1 1
P50 0 25 3 35 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,

ProcessesAllocationMax NeedAvailableRemaining need
A B CA B CA B CA B C
P10 1 07 5 33 3 27 4 3
P22 0 03 2 21 2 2
P33 0 29 0 26 0 0
P42 1 14 2 22 1 1
P50 0 25 3 35 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

ProcessesAllocationMax NeedAvailableRemaining need
A B CA B CA B CA B C
P10 1 07 5 33 2 27 4 3
P22 0 03 2 25 3 21 2 2
P33 0 29 0 26 0 0
P42 1 14 2 22 1 1
P50 0 25 3 35 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

ProcessesAllocationMax NeedAvailableRemaining need
A B CA B CA B CA B C
P10 1 07 5 33 2 27 4 3
P22 0 03 2 25 3 21 2 2
P33 0 29 0 27 4 36 0 0
P42 1 14 2 22 1 1
P50 0 25 3 35 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

ProcessesAllocationMax NeedAvailableRemaining need
A B CA B CA B CA B C
P10 1 07 5 33 2 27 4 3
P22 0 03 2 25 3 21 2 2
P33 0 29 0 27 4 36 0 0
P42 1 14 2 27 4 52 1 1
P50 0 25 3 35 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

ProcessesAllocationMax NeedAvailableRemaining need
A B CA B CA B CA B C
P10 1 07 5 33 2 27 4 3
P22 0 03 2 25 3 21 2 2
P33 0 29 0 27 4 36 0 0
P42 1 14 2 27 4 52 1 1
P50 0 25 3 37 5 55 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

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Bankers Algorithm Program in C

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Time Complexity

Time complexity of Banker's Algorithm is O(n3a)O(n^3a) where, n=number of processes a=number of resources

Space Complexity

The space Complexity of thBanker's Algorithm is O(n)O(n).

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Conclusion

  • Banker's Algorithm is known as the Deadlock Avoidance Algorithm.
  • It is also used as a Deadlock Detection Algorithm.
  • It tests for the safe state by calculating the allocation of the maximum possible resources.
  • Since, the number of processes remains fixed, it is not possible for interactive systems. Hence, the requirement of a dynamic system is needed.