The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue.

Following **Data structures** are used to implement the Banker’s Algorithm:

Let **‘n’ **be the number of processes in the system and **‘m’ **be the number of resources types.

**Available : **

- It is a 1-d array of size
**‘m’**indicating the number of available resources of each type. - Available[ j ] = k means there are
**‘k’**instances of resource type**R**_{j}

**Max :**

- It is a 2-d array of size ‘
**n*m’**that defines the maximum demand of each process in a system. - Max[ i, j ] = k means process
**P**may request at most_{i}**‘k’**instances of resource type**R**_{j.}

**Allocation :**

- It is a 2-d array of size
**‘n*m’**that defines the number of resources of each type currently allocated to each process. - Allocation[ i, j ] = k means process
**P**is currently allocated_{i}**‘k’**instances of resource type**R**_{j}

**Need :**

- It is a 2-d array of size
**‘n*m’**that indicates the remaining resource need of each process. - Need [ i, j ] = k means process
**P**currently allocated_{i}**‘k’**instances of resource type**R**_{j} - Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]

Allocation_{i} specifies the resources currently allocated to process P_{i} and Need_{i} specifies the additional resources that process P_{i} may still request to complete its task.

Banker’s algorithm consist of Safety algorithm and Resource request algorithm

**Safety Algorithm**

The algorithm for finding out whether or not a system is in a safe state can be described as follows:

1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.

Initialize: Work = Available

Finish[i] = false; for i=1, 2, 3, 4….n2) Find an i such that both

a) Finish[i] = false

b) Need_{i}<= Work if no such i exists goto step (4)3) Work = Work + Allocation

Finish[i] = true

goto step (2)4) if finish [i] = true for all i

then the system is in a safe state

**Resource-Request Algorithm**

Let Request_{i} be the request array for process P_{i}. Request_{i }[j] = k means process P_{i} wants k instances of resource type R_{j}. When a request for resources is made by process P_{i}, the following actions are taken:

1) If Request

_{i}<= Need_{i}

Goto step (2) ; otherwise, raise an error condition, since the process has exceeded its maximum claim.2) If Request

_{i}<= Available Goto step (3); otherwise, P_{i}must wait, since the resources are not available.3) Have the system pretend to have allocated the requested resources to process Pi by modifying the state as

follows:

Available = Available – Requesti

Allocation_{i}= Allocation_{i}+ Request_{i}

Need_{i}= Need_{i}– Request_{i}

**Example:**

**Considering a system with five processes P _{0} through P_{4} and three resources types A, B, C. Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time t_{0} following snapshot of the system has been taken:**

**Question1. What will be the content of the Need matrix?**

Need [i, j] = Max [i, j] – Allocation [i, j]

So, the content of Need Matrix is:

**Question2. Is the system in safe state? If Yes, then what is the safe sequence?**

Applying the Safety algorithm on the given system,

**Question3. What will happen if process P _{1 }requests one additional instance of resource type A and two instances of resource type C?**

We must determine whether this new system state is safe. To do so, we again execute Safety algorithm on the above data structures.

Hence the new system state is safe, so we can immediately grant the request for process ** P _{1 .}**

**GATE question:**

**http://quiz.geeksforgeeks.org/gate-gate-cs-2014-set-1-question-41/**

**Reference:**

Operating System Concepts 8th Edition by Abraham Silberschatz, Peter B. Galvin, Greg Gagne

This article has been contributed by Vikash Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above