Hello! Welcome to your lesson on the Banker's Algorithm.
In our last session, we established the critical difference between safe, unsafe, and deadlocked states. The key takeaway was that to prevent deadlocks, a system must use a deadlock avoidance strategy to ensure it never enters an unsafe state.
Today, we will learn the most famous deadlock avoidance algorithm: the Banker's Algorithm. Your goal for this lesson is to apply the Banker's algorithm (both its Safety and Resource-Request components) to determine if a system is in a safe state and to decide whether to grant new resource requests. This is a classic and high-yield topic for the GATE exam, so mastering the step-by-step application is crucial.
The Foundation: Data Structures for Banker's Algorithm
The Banker's Algorithm operates on a snapshot of the system's resource allocation state. To make decisions, it relies on several key data structures, which are typically represented as vectors and matrices. Let's assume we have 'n' processes and 'm' resource types.
- Available: A vector of length 'm'.
Available[j] = kmeans there arekinstances of resource typeRjavailable. - Max: An n x m matrix.
Max[i][j] = kmeans processPimay request a maximum ofkinstances of resource typeRj. This information must be known a priori. - Allocation: An n x m matrix.
Allocation[i][j] = kmeans processPiis currently allocatedkinstances of resource typeRj. - Need: An n x m matrix.
Need[i][j] = kmeans processPimay still needkmore instances of resource typeRjto complete its task. This is calculated as:
With these structures defined, we can now explore the two main components of the algorithm.
Part 1: The Safety Algorithm - Is the System Safe?
The first part of the Banker's Algorithm is the Safety Algorithm. Its purpose is to answer a fundamental question: given the current state (Available, Max, Allocation), is the system in a safe state? It does this by trying to find a "safe sequence" of processes that can all run to completion.
The following video from the "Gate Smashers" channel provides an excellent walkthrough of a typical GATE problem, demonstrating how to set up the data structures and apply the Safety Algorithm step-by-step.
L-4.5: Deadlock Avoidance Banker's Algorithm with Example |With English Subtitles
This video walks through a complete example of the Banker's Algorithm, showing how to calculate the 'Need' and 'Available' matrices and then applying the Safety Algorithm to find a safe sequence.
Watch the video from 02:26 to 22:21. Pay close attention to: How the initial 'Available' resources are calculated from the total system resources and current allocations. The calculation of the 'Remaining Need' matrix ('Need = Max - Allocation'). The core iterative logic: comparing each process's 'Need' vector with the 'Available' vector. How the 'Available' vector is updated after a process is hypothetically executed and releases its resources.
Summary of the Safety Algorithm
As you saw in the video, the algorithm systematically checks if there's a way for all processes to finish. Here's a formal summary of the steps:
-
Initialization:
- Let
Workbe a vector of lengthm, initialized toAvailable. - Let
Finishbe a boolean vector of lengthn, initialized tofalsefor all processes.
- Let
-
Find a Process: Find a process
Pisuch that both conditions are true:Finish[i] == falseNeed[i] <= Work(This is a vector comparison; each element ofNeed[i]must be less than or equal to the corresponding element ofWork).
-
If no such process exists: Go to Step 5.
-
Execute the Process: If such a process
Piis found:Work = Work + Allocation[i](SimulatePifinishing and releasing its resources).Finish[i] = true.- Append
Pito the safe sequence. - Go back to Step 2.
-
Conclusion: If
Finish[i] == truefor alli, the system is in a safe state, and the sequence you found is a valid safe sequence. Otherwise, the system is in an unsafe state.
Test your understanding!
Consider a system with 3 resource types (X, Y, Z) and 3 processes (P0, P1, P2). The total instances of each resource are [X=5, Y=5, Z=5]. The current state is given below.
| Process | Allocation (X,Y,Z) | Request (Need) (X,Y,Z) |
|---|---|---|
| P0 | (1, 2, 1) | (1, 0, 3) |
| P1 | (2, 0, 1) | (0, 1, 2) |
| P2 | (2, 2, 1) | (1, 2, 0) |
Is the system in a safe state? If so, provide one valid safe sequence. (This problem is adapted from Problem-01 in the GateVidyalay resource).
Show answer
-
Calculate Total Allocated:
- Allocated(X) = 1 + 2 + 2 = 5
- Allocated(Y) = 2 + 0 + 2 = 4
- Allocated(Z) = 1 + 1 + 1 = 3
- Total Allocated =
[5, 4, 3]
-
Calculate Available:
- Available = Total Resources - Total Allocated
- Available =
[5, 5, 5] - [5, 4, 3] = [0, 1, 2] - So,
Work = [0, 1, 2]
-
Run Safety Algorithm:
- Iteration 1:
- P0's Need
[1,0,3]> Work[0,1,2](needs X). - P1's Need
[0,1,2]<= Work[0,1,2]. Yes! Execute P1. - New Work = Work + P1's Allocation =
[0,1,2] + [2,0,1] = [2,1,3]. - Safe sequence so far:
<P1>
- P0's Need
- Iteration 2:
- P0's Need
[1,0,3]<= Work[2,1,3]. Yes! Execute P0. - New Work = Work + P0's Allocation =
[2,1,3] + [1,2,1] = [3,3,4]. - Safe sequence so far:
<P1, P0>
- P0's Need
- Iteration 3:
- P2's Need
[1,2,0]<= Work[3,3,4]. Yes! Execute P2. - New Work = Work + P2's Allocation =
[3,3,4] + [2,2,1] = [5,5,5]. - Safe sequence so far:
<P1, P0, P2>
- P2's Need
- Iteration 1:
All processes have finished. The system is in a safe state, and a valid safe sequence is <P1, P0, P2>.
Part 2: The Resource-Request Algorithm - Can We Grant a Request?
Knowing if the current state is safe is only half the battle. The real power of the Banker's Algorithm comes from its ability to decide if an incoming resource request can be granted without moving the system into an unsafe state. This is handled by the Resource-Request Algorithm.
The following video provides an excellent conceptual overview and then dives into detailed examples of how to evaluate a request.
Banker's Algorithm: Resource Allocation | L 24 | Operating System | GATE 2022 CSE #VishvadeepGothi
This video from Unacademy Computer Science explains the Resource-Request algorithm. It starts with an intuitive analogy and then formalizes the steps for checking a request and ensuring the system remains safe afterward.
Watch the following segments carefully: Conceptual Steps (14:33 - 15:17): Get a high-level overview of the algorithm's logic. Formalizing the Checks (19:50 - 26:01): Understand the first two critical checks: Request <= Need and Request <= Available. Putting it all Together (26:01 - 33:21) & (36:08 - 42:28): Watch the two complete examples. Observe how the system state is temporarily modified to check for safety before the request is either granted or denied. Focus on the process of pretending to allocate the resources and then running the Safety Algorithm.
Summary of the Resource-Request Algorithm
When a process Pi makes a request for resources, Request_i, the system performs the following steps:
-
Check Validity: Is
Request_i <= Need_i?- If no, terminate the request with an error. The process has exceeded its maximum declared claim.
- If yes, proceed to the next step.
-
Check Availability: Is
Request_i <= Available?- If no, process
Pimust wait. There aren't enough free resources right now. - If yes, proceed to the next step.
- If no, process
-
Pretend to Allocate: The system now simulates the allocation to see if it results in a safe state. It modifies the data structures temporarily:
Available = Available - Request_iAllocation[i] = Allocation[i] + Request_iNeed[i] = Need[i] - Request_i
-
Run the Safety Algorithm: Using the new, modified state from Step 3, run the full Safety Algorithm.
-
Decision:
- If the Safety Algorithm determines the new state is safe, the resource request is granted, and the state changes from Step 3 become permanent.
- If the Safety Algorithm determines the new state is unsafe, the request is denied. Process
Pimust wait, and the system state is reverted to what it was before Step 3.
This ensures the system only moves from one safe state to another safe state, thus avoiding deadlock.
Test your understanding!
Consider a system in a safe state with Available = [3, 2, 2] and the following Need matrix.
| Process | Allocation (X,Y,Z) | Max (X,Y,Z) | Need (X,Y,Z) |
|---|---|---|---|
| P0 | (0, 0, 1) | (8, 4, 3) | (8, 4, 2) |
| P1 | (3, 2, 0) | (6, 2, 0) | (3, 0, 0) |
| P2 | (2, 1, 1) | (3, 3, 3) | (1, 2, 2) |
Now, consider two independent requests:
- REQ1: P0 requests
(0, 0, 2) - REQ2: P1 requests
(2, 0, 0)
Which of these requests can be granted immediately? (This problem is from Problem-02 in the GateVidyalay resource).
Show answer
Evaluating REQ1: P0 requests (0, 0, 2)
- Check Validity: Is
Request(0,0,2) <= P0's Need(8,4,2)? Yes. - Check Availability: Is
Request(0,0,2) <= Available(3,2,2)? Yes. - Pretend to Allocate:
- New Available =
[3,2,2] - [0,0,2] = [3,2,0] - P0's New Allocation =
[0,0,1] + [0,0,2] = [0,0,3] - P0's New Need =
[8,4,2] - [0,0,2] = [8,4,0]
- New Available =
- Run Safety Check on new state:
Work = [3,2,0]- Check P0 (Need
[8,4,0]): No. - Check P1 (Need
[3,0,0]): Yes. New Work =[3,2,0] + [3,2,0] = [6,4,0]. - Check P2 (Need
[1,2,2]): No (needs Z). - Check P0 again (Need
[8,4,0]): No (needs X). - No process can run. The system would enter an unsafe state.
- Decision: REQ1 is denied.
Evaluating REQ2: P1 requests (2, 0, 0) (Starting from the original state)
- Check Validity: Is
Request(2,0,0) <= P1's Need(3,0,0)? Yes. - Check Availability: Is
Request(2,0,0) <= Available(3,2,2)? Yes. - Pretend to Allocate:
- New Available =
[3,2,2] - [2,0,0] = [1,2,2] - P1's New Allocation =
[3,2,0] + [2,0,0] = [5,2,0] - P1's New Need =
[3,0,0] - [2,0,0] = [1,0,0]
- New Available =
- Run Safety Check on new state:
Work = [1,2,2]- Check P0 (Need
[8,4,2]): No. - Check P1 (New Need
[1,0,0]): Yes. New Work =[1,2,2] + [5,2,0] = [6,4,2]. - Check P2 (Need
[1,2,2]): Yes. New Work =[6,4,2] + [2,1,1] = [8,5,3]. - Check P0 (Need
[8,4,2]): Yes. New Work =[8,5,3] + [0,0,1] = [8,5,4]. - A safe sequence
<P1, P2, P0>was found. The new state is safe. - Decision: REQ2 can be granted.
Therefore, only REQ2 can be permitted.
Conclusion
In this lesson, you have learned to apply both components of the Banker's Algorithm, a cornerstone of deadlock avoidance.
Key Takeaways:
- The Banker's Algorithm requires prior knowledge of the maximum resource needs of all processes.
- The Safety Algorithm determines if the current system state is safe by trying to find a valid safe sequence. The core check is
Need[i] <= Work. - The Resource-Request Algorithm decides if a request can be granted by first checking for validity (
Request <= Need) and availability (Request <= Available), and then temporarily allocating the resources and running the Safety Algorithm to ensure the resulting state is still safe.
Preview of the Next Lesson:
While deadlock avoidance is a powerful proactive strategy, it's not always practical because processes rarely know their maximum resource needs in advance. In the next lesson, we will explore a reactive strategy: deadlock detection. You will learn to apply the deadlock detection algorithm for systems with multiple resource instances to find out if a deadlock has already occurred.