Banker's Algorithm in Operating System: Examples & Implementation

Overview

The Banker's Algorithm is a fundamental deadlock avoidance algorithm used in operating systems for safe resource allocation to processes. The algorithm determines whether resources can be safely granted to a process without causing the system to enter a deadlock state. This comprehensive guide explores the algorithm's working principles, implementation, characteristics, and practical examples.

Publication Date: 22 August 2025
Reading Time: 5 minutes
Source: NxtWave

Table of Contents

What is Banker's Algorithm in Operating Systems?

The Banker's Algorithm is a resource allocation and deadlock prevention algorithm useful when allocating resources to several processes in an operating system while keeping the system safe. The algorithm tracks three critical aspects:

  1. The maximum resources a process may utilize
  2. The current resources allocated for the processes
  3. How many resources are available

When a process requests resource allocation, the algorithm checks the system to ensure granting the request will not lead the system into a deadlock state by verifying if it can eventually attain a safe state where all processes finish. If the system cannot reach a safe state, the request is declined, thus avoiding potential deadlock.

Why is it Called the "Banker's Algorithm"?

The Banker's Algorithm derives its name from the analogy of a bank managing resources, particularly loans. This model considers a bank with numerous customers who deposit specific amounts. Loans are given out by deducting the loan amounts from the total deposits. However, to prevent an unstable financial situation from developing, the banker must be ready to meet all deposit demands from all depositors, even when they all request their money simultaneously. Otherwise, a deadlock would arise, and neither the bank nor the customers could move further.

Similarly, the Banker's Algorithm checks whether allocating resources to a process is safe in the operating system. The system tests whether it can still fulfill all process requirements without causing a deadlock. Resource allocation must be both efficient and secure.

Characteristics of Banker's Algorithm

The Banker's Algorithm exhibits four key characteristics:

1. Waiting Time

A requesting process must wait for requested resources when they cannot be safely allocated immediately.

2. Resource Allocation Efficiency

The algorithm attempts to ensure resources are allocated in a way that avoids deadlock while maintaining system efficiency.

3. Regression of Resource Use

The algorithm assures quick release for reusing resources when processes are done, allowing resources to be returned to the available pool.

4. Dynamic Usage

The Banker's Algorithm is dynamic in handling requests for allocation and releases, adapting to changing system states in real-time.

How the Banker's Algorithm Works

The Banker's Algorithm can be understood through a practical analogy: Consider a library with a limited collection of books where each student requires a certain number of books. The lending of books each student is requesting is checked to ensure that it does not cause the system to become an unsafe state.

The algorithm performs the following verification:

  1. Checks if granting the request maintains system safety
  2. Confirms the ability of all students to finish their jobs without entering into a deadlock
  3. When the check is true, books are given to students
  4. Once students are finished, they return the books
  5. This continues until all students finish their assignments

Like how bank or classroom resources are efficiently managed, the Banker's Algorithm ensures that resource allocation will not lead to an unsafe state from where deadlock arises.

Requirements of Banker's Algorithm

Implementing the Banker's Algorithm requires three fundamental components:

Initial Allocation

The number of resources initially allocated to each process. It carries out a record of the number of each resource instance currently assigned to each process.

Maximum Need

The maximum number of resources that each process may require during its execution. It may be used to predict future resource requests for each process.

Available

The total number of instances for every resource type in the system that were unallocated before allocation.

The Banker's Algorithm can allocate resources safely without entering a deadlock state using these three components.

Data Structures Used in Banker's Algorithm

Implementing the Banker's Algorithm in an operating system adopts several key data structures for administrating and tracking resources and processes:

Available Vector

A vector of length "m" that tracks the number of available instances for each resource type in the system.

Format: If Available[j] = k, there are k instances of resource type Rj available.

Max Matrix

A matrix of size [n × m] expressing the maximum number of resources each process can demand.

Format: If Max[i][j] = k, process Pi can request up to k instances of resource Rj.

Allocation Matrix

A matrix of size [n × m] representing the number of resources allocated to each process.

Format: If Allocation[i][j] = k, process Pi has been allocated k of resource Rj.

Need Matrix

A matrix of size [n × m] that provides the number of resources still needed for each process.

Format: If Need[i][j] = k, process Pi still needs k more instances of resource Rj to complete.

Calculation: Need = Maximum Demand - Allocation

Finish Vector

A vector of length n, where each entry is a Boolean value (TRUE or FALSE).

Purpose: Shows if the process has finished executing and released all its resources. If Finish[i] = TRUE, process Pi is said to have completed execution with the resources freed.

Key Concepts in Banker's Algorithm

Safe State

A system is said to be in a secure state when there is a sequence of processes such that by each process obtaining required resources, finishing, and then releasing resources that might be needed by other methods, such other processes have a possibility of finishing. In a safe state, no deadlock can occur in the system.

Unsafe State

It is called unsafe when resources may still be allocated to processes, allowing them to proceed but with no guarantees that all will be completed without causing deadlocks. In this case, a deadlock occurs, and the process may not finish.

Banker's Algorithm Process

The Banker's Algorithm process involves five key steps:

Step 1: Mark Active Processes

Mark active processes as running or blocked.

Active := Running ∪ Blocked
for k = 1 to r:
    New_request[k] := Requested_resources[requesting_process, k]

Step 2: Change Resource Allocation State

Change the resource allocation state to include the most recent request.

Simulated_allocation := Allocated_resources
for k = 1 to r:
    // Compute projected allocation state
    Simulated_allocation[requesting_process, k] := Simulated_allocation[requesting_process, k] + New_request[k]

Step 3: Verify Resource Allocation

Verify if such resource allocation exceeds the available resources; if not, it proceeds.

Feasible := true
for k = 1 to r:
    // Check if projected allocation state is feasible
    if Total_resources[k] < Simulated_total_alloc[k]:
        Feasible := false

Step 4: Remove Finished Processes

The algorithm removes active processes that can finish upon accessing the available resources.

if Feasible = true:
    while set Active contains a process P1 such that:
        for all k, Total_resources[k] - Simulated_total_alloc[k] >= Max_need[P1, k] - Simulated_allocation[P1, k]:
            Delete P1 from Active
        for k = 1 to r:
            Simulated_total_alloc[k] := Simulated_total_alloc[k] - Simulated_allocation[P1, k]

Step 5: Grant Request and Update Resources

If all processes can be finished, the request will be granted, and resources will be re-updated accordingly.

if set Active is empty:
    for k = 1 to r:
        Requested_resources[requesting_process, k] := 0
    for k = 1 to r:
        Allocated_resources[requesting_process, k] := Allocated_resources[requesting_process, k] + New_request[k]
    Total_alloc[k] := Total_alloc[k] + New_request[k]

Types of Banker's Algorithms

There are two types of banker's algorithms:

Safety Algorithm

The Safety Algorithm determines whether the system is in a safe state.

Step 1: Initialize Vectors

The algorithm initializes vectors Work and Finish.

Initialize: Work = Available
Finish[i] = false; for i = 1, 2, 3, 4....n

Step 2: Search for Available Processes

It searches for processes not finished yet for available resources.

Find an i such that both:
    2.1 Finish[i] = false
    2.2 Needi <= Work
    if no such i exists, go to step (4)

Step 3: Update Resources

The processes are finished after available resources are updated.

Work = Work + Allocation[i]
Finish[i] = true
Go to step (2)

Step 4: Verify Safe State

It ensures a safe state if all processes are successfully finished.

If Finish[i] = true for all i, then the system is in a safe state

Resource-Request Algorithm

The Resource-Request Algorithm handles individual resource requests from processes.

Step 1: Validate Request

This requires a check to see if the request for resources by a process is valid.

If Requesti <= Needi, go to step (2);

Step 2: Check Maximum Claim

The process request for resources does not exceed the maximum claim.

If Requesti <= Available, go to step (3);

Step 3: Allocate or Wait

If requested resources are unavailable in the system, the process waits until they are released.

Available = Available - Requesti
Allocationi = Allocationi + Requesti
Needi = Needi - Requesti

Example of Banker's Algorithm in OS

This section demonstrates a complete example of the Banker's Algorithm with solution.

Initial System State

We have the following tables for processes, allocations, maximum needs, and available resources:

Process Allocation (A, B, C) Max (A, B, C) Available (A, B, C)
P0 (1, 2, 5) (4, 3, 2) (3, 2, 1)
P1 (2, 1, 2) (4, 3, 2)
P2 (0, 1, 9) (6, 2, 3)
P3 (2, 0, 8) (6, 4, 4)
P4 (1, 1, 2) (2, 2, 5)

Step 1: Calculate the Need Matrix

The Need matrix is calculated using the formula:

Need[i] = Max[i] - Allocation[i]

Calculation for each process:

Process Need (A, B, C)
P0 (4-1, 3-2, 2-5) = (3, 1, -3)
P1 (4-2, 3-1, 2-2) = (2, 2, 0)
P2 (6-0, 2-1, 3-9) = (6, 1, -6)
P3 (6-2, 4-0, 4-8) = (4, 4, -4)
P4 (2-1, 2-1, 5-2) = (1, 1, 3)

Step 2: Determine Safe State and Safe Sequence

To determine if the system is in a safe state, we follow the Safety Algorithm.

Initialize Work and Finish

Find Processes That Can Finish

Process P0: Need = (3, 1, -3), but this contains a negative value, which is invalid. Therefore, P0 cannot finish.

Process P1: Need = (2, 2, 0) and Work = (3, 2, 1). Since Need ≤ Work, P1 can finish.

Process P2: Need = (6, 1, -6), but this contains a negative value, so P2 cannot finish.

Process P3: Need = (4, 4, -4), which also contains a negative value, so P3 cannot finish.

Process P4: Need = (1, 1, 3) and Work = (5, 3, 3). Since Need ≤ Work, P4 can finish.

Repeat the Process

Now, we have the updated Work = (6, 4, 5). We check again for other processes:

Safe Sequence Result

Since P1 and P4 can finish, we have a safe sequence: <P1, P4>

Implementation of Banker's Algorithm

This section provides a complete C++ implementation of the Banker's Algorithm.

C++ Code Implementation

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n = 5, m = 3;
    vector<vector<int>> Max = {{6, 5, 3}, {4, 2, 1}, {5, 1, 2}, {1, 0, 2}, {5, 2, 3}};
    vector<vector<int>> Alloc = {{1, 0, 2}, {3, 1, 0}, {0, 1, 1}, {1, 0, 1}, {1, 0, 2}};
    vector<int> Total = {7, 5, 7}; // Total available resources
    vector<int> Avail(m); // Available resources after allocation

    for (int j = 0; j < m; j++) {
        int s = 0;
        for (int i = 0; i < n; i++) {
            s += Alloc[i][j];
        }
        Avail[j] = Total[j] - s;
    }

    vector<int> Exec(n, 0);
    vector<vector<int>> Need(n, vector<int>(m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            Need[i][j] = Max[i][j] - Alloc[i][j];
        }
    }

    int k, c = 0;
    vector<int> seq;
    while (c < n) {
        for (int i = 0; i < n; i++) {
            if (Exec[i] == 0) {
                int mark = 0;
                for (int j = 0; j < m; j++) {
                    if (Need[i][j] > Avail[j]) {
                        mark = 1;
                        break;
                    }
                }
                if (mark == 0) {
                    seq.push_back(i + 1);
                    for (k = 0; k < m; k++) {
                        Avail[k] += Alloc[i][k];
                    }
                    Exec[i] = 1;
                }
            }
        }
        c++;
    }

    cout << "The safe sequence of execution is ";
    for (int i = 0; i < n; i++) {
        if (i == n - 1)
            cout << " P" << seq[i] << "\n";
        else
            cout << " P" << seq[i] << " ->";
    }
}

Code Explanation

The above C++ code implements the Banker's Algorithm to determine a safe sequence of process execution. It computes the Need, Available, and Allocation matrices and then iterates through processes to check whether their resource needs can be satisfied. If so, that process runs, and resources are updated. Finally, the safe sequence is printed.

Program Output

The safe sequence of execution is P1 -> P3 -> P4 -> P2 -> P5

Example of an Unsafe State in Banker's Algorithm

This section demonstrates a scenario where the system enters an unsafe state.

System Configuration

Consider a system with 3 processes (P1, P2, P3) with 6 instances of Resources. The state of the system is defined by the following conditions:

Step 1: Available Resources

1 instance of resource available.

Step 2: Current Allocation

Step 3: Maximum Demand

Maximum resources each process may request:

Step 4: Calculate Need for Each Process

Need = Maximum Demand - Allocation

Need Matrix

Process Maximum Demand Allocation Need
P1 4 2 2
P2 5 3 2
P3 3 1 2

Analysis of Unsafe State

In this example:

The system has only one instance of the resource, and each process needs two more resources in order to complete. Even in the presence of resources, there is no guarantee that all processes will reach termination without a deadlock.

Advantages of Banker's Algorithm

The Banker's Algorithm offers several key advantages:

1. Deadlock Detection and Prevention

The algorithm detects a deadlock and prevents it as it allocates resources safely.

2. Safe Resource Allocation

The algorithm caters to process requests by checking safe or unsafe allocation of resources.

3. Advance Resource Declaration

One advantage of this algorithm is that it allows processes to declare their maximum resource requirements in advance.

4. Safety Verification

The safety algorithm ensures that resources are allocated to avoid leaving the system unsafe.

Disadvantages of Banker's Algorithm

The Banker's Algorithm has several limitations:

1. Fixed Maximum Requirements

The processes must declare the maximum resource requirements before and cannot be changed during execution.

2. Advance Declaration Requirement

All processes must declare their maximum requirements before execution begins.

3. Fixed Number of Processes

It starts with a fixed number of processes; at no time during execution can another process be entered.

4. Time Constraints

The algorithm has a certain finite period within which resource allocation must be done; if unsuccessful, there is a maximum time limit (e.g., one year).

Conclusion

The Banker's Algorithm is a simple but competent way of resource allocation and deadlock avoidance in an operating system. The Banker's Algorithm will always keep a system in a safe state. It has some overheads, and there are restrictions on its use. It suits those systems in which resource requirements are known, and deadlock avoidance is essential. Other methods may prove more effective for real-time systems with highly variable resource requirements.

Frequently Asked Questions

What is the Banker's Algorithm? What is an example of it?

The Banker's Algorithm is an example of a deadlock avoidance algorithm. It ensures that resources are allotted so the system can safely avoid entering a deadlock state by checking for potential safe sequences in resource allocation.

What is the Banker's Algorithm in OS?

The Banker's Algorithm manages resource allocation and avoids deadlock. It determines whether a requested resource can be safely allocated to a process without leaving the system unsafe so that all processes can finish in time.

What is the Deadlock Algorithm in OS?

The deadlocks in an operating system are the set of strategies to detect, avoid, and even resolve the deadlocks. The standard algorithms for deadlock avoidance include the Banker's algorithm, while the resource allocation graph and wait-for graphs are used for deadlock detection.


About NxtWave

NxtWave is a technology education platform offering comprehensive courses in software development, data analysis, and emerging technologies. For more information, visit ccbp.in.

Contact Information: