Banker's Algorithm in Operating System (OS)

There is an algorithm called Banker's Algorithm used in removing deadlocks while dealing with the safe allocation of resources to processes in a computer system. It gives all the resources to each process and avoids resource allocation conflicts.

Overview of Banker's Algorithm

  • The 'S-State' checks all the possible tests or activities before determining whether the resource should be allocated to any process or not.
  • It enables the operating system to share resources among all the processes without creating deadlock or any critical situation.
  • The algorithm is so named based on banking operations because it mimics the process through which a bank determines whether or not it can safely make loan approvals.

Real Life Example

Imagine having a bank with T amount of money and n account holders. At some point, whenever one of the account holders requests a loan:

  • The bank withdraws the requested amount of cash from the total amount available for any further withdrawals.
  • The bank checks if the cash that is available for withdrawal will be enough to cater to all future requests/withdraws.
  • If there is enough money available (that is to say, the available cash is greater than T), he lends the loan.
  • This ensures that the bank will not suffer operational problems when it receives subsequent applications.

Banker's Algorithm in Operating Systems

Likewise, in an operating system:

  • When a new process is created, it needs to provide all the vital information, such as which processes are scheduled to run shortly, resource requests, and potential delays.
  • This knowledge helps the OS decide which sequence of process executions needs to proceed to avoid any deadlock.
  • Since the order of executions that should occur in order to prevent deadlocks is defined, the Banker's Algorithm is usually considered a deadlock avoidance or a deadlock detection algorithm in OS

Advantages

Following are the essential characteristics of the Banker's algorithm:

  1. It contains various resources that meet the requirements of each process.
  2. Each process should provide information to the operating system for upcoming resource requests, the number of resources, and how long the resources will be held.
  3. It helps the operating system manage and control process requests for each type of resource in the computer system.
  4. The algorithm has a Max resource attribute that represents indicates each process can hold the maximum number of resources in a system.
  5. It means that in the Banker's Algorithm, the resources are granted only if there is no possibility of a deadlock when those resources are to be assigned. Thus, it ensures that the system runs at optimal performance.
  6. The algorithm allows one to avoid the pointless holding of resources by any process since the algorithm actually checks whether the granting of resources is feasible or not.
  7. Since the Banker's Algorithm analyzes all potential problems before assigning the resources, its implementation renders the OS more stable and reliable.

Disadvantages

  1. It requires a fixed number of processes, and no additional processes can be started in the system while executing the process.
  2. The algorithm does no longer allows the processes to exchange its maximum needs while processing its tasks.
  3. Each process has to know and state their maximum resource requirement in advance for the system.
  4. The number of resource requests can be granted in a finite time, but the time limit for allocating the resources is one year.
  5. It could be pretty intricate to manage the algorithm, which is especially known in the case of systems with a vast quantity of processes and resources. This, consequently, translates to increased overhead.
  6. In dynamic environments, often with frequently changing processes and resource needs, the Banker's Algorithm proves to be too inflexibly rigid to handle dynamic resource allocation well.
  7. Since, at each step, the algorithm is checking for safe states before allocating resources, it tends to be very memory and processing-intensive and thus not very efficient for large systems.

When working with a banker's algorithm, it requests to know about three things:

  1. How much each process can request for each resource in the system. It is denoted by the [MAX] request.
  2. How much each process is currently holding each resource in a system. It is denoted by the [ALLOCATED] resource.
  3. It represents the number of each resource currently available in the system. It is denoted by the [AVAILABLE] resource.

Important Data Structures in the Banker's Algorithm

Suppose n is the number of processes, and m is the number of each type of resource used in a computer system.

  1. Available: It is an array of length 'm' that defines each type of resource available in the system. When Available[j] = K, means that 'K' instances of Resources type R[j] are available in the system.
  2. Max: It is a [n x m] matrix that indicates each process P[i] can store the maximum number of resources R[j] (each type) in a system.
  3. Allocation: It is a matrix of m x n orders that indicates the type of resources currently allocated to each process in the system. When Allocation [i, j] = K, it means that process P[i] is currently allocated K instances of Resources type R[j] in the system.
  4. Need: It is an M x N matrix sequence representing the number of remaining resources for each process. When the Need[i] [j] = k, then process P[i] may require K more instances of resources type Rj to complete the assigned work.
    Nedd[i][j] = Max[i][j] - Allocation[i][j].
  5. Finish: It is the vector of the order m. It includes a Boolean value (true/false) indicating whether the process has been allocated to the requested resources, and all resources have been released after finishing its task.

The Banker's Algorithm is essentially a combination of two components:

  • Safety Algorithm: Determines if the system is in a safe state, which means all the processes will eventually be allowed to execute without any deadlocks.
  • Resource Request Algorithm: It decides whether it is safe to grant the request made by the other process for allocating more resources to itself without violating the safety.

Safety Algorithm

A safe state is said to be a state from which the system can execute some sequence of processes without deadlock. A system is said to be safe if all its processes can be executed safely in some sequence.

It is a safety algorithm used to check whether or not a system is in a safe state or follows the safe sequence in a banker's algorithm:

1. There are two vectors, Wok and Finish, of length m and n in a safety algorithm.

Initialization:

  • Work: Initialize work = Available, where Available is the array that stores the number of available instances for each resource type.
  • Finish: Initialize the Finish array for each process such that Finish[i] = false; for I = 0, 1, 2, 3, 4… n - 1., indicating that no processes have finished at the start.

2. Check the availability status for each type of resource [i], such as:

If the i does not exist, go to step 4.

3. If a process i satisfies the conditions in step 2, simulate the allocation by updating the Work vector:

This adds the resources currently allocated to process I back to the Work vector, stimulating the release of resources after the process has finished.

Go to step 2 to check the status of resource availability for the next process.

4. If Finish[i] == true, it means that the system is safe for all processes.

Resource Request Algorithm

A Resource Request Algorithm A description of how the system behaves when a process requests a resource needs to be made. The algorithm will check whether the requested resources can be allocated without causing deadlock. The request can then be portrayed as a request matrix.

A resource request algorithm checks how a system will behave when a process makes each type of resource request in a system as a request matrix.

Let create a resource request array R[i] for each process P[i]. If the Resource Requesti [j] equal to 'K', which means the process P[i] requires 'k' instances of Resources type R[j] in the system.

Steps of the Resource Request Algorithm:

1. Check if the Request is within the Process's Maximum Claim:

At every request of resources done by a process P[i], the algorithm checks if the number of resources requested is less than or equal to the Need of that process:

If the condition holds, then it proceeds to the next one. Otherwise, process P[i] has exceeded its maximum claim; the request is denied.

If the process exceeds its maximum claim, this is then a violation of the rules of the system; the process cannot be allowed to proceed.

2. Check if the Requested Resources are Available:

The system now checks the availability of the requested resources in sufficient quantity. This is done by comparing the request [i] with the Available resources:

If this condition is satisfied, proceed to the next step. If not, then P[i] must wait until the required resources become available.

This makes sure the system does not allocate resources it does not currently have, which otherwise could lead to deadlock or unsafe states.

3. Simulate Resource Allocation:

If available, the system allocates temporary requested resources to process P[i] by modifying the Available, Allocation, and Need matrices.

4. Check System Safety:

  • After the allocation of the requested resources, the system runs the Safety Algorithm to check whether the system continues to be in a safe state after that allocation.
  • The resources are permanently allocated if the system is in a safe state to process P[i].
  • Suppose the system is entering an unsafe state. In that case, process P[i] must wait, and the system must roll back to its earlier state by recovering its original values of Available, Allocation, and Need matrices.

Example: Consider a system that contains five processes P1, P2, P3, P4, P5 and the three resource types A, B and C. Following are the resources types: A has 10, B has 5 and the resource type C has 7 instances.

ProcessAllocation
A         B         C
Max
A         B         C
Available
A         B         C
P10         1          07         5         33         3         2
P22         0         03         2         2
P33         0         29         0         2
P42         1         12         2         2
P50         0         24         3         3

Answer the following questions using the banker's algorithm:

  1. What is the reference of the need matrix?
  2. Determine if the system is safe or not.
  3. What will happen if the resource request (1, 0, 0) for process P1 can the system accept this request immediately?

Ans. 1: The context of the need matrix is as follows:

Need [i] = Max [i] - Allocation [i]
Need for P1: (7, 5, 3) - (0, 1, 0) = 7, 4, 3
Need for P2: (3, 2, 2) - (2, 0, 0) = 1, 2, 2
Need for P3: (9, 0, 2) - (3, 0, 2) = 6, 0, 0
Need for P4: (2, 2, 2) - (2, 1, 1) = 0, 1, 1
Need for P5: (4, 3, 3) - (0, 0, 2) = 4, 3, 1

ProcessNeed
A         B         C
P17         4         3
P21         2         2
P36         0         0
P40         1         1
P54         3         1

Hence, we created the context of need matrix.

Ans. 2: Apply the Banker's Algorithm:

Available Resources of A, B and C are 3, 3, and 2.

Now we check if each type of resource request is available for each process.

Step 1: For Process P1:

Need <= Available

7, 4, 3 <= 3, 3, 2 condition is false.

So, we examine another process, P2.

Step 2: For Process P2:

Need <= Available

1, 2, 2 <= 3, 3, 2 condition true

New available = available + Allocation

(3, 3, 2) + (2, 0, 0) => 5, 3, 2

Similarly, we examine another process P3.

Step 3: For Process P3:

P3 Need <= Available

6, 0, 0 < = 5, 3, 2 condition is false.

Similarly, we examine another process, P4.

Step 4: For Process P4:

P4 Need <= Available

0, 1, 1 <= 5, 3, 2 condition is true

New Available resource = Available + Allocation

5, 3, 2 + 2, 1, 1 => 7, 4, 3

Similarly, we examine another process P5.

Step 5: For Process P5:

P5 Need <= Available

4, 3, 1 <= 7, 4, 3 condition is true

New available resource = Available + Allocation

7, 4, 3 + 0, 0, 2 => 7, 4, 5

Now, we again examine each type of resource request for processes P1 and P3.

Step 6: For Process P1:

P1 Need <= Available

7, 4, 3 <= 7, 4, 5 condition is true

New Available Resource = Available + Allocation

7, 4, 5 + 0, 1, 0 => 7, 5, 5

So, we examine another process P2.

Step 7: For Process P3:

P3 Need <= Available

6, 0, 0 <= 7, 5, 5 condition is true

New Available Resource = Available + Allocation

7, 5, 5 + 3, 0, 2 => 10, 5, 7

Hence, we execute the banker's algorithm to find the safe state and the safe sequence like P2, P4, P5, P1 and P3.

Ans. 3: Whether the system can accommodate the resource request (1,0,0)

for the process, P1 can be checked by using Banker's Algorithm as follows:

1. Checking if the request is within the maximum claim of P1

If the resources requested: (1,0,0) are less than or equal to P1's Need, then go ahead; otherwise, reject.

2. Check whether the system has enough available resources to handle the request

(1,0,0). If the resources are available, go; otherwise, P1 should wait.

3. Simulate resource allocation:

Allocate the resources to P1 temporarily and check if the system is safe or not using the Safety Algorithm. If the system is safe, the request is granted. Otherwise, cancel the allocation and let P1 wait.