# Banker's Algorithm Java

In Java, Banker's algorithm is a deadlock avoidance and resource allocation algorithm. This algorithm tests for security by simulating allocation for a predetermined maximum possible amount of all resources. After that, before deciding whether the allocation should be allowed to continue or not, it creates an "s-state" check for testing it to all possible activities. In order to implement the Banker's algorithm in Java, we use the following data structures:

Suppose we have N processes and M resource types in the system.

• Available Data structure

The first data structure which we use is the Available Data Structure. This data structure constructs using a one-dimensional array of size M(Number of available resources). If there are K instances of resource type RJ, it will be denoted as:

Available[ J ] = K

• Max Data structure

Another data structure used in Banker's algorithm is Max Data Structure. This data structure constructs using a two-dimensional array of size M*N. In the system, it defines the maximum demand of each process.

If a process PI request for K instances of resource type RJ, it will be denoted as:

Max[ I, J ] = K

• Allocation Data structure

Just like the Available and Max data structure, Allocation Data Structure is a two-dimensional array having a size M*N. It defines the total number of resources allocated to a process.

If a process PI is currently allocated K instances of resource type RJ, it will be demoted as:

Allocation[ I, J ] = K

• Need Data structure

The Need Data Structure is also a two-dimensional array having a size M*N. It defines the total number of remaining resource which a process need.

If a process PI is currently allocated K instances of resource type RJ, it will be defined as:

Need [ I, J ] = Max[ I, J ] - Allocation [ I, J ]

In simple words, (Allocation)I define the total number of resources that are currently allocated to a process PI, and (Need)I define the total number of remaining resources for which a process PI is requested for completing its task.

We know that the Banker's algorithm is a combination of the Resource Request Algorithm and Safety Algorithm.

Let's take an overview of both the algorithms, and then we implement both of them to construct Banker's algorithm program in Java.

### Resource Request Algorithm

The Resource request algorithm plays an important role in the allocation of resources to a process. This algorithm checks how a system will behave when a process makes each type of resource request in a system as a request matrix.

The request resource algorithm is given below:

1. If RequestI <= NeedI

Else

Print an error message for exceeding its maximum claim.

2. If RequestI <= Available

Else

Print an error message "resources are not available".

3. Allocate the resources to the process and remove the quantity of the resources allocated to a process from the Available and Need like as:

Available = Available - RequestI

AllocationI = AllocationI + RequestI

NeedI = NeedI - RequestI

### Safety Algorithm

The safety algorithm is used to determine whether our system is in a safe state or not. If the system follows the safe sequence in Banker's algorithm, our system is in a safe state. The safety algorithm is given below:

1. Let the Work and the Finish are the two vectors of length M and N.

Initialize : Work = Available

for 1 to N

Finish [ i ] = false;

2. Find i such that both

1. Finish [ i ] = false
2. Needi <= work

if no such i exists, go to step (4)

3. Work = Work + Allocationi

Finish[ i ] = true

Go to step(2)

4. If Finish[ i ] = true for all i,

The system is in a safe state.

To get more knowledge about Banker's algorithm, resource request algorithm, and safety algorithm, go through the following link: https://www.javatpoint.com/bankers-algorithm-in-operating-system

Let's implement the code of the Banker's algorithm using the above-discussed resource request and safety algorithm in Java.

BankersAlgoExample.java

Output    ### Feedback   