# Deadlock Prevention using Banker's Algorithm in C

The banker's algorithm is a resource allocation and deadlock avoidance algorithm that simulates resource allocation for predetermined maximum possible amounts of all resources before performing an "s-state" check to look for potential activities and determining whether allocation should be permitted to continue.

## Why the Banker's Algorithm is called that?

The reason the banker's algorithm is so titled is because it is employed in the banking sector to determine whether or not to authorize a loan to an individual. Assume a bank has n account holders, each of whom has an individual balance of S. When someone requests for a loan, the bank first deducts the loan amount from the total amount of money it possesses, and only approves the loan if the balance is more than S. It is done because the bank can simply do it if every account holder shows up to get their money. In other words, the bank would never arrange its funds in a way that would prevent it from meeting the demands of all of its clients. The bank would strive to maintain safety at all times.

The Banker's Algorithm is implemented using the following data structures:

Let n be the number of processes in the system and m be the number of resource kinds.

Available:

• This 1-d array of size 'm' lists the number of resources of each category that are currently available.
• There are 'k' instances of the resource type if Available[j] = Rj

Max:

• The maximum demand of each process in a system is specified by a 2-d array of size 'n*m'.
• Process Pi may request a maximum of 'k' instances of resource type Rj if Max[i, j] = k.

Allocation:

• The quantity of resources of each kind currently assigned to each process is specified by a 2-d array of size 'n*m'.
• Process is shown by Allocation[i, j] = k. Pi has been given 'k' instances of the resource type at this time. Rj

Need:

• The remaining resource needs of each process are shown in a 2-d array of size 'n*m'.
• Process is shown by Need[i, j] = k. Right now, Pi requires "k" instances of the resource type. Rj
• Allocation[i, j] - Maximum[i, j] = Need[i, j]

The resources that are now allotted to process Pi are identified by Allocationi, while the extra resources that process Pi could yet need in order to do its work are identified by Needi.

The safety algorithm and the resource request algorithm make up the banker's algorithm.

### Algorithm for Safety

The following is a description of the method for determining if a system is in a safe state:

### Algorithm for Resource Requests

Let Requesti represent the process Pi request array. Process Pi requests k instances of resource type Rj, which is indicated by Requesti [j] = k. The following things happen when process Pi makes a request for resources:

## Below is the implementation of Banker's Algorithm

Output

```Following is the SAFE Sequence
P1 -> P3 -> P4 -> P0 -> P2
........................................................
Process execute din 1.33 seconds
Press any key to continue.
```

Explanation:

• The processes must anticipate the maximum number of resources required as they enter the system, which may be done in a reasonable amount of time.
• In contrast to interactive systems, this method maintains a fixed number of processes.
• A specific number of resources must be available to distribute in order for this technique to work. The algorithm would not function if a gadget broke and went unexpectedly offline.
• The method will incur overhead costs since it must be invoked for each process when there are several processes and resources.