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.

In this operating system tutorial, you will learn:

Banker's Algorithm Notations

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

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:

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:

Disadvantage of Banker's algorithm

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

Summary:

 

YOU MIGHT LIKE: