Bankers Algorithm in C

Hey. Today in this post we will be dealing with Bankers Algorithm. Bankers algorithm is used in Operating systems to prevent deadlocks and it is a resource allocation algorithm. First of all let me explain what is a Bankers algorithm.

Bankers Algorithm

Consider there are 3 resources A, B and C. There are 6, 6, 6 instances of the resources available respectively. Consider 3 processes P0, P1 and P2. Consider the following matrix which shows the instances of the resources that are allocated to P0, P1 and P2.

And consider the following instances of A, B and C are still required by the processes to complete it's execution.

First let us consider P0. 3, 1, 1 instances of A, B and C are required for its completion. There are 6, 6, 6 instances available. So we can afford the required instances. After completion of the process P0 the available instances of A, B, C would become 7, 7, 7. Then the process P1 would be executed as 3, 4, 4 instances are available. The available resources would become 8, 8, 8 after the execution of P1. P2 requires 2, 2, 2 instances which is available and so it would complete the execution. Such a situation in which where all the processes complete their execution is called a safe state.

Consider a case in which the allocation of resources for a process is not possible, in such a case we will look for some other sequence for execution of the processes. If the allocation of resources is not possible in whichever sequence we execute the process then the system is said to be in an unsafe state.

The following is a recursive algorithm to find out if the system is in safe state or not and the sequence in which the programs would execute for a safe state condition.

In the following algorithm I have considered 4 resource A, B, C, D and 4 processes P0, P1, P2, P3.



Feel free to post your queries below :) 


Popular posts from this blog

Finding the Number of Trailing Zero's in Factorial of a Number

Finding the sum of N Biggest elements in Binary Search Tree

Finding a Single Number amongst Repeated Numbers [Repeated Twice]