Banker’s Algorithm
Banker’s Algorithm
The resource-allocation graph algorithm works only when each resource type has one instance.
When there are multiple instances of each resource type, we use the Banker’s Algorithm for deadlock avoidance.
It is called the Banker’s Algorithm because:
Just like a banker gives loans only if he is sure he can satisfy all customers eventually, the OS allocates resources only if it guarantees no deadlock will occur.
Basic Idea
When a thread (process) enters the system:
-
It must declare its maximum resource requirement.
-
The system will grant a request only if the system remains in a safe state after allocation.
If granting a request makes the system unsafe → the thread must wait.
Important Data Structures
Suppose:
-
n = number of threads
-
m = number of resource types
We maintain:
Available (Vector of size m)
Example:
If Available = (3,3,2)
That means:
-
3 instances of A are free
-
3 instances of B are free
-
2 instances of C are free
Max (n × m Matrix)
This is declared before execution starts.
Allocation (n × m Matrix)
Need (n × m Matrix)
It represents how many more resources a thread may still request.
Safe State Concept
A system is in a safe state if:
There exists a sequence of threads such that each thread can finish with currently available resources plus resources released by previous threads.
If such a sequence exists → Safe state
If not → Unsafe state (may lead to deadlock)
Safety Algorithm (To Check Safe State)
-
Let Work and Finish be vectors of length m and n, respectively. Initialize Work = Available and Finish[i] = false for i = 0, 1, ..., n − 1.
-
Find an index i such that both
a. Finish[i] == false
b. Needi ≤ Work
If no such i exists, go to step 4. -
Work = Work + Allocationi
Finish[i] = true
Go to step 2. -
If Finish[i] == true for all i, then the system is in a safe state.
Explanation
Step 1: Initialization
Let:
-
Work = vector of length m
-
Finish = vector of length n
Initialize:
Step 2: Find a Suitable Thread
Find an index i such that:
-
Finish[i] == false -
Need[i] ≤ Work
If no such i exists, go to Step 4.
Step 3: Pretend Thread i Finishes
If such an i is found:
Then go back to Step 2.
Step 4: Check Final Condition
If:
Then:
✅ The system is in a safe state
Otherwise:
❌ The system is in an unsafe state
Time Complexity
The safety algorithm may require:
Where:
-
m = number of resource types
-
n = number of threads
Resource-Request Algorithm
2.If Requestᵢ ≤ Available, go to step 3. Otherwise, Tᵢ must wait, since the resources are not available.
3.Have the system pretend to have allocated the requested resources to thread Tᵢ by modifying the state as follows:
Available = Available − Requestᵢ
Allocationᵢ = Allocationᵢ + Requestᵢ
Needᵢ = Needᵢ − Requestᵢ
Explanation
When a thread Ti requests resources:
Let:
Step 1: Check Maximum Claim
If:
If not → error (exceeded maximum claim)
Step 2: Check Availability
If:
If not → thread must wait.
Step 3: Pretend Allocation
Temporarily update:
Then run Safety Algorithm.
-
If safe → grant request.
-
If unsafe → rollback changes and make thread wait.
Example
System:
-
5 Threads: T0–T4
-
3 Resource types: A (10), B (5), C (7)
Available = (3,3,2)
Need matrix:
| Thread | A | B | C |
|---|---|---|---|
| T0 | 7 | 4 | 3 |
| T1 | 1 | 2 | 2 |
| T2 | 6 | 0 | 0 |
| T3 | 0 | 1 | 1 |
| T4 | 4 | 3 | 1 |
Safe sequence:
So system is safe.
If T1 requests:
Check:
-
(1,0,2) ≤ (3,3,2) ✅
-
Pretend allocation
-
Run safety algorithm
-
New safe sequence exists
So request is granted and we arrive at the following new state
We must determine whether this new system state is safe. To do so, we execute our safety algorithm and find that the sequence <T1, T3, T4, T0, T2> satisfies the safety requirement. Hence, we can immediately grant the request of thread T1.
You should be able to see, however, that when the system is in this state, a request for (3,3,0) by T4 cannot be granted, since the resources are not available. Furthermore, a request for (0,2,0) by T0 cannot be granted, even though the resources are available, since the resulting state is unsafe.
Why It Works
The algorithm ensures:
-
The system never enters a deadlock state.
-
Every allocation guarantees at least one safe sequence.
Time Complexity
Safety algorithm may require:
Where:
-
m = number of resource types
-
n = number of threads
Key Points for Exams
✔ Used for multiple instances of resources
✔ Avoids deadlock (does NOT detect it)
✔ Requires advance knowledge of maximum need
✔ Uses:
-
Available
-
Max
-
Allocation
-
Need
✔ Uses safety algorithm before granting request


Comments
Post a Comment