Banker's Algorithm

Banker's Algorithm
Definition

The Banker's Algorithm is a resource allocation and deadlock avoidance algorithm used in operating systems. It works by simulating the allocation of requested resources to determine if a system can remain in a safe state to avoid deadlocks. It is named after a banking system, where the bank ensures that it never allocates cash in such a way that it can no longer satisfy the needs of its customers.

Why use Banker's Algorithm?

Operating systems must manage resources efficiently and prevent deadlocks. A deadlock occurs when two or more processes are indefinitely waiting for each other to release a resource. The Banker's Algorithm ensures that the system remains in a safe state, meaning there is an order in which all processes can complete their execution without causing a deadlock.

Key Concepts
  1. Safe State: A state is considered safe if the system can allocate resources to each process (up to its maximum) in some order and still avoid a deadlock. This means there exists a safe sequence of processes.

  2. Unsafe State: If no such safe sequence exists, the system is in an unsafe state. An unsafe state does not necessarily mean a deadlock will occur, but it indicates a possibility of a deadlock.

  3. Safe Sequence: A sequence of processes {1, P2, …, Pn>} is a safe sequence if, for each P</em>i{P</em>i}, the resources that P<em>i{P<em>i} still needs can be satisfied by the currently available resources plus the resources held by all P</em>j{P</em>j} where {j < i}. If P<em>i{P<em>i}'s needs are satisfied, it can run to completion, release its resources, and then P</em>i+1{P</em>{i+1}} can use those resources, and so on.

Data Structures Needed

To implement the Banker's Algorithm, the following data structures are required:

  • Available: A vector of length m (number of resource types) indicating the number of available resources of each type. If Available[j]=k{Available[j] = k}, then k instances of resource type j are available.

  • Max: An n×mn \times m matrix defining the maximum demand of each process. If Max[i][j]=k{Max[i][j] = k}, then process Pi{P_i} may request at most k instances of resource type *j}.

  • Allocation: An n×mn \times m matrix defining the number of resources of each type currently allocated to each process. If Allocation[i][j]=k{Allocation[i][j] = k}, then process Pi{P_i} is currently allocated k instances of resource type *j$.

  • Need: An n×mn \times m matrix indicating the remaining resource need of each process. If Need[i][j]=k{Need[i][j] = k}, then process Pi{P_i} still needs k instances of resource type *j$. This can be calculated as Need[i][j]=Max[i][j]Allocation[i][j]{Need[i][j] = Max[i][j] - Allocation[i][j]}.

Where:

  • nn = number of processes

  • mm = number of resource types

Algorithm Steps (Safety Algorithm)

This algorithm checks if the current state is safe (i.e., if a safe sequence exists).

  1. Initialization:

    • Let Work be a vector of length m, initialized to Available. (Work tracks available resources during simulation).

    • Let Finish be a boolean vector of length n, initialized to false for all processes. (Finish[i] = true if Pi{P_i} can complete).

  2. Find a Process:

    • Find an index i{i} such that Finish[i] == false and Need<em>iWork{Need<em>i \le Work} (where Need</em>i{Need</em>i} is the vector for process Pi{P_i}'s needs).

    • If no such i{i} exists, go to Step 4.

  3. Allocate and Release (Simulate):

    • If such an i{i} is found:

      • Work = Work + Allocation_i (resources Pi{P_i} held are now 'available').

      • Finish[i] = true (process Pi{P_i} can complete).

      • Go back to Step 2.

  4. Check for Safety:

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

    • Otherwise, the system is in an unsafe state.