Banker’s Algorithm in Operating System [Example]

What is Banker’s Algorithm?

Banker’s Algorithm is used majorly in the banking system to avoid deadlock. It helps you to identify whether a loan will be given or not.

This algorithm is used to test for safely simulating the allocation for determining the maximum amount available for all resources. It also checks for all the possible activities before determining whether allocation should be continued or not.

For example, there are X number of account holders of a specific bank, and the total amount of money of their accounts is G.

When the bank processes a car loan, the software system subtracts the amount of loan granted for purchasing a car from the total money ( G+ Fixed deposit + Monthly Income Scheme + Gold, etc.) that the bank has.

It also checks that the difference is more than or not G. It only processes the car loan when the bank has sufficient money even if all account holders withdraw the money G simultaneously.

Banker’s Algorithm Notations

Here is an important notation used in Banker’s algorithm:

  • X: Indicates the total number of processes of the system.
  • Y: Indicates the total number of resources present in the system.

Available

[I: Y] indicate which resource is available.

Max

[l:X,l: Y]: Expression of the maximum number of resources of type j or process i

Allocation

[l:X,l:Y]. Indicate where process you have received a resource of type j

Need

Express how many more resources can be allocated in the future

Example of Banker’s algorithm

Assume that we have the following resources:

  • 5 Pen drives
  • 2 Printers
  • 4 Scanners
  • 3 Hard disks

Here, we have created a vector representing total resources: Available = (5, 2, 4, 3).

Assume there are four processes. The available resources are already allocated as per the matrix table below.

Process Name Pen Drives Printer Scanner Hard disk
P 2 0 1 1
Q 0 1 0 0
R 1 0 1 1
S 1 1 0 1
Total 4 2 2 3

Here, the allocated resources is the total of these columns:

Allocated = (4, 2, 2, 3).

We also create a Matrix to display the number of each resource required for all the processes. This matrix is called Need=(3,0,2,2)

Process Name Pen Drives Printer Scanner Hard disk
P 1 1 0 0
Q 0 1 1 2
R 2 1 0 0
S 0 0 1 0

The available vector will be :

Available=Available- Allocated

= (5, 2, 4, 3) -(4, 2, 2, 3)

=(1, 0, 2, 0)

Resource Request Algorithm

Resource request algorithm enables you to represent the system behavior when a specific process makes a resource request.

Let understand this by the following steps:

Step 1) When a total requested instance of all resources is lesser than the process, move to step 2.

Step 2) When a requested instance of each and every resource type is lesser compared to the available resources of each type, it will be processed to the next step. Otherwise, the process requires to wait because of the unavailability of sufficient resources.

Step 3) Resource is allocated as shown in the below given Pseudocode.

Available = Available – Request (y)
Allocation(x) = Allocation(x) + Request(x)
Need(x) = Need(x) - Request(x)

This final step is performed because the system needs to assume that resources have been allocated. So that there should be less resources available after allocation.

Characteristics of Banker’s Algorithm

Here are important characteristics of banker’s algorithm:

  • Keep many resources that satisfy the requirement of at least one client
  • Whenever a process gets all its resources, it needs to return them in a restricted period.
  • When a process requests a resource, it needs to wait
  • The system has a limited number of resources
  • Advance feature for max resource allocation

Disadvantage of Banker’s algorithm

Here, are cons/drawbacks of using banker’s algorithm

  • Does not allow the process to change its Maximum need while processing
  • It allows all requests to be granted in restricted time, but one year is a fixed period for that.
  • All processes must know and state their maximum resource needs in advance.

Summary

  • Banker’s algorithm is used majorly in the banking system to avoid deadlock. It helps you to identify whether a loan will be given or not.
  • Notations used in banker’s algorithms are 1) Available 2) Max 3) Allocation 4) Need
  • Resource request algorithm enables you to represent the system behavior when a specific process makes a resource request.
  • Banker’s algorithm keeps many resources that satisfy the requirement of at least one client
  • The biggest drawback of banker’s algorithm this that it does not allow the process to change its Maximum need while processing.