Javatpoint Logo
Javatpoint Logo

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.

Utilizing the following data structures, the Banker's Algorithm is implemented:

Assume that there are n processes in the system and m different resource kinds.

Available:

  • It is a 1-d array of size "m" that lists the total amount of resources of each type that are accessible.
  • There are 'k' instances of the resource type if Available[j] = k. 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 number of resources of each type currently allocated to each process is specified by a 2-d array of size 'n*m'.
  • Process is indicated by Allocation[i, j] = k. Pi has been given 'k' instances of the resource type at this time. Rj

Need:

  • It is a 2-d array of size 'n*m' that shows how much more each process needs in terms of resources.
  • Need I j] = k denotes that process Pi need 'k' instances of the resource type at this time. Rj
  • Allocation [i, j] - Maximum [i, j] = Need [i, j]

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

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 Request represent the process Pi request array. Process Pi requests k instances of resource type Rj, which is indicated by Request [j] = k. The following things happen when process Pi makes a request for resources:

Banker's algorithm source code

Output:

Following is the SAFE Sequence
 P1 -> P3 -> P4 -> P0 -> P2
.......................................................
Process executed in 2.32 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.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA