Banker's Algorithm in Operating System


In computer systems, the Banker’s algorithm is utilized to avoid deadlock so that resources can be safely allocated to each process. This algorithm is named so because it can be used in a banking system to ensure that the bank never allocates its available cash such that it can no longer satisfy the requirements of all of its customers.

In this article, we will discuss the banker’s algorithm in detail. But before that let us understand a real-world situation analogous to it.

Banker's Algorithm: How Does It Work?

Let us consider a bank has "n" number of accounts and "M" is the total cash in the bank. If a customer has applied for a loan, then the bank first subtracts the loan amount from the total cash available and then verifies that the cash difference must be greater than the total cash "M" to approve the loan. This process helps the bank to manage and operate all the banking functions without any restriction.

In the same way, the banker’s algorithm works in a computer system’s operating system. In a computer system, when a new process enters the system. Then, the process must declare the maximum number of instances of each resource type that it may require to execute. This number must not be more than the total number of resources in the system. Now, when a new process requests resources, the system must calculate whether the allocation of the requested resources will leave the computer system in a safe state. If so then the process will get allocated the requested resources, otherwise it must wait until some other process releases the requested resources.

By following this practice, the banker’s algorithm avoids deadlock and allocate resources safely. For this reason, it is also termed as deadlock detection algorithm or deadlock avoidance algorithm in the operating system.

Data Structures Used in Banker’s Algorithm

In a computer system, if the number of processes is equal to "n" and the number of resource types is "m". The following four data structures are required to implement the banker’s algorithm −

  • Available − It is a vector of length "m" that indicates the number of available resources of each type in the system. If Available [j] = k, then "k" be the instances of resource type Rj available in the system.

  • Max − It is a [n × m] matrix that defines the maximum resource demand of each process. When Max [I, j] = k, then a process Pi may request maximum k instances of resource type, Rj.

  • Allocation − – It is also a [n × m] matrix that defines the number of resources of each type which are currently allocated to each process. Therefore, if Allocation [i, j] = k, then a process Pi is currently allocated "k" instances of resource type, Rj.

  • Need − It is an [n × m] matrix that represents the remaining resource need of each process. When Need [i, j] = k, then a process Pi may need "k" more instances of resource type Rj to accomplish the assigned task.

  • Finish − It is a matrix of length "m". It includes a Boolean value, i.e. TRUE or FALSE to indicate whether a process has been allocated to the needed resources, or all resources have been released after finishing the assigned work.

Also, it is to be noted that,

Need [i, j] = Max [i, j] – Allocation [i, j]

The baker’s algorithm is a combination of two algorithms namely, Safety Algorithm and Resource Request Algorithm. These two algorithms together control the processes and avoid dead lock in a system.

Safety Algorithm

The safety algorithm is one that determines whether or not the system is in a safe state. It follows the following safe sequence in the banker’s algorithm −

Step 1

  • Consider two vectors named Work and Finish of lengths "m" and "n" respectively.

  • Initialize: Work = Available

  • Finish [i] = false; for i = 0, 1, 2, 3, 4, 5 … n.

Step 2

  • Find "i" such that both Finish [i] = false and Needi ≤ Work

  • If such "i" does not exist, then go to the step 4.

Step 3

  • Work = Work + Allocation

  • Finish [i] = true

  • Go to step 2.

Step 4

  • If Finish [i] = true for all "i", it means that the system is in a safe state otherwise it is in unsafe state.

  • This algorithm takes (m × n2) operations to decide whether the state is safe or not.

Now, let us discuss the Resource Request Algorithm.

Resource Request Algorithm

This algorithm determines how a system behaves when a process request for each type of resource in the system.

Let us consider a request vector Requesti for a process Pi. When Requesti [j] = k, then the process Pi requires k instances of resource type Rj.

When a process Pi requests for resources, the following sequence is followed −

Step 1

If Requesti ≤ Needi, then go to the step 2. Otherwise give an error as the process has exceeded its maximum claim for the resource.

Step 2

If Requesti ≤ Available, then go to the step 3. Otherwise, the process Pi must wait until the resource is available.

Step 3

If the requested resource is allocated to the process, Pi by modifying the state as follows −

Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;

When the state of the resulting resource allocation is safe, then the requested resources are allocated to the process, Pi. If the new state is unsafe, then the process Pi must wait for Requesti and the old resource allocation state is restored.

Updated on: 21-Apr-2023

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements