Banker's algorithm
Banker's algorithm
Introduction
The Safety Algorithm and Banker's Algorithm are fundamental concepts in operating systems used for
deadlock avoidance in resource allocation. These algorithms ensure that a system can allocate resources to
processes without entering an unsafe or deadlocked state.
1. Key Concepts
1. Resources: Resources (e.g., CPU, memory, I/O devices) can be allocated to processes. Each resource
has a finite number of instances.
2. Processes: Processes request and release resources during execution.
3. State:
o Safe State: The system can allocate resources to all processes in some order without leading to a
deadlock.
o Unsafe State: The system cannot guarantee that all processes will finish without deadlock.
2. Safety Algorithm
The Safety Algorithm determines whether the system is in a safe state by simulating the allocation of
resources.
1. Initialize:
o Work = Available (vector representing available resources).
o Finish[i] = False for all processes i (indicating whether a process can finish).
2. Find a Process:
o Look for a process P such that:
▪ Finish[i] = False, and
▪ Need[i] ≤ Work.
o If no such process exists, go to step 4.
3. Simulate Resource Allocation:
o If P is found:
▪ Work = Work + Allocation[i].
▪ Finish[i] = True.
▪ Repeat step 2.
4. Check State:
o If all Finish[i] = True for all processes, the system is in a safe state.
o Otherwise, it is unsafe.
Example:
Given:
• Available = [3, 3, 2]
• Allocation and Need matrices for processes P0–P4:
3. Banker's Algorithm
The Banker's Algorithm extends the Safety Algorithm by handling dynamic resource requests. It determines
whether a request can be granted safely.
1. Request Validation:
o Check if the process's request (Request[i]) is valid:
▪ Request[i] ≤ Need[i].
▪ Request[i] ≤ Available.
o If not, the request is invalid.
2. Pretend Allocation:
o Temporarily allocate the requested resources:
▪ Available = Available - Request[i].
▪ Allocation[i] = Allocation[i] + Request[i].
▪ Need[i] = Need[i] - Request[i].
3. Run the Safety Algorithm:
o Check if the system remains in a safe state after the allocation.
4. Decision:
o If the state is safe:
▪ Grant the request permanently.
o If the state is unsafe:
▪ Roll back to the original state.
Example:
Using the same system configuration as the Safety Algorithm, process P1 requests resources [1, 0, 2].
4. Comparison
Advantages:
Limitations:
Allocation Matrix:
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
Maximum Matrix:
A B C
P0 7 5 3
P1 3 2 2
P2 9 0 2
P3 2 2 2
P4 4 3 3
Available Resources: A = 3, B = 3, C = 2
Allocation Matrix:
R1 R2 R3
P0 0 1 0
P1 2 0 0
P2 3 0 2
Maximum Matrix:
R1 R2 R3
P0 7 5 3
P1 3 2 2
P2 9 0 2
Available Resources: R1 = 3, R2 = 3, R3 = 2
Process P0 makes a request for (2, 0, 0). Using the Banker's Algorithm, determine whether this request can be safely
granted.
Allocation Matrix:
X Y Z
P0 1 2 2
P1 2 0 1
P2 3 1 0
P3 0 0 2
Maximum Matrix:
X Y Z
P0 3 3 3
P1 2 2 2
P2 3 3 2
P3 1 1 2
Available Resources: X = 2, Y = 1, Z = 1
If process P2 finishes and releases its allocated resources, update the available resources and determine if the system is
now in a safe state.
Allocation Matrix:
A B C D
P0 0 1 0 3
P1 2 0 1 1
P2 3 0 0 2
P3 2 1 1 0
P4 0 0 2 1
Maximum Matrix:
A B C D
P0 7 5 3 4
P1 3 2 2 2
P2 9 0 2 3
P3 4 2 2 1
P4 4 3 3 3
Available Resources: A = 1, B = 1, C = 2, D = 1