LAB9
LAB9
SCXVCVSSS
SUBJECT: COMPUTER SCIENCE
ESSION:2024
2222202420
SUBMITTED BY
23-24
ASSIGNMENT:1
COURSE:OPERATING SYSTEM
. DEADLOCK AVOIDENCE
BANKER’S ALGORITHM
INTRODUCTION
The Banker’s Algorithm is a resource allocation and deadlock avoidance algorithm
that tests for safety by simulating the allocation for the predetermined maximum
possible amounts of all resources, then makes an “s-state” check to test for possible
activities, before deciding whether allocation should be allowed to continue.
The Banker’s Algorithm is a smart way for computer systems to manage how
programs use resources, like memory or CPU time. It helps prevent situations where
programs get stuck and can not finish their tasks. This condition is known as deadlock.
By keeping track of what resources each program needs and what’s available, the
banker algorithm makes sure that programs only get what they need in a safe order.
This helps computers run smoothly and efficiently, especially when lots of programs
are running at the same time.
IMPORTANT TERMS
Following are the important data structures terms applied in the banker's algorithm as follows:
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].
A safe state is one where there exists a sequence of processes that allows each process to obtain
its required resources, execute, and then release the resources, eventually allowing all processes
to complete without causing a deadlock. In a safe state, the system can allocate resources in such
a way that all processes can finish, ensuring that no circular wait or resource starvation occurs.
TERMINOLOGIES
1. Initialize:
Available: The available resources in the system.
Allocation: The matrix of resources allocated to each process.
Max: The maximum resources each process may require.
Need: The remaining resources each process needs to finish.
2. Work and Finish Arrays:
Work: Initially set to the available resources.
Finish: Initially set to false for all processes, indicating that none of them have
finished yet.
3. Find a Process:
Find a process i such that:
Finish[i] == false (the process is not yet finished)
Need[i] <= Work (the process can finish with the current available
resources)
4. Allocate Resources:
If such a process is found, simulate its completion:
Work = Work + Allocation[i] (resources are released after the process
finishes)
Finish[i] = true (mark the process as finished)
Repeat the process for all processes.
5. Check Safe State:
If all processes finish, the system is in a safe state.
If no process can finish and there are still processes that are not finished, the
system is in an unsafe state.
EXAMPLE
Consider a system with 3 processes (P1, P2, P3) and 3 resource types (A, B, C). The system is
defined by the following tables:
Resource Type A B C
Available 3 3 2
P1 7 5 3
P2 3 2 2
P3 9 0 2
Allocation Table
Process A B C
P1 2 1 1
P2 2 1 1
P3 3 2 2
Process A B C
P1 5 4 2
P2 1 1 1
P3 6 0 0
#include <iostream>
#include <vector>
using namespace std;
// Function to check if the system is in a safe state
bool isSafeState(vector<vector<int>>& allocation, vector<vector<int>>& need, vector<int>&
available) {
int numProcesses = allocation.size();
int numResources = available.size();
int completedProcesses = 0;
int main() {
// Define the Allocation, Need, and Available matrices
vector<vector<int>> allocation = {{2, 1, 1}, {2, 1, 1}, {3, 2, 2}};
vector<vector<int>> max = {{7, 5, 3}, {3, 2, 2}, {9, 0, 2}};
vector<int> available = {3, 3, 2};
int numProcesses = allocation.size();
int numResources = available.size();
return 0;
OUTPUT
}
CODE EXPLANATION:
1. INPUT: The program accepts the allocation matrix, the maximum need matrix, and the
available resources.
2. NEED MATRIX CALCULATION: It calculates the need matrix by subtracting the
allocation matrix from the maximum matrix.
3. SAFETY CHECK: The function isSafeState checks if the system is in a safe state by
simulating the execution of processes and checking if a process can finish with the
available resources.
4. OUTPUT: It outputs whether the system is in a safe state or an unsafe state based on the
result of the safety check.
ANALYSIS:
Time Complexity: The time complexity of the Banker's Algorithm is O(P * R), where P
is the number of processes and R is the number of resource types. This is because the
algorithm iterates over the processes and resource types to check safety.
Space Complexity: The space complexity is **O(P * R
An unsafe state is a state in which there is no guarantee that all processes can finish, meaning
that a deadlock may eventually occur.
In an unsafe state, it’s possible that some processes may be blocked indefinitely, waiting for
resources held by other processes, which leads to a circular wait.
The system is considered unsafe if there is no sequence of process executions that allows all
processes to eventually complete without the risk of deadlock.
1. INITIALIZATION:
Available: The number of resources available in the system.
Allocation: The matrix that indicates the number of resources allocated to each process.
Max: The matrix that indicates the maximum number of resources each process may need.
Need: The matrix that indicates the remaining resources each process needs. It is
calculated as: Need[i]=Max[i]−Allocation[i]\text{Need}[i] = \text{Max}[i] -
\text{Allocation}[i]Need[i]=Max[i]−Allocation[i]
2. SAFETY CHECK:
Initialize Work to be equal to Available (the currently available resources).
Initialize Finish to be an array of false values (indicating that no processes are finished
initially).
For each process i, check if Need[i] <= Work. If this condition is true, the process can
finish and release its allocated resources. This process will then be marked as finished, and
its resources will be added to Work.
Repeat this process until either:
All processes are finished, in which case the system is in a safe state.
No process can finish, indicating that the system is in an unsafe state.
3. UNSAFE STATE:
If no process can be found that can finish, it implies that the system is in an unsafe state.
The unsafe state does not necessarily mean that deadlock has occurred, but it implies that
deadlock is possible.
Consider the same system with 3 processes (P1, P2, P3) and 3 resource types (A, B, C).
Process
A B C
P1 7 5 3
P2 3 2 2
P3 9 0 2
Allocation Table
Process
A B C
P1 3 2 2
P2 2 1 1
P3 3 2 2
Need Table (Max - Allocation)
Process A B C
P1 4 3 1
P2 1 1 1
P3 6 0 0
1. INITIALIZATION:
o Work = Available = (3, 2, 2)
o Finish = {false, false, false} (none of the processes have finished yet)
2. FIRST ITERATION:
o Check process P1:
Need(P1) = (4, 3, 1) → Cannot finish because Need(P1) > Work
o Check process P2:
Need(P2) = (1, 1, 1) → Can finish because Need(P2) ≤ Work
Allocate resources to P2:
Work = Work + Allocation(P2) = (3, 2, 2) + (2, 1, 1) = (5, 3, 3)
Mark P2 as finished: Finish = {false, true, false}
3. SECOND ITERATION:
o Check process P1:
Need(P1) = (4, 3, 1) → Can finish because Need(P1) ≤ Work
Allocate resources to P1:
Work = Work + Allocation(P1) = (5, 3, 3) + (3, 2, 2) = (8, 5, 5)
Mark P1 as finished: Finish = {true, true, false}
4. THIRD ITERATION:
o Check process P3:
Need(P3) = (6, 0, 0) → Cannot finish because Need(P3) > Work (even
though Work has become (8, 5, 5), we still cannot satisfy P3's needs since
it requires 6 units of A, but only 3 units of A are available in total at any
point).
o No more processes can finish. We have reached an unsafe state because no
process can be completed, and there’s no safe sequence.
Since no process can be allocated resources to finish, the system is in an unsafe state.
CODE
#include <iostream>
#include <vector>
using namespace std;
int completedProcesses = 0;
int main() {
// Define the Allocation, Need, and Available matrices
vector<vector<int>> allocation = {{3, 2, 2}, {2, 1, 1}, {3, 2, 2}};
vector<vector<int>> max = {{7, 5, 3}, {3, 2, 2}, {9, 0, 2}};
vector<int> available = {3, 2, 2};
int numProcesses = allocation.size();
int numResources = available.size();
return 0;
}
CODE EXPLANATION:
INPUT: The program accepts the allocation matrix, the maximum need matrix, and the
available resources.
NEED MATRIX CALCULATION: It calculates the need matrix by subtracting the
allocation matrix from the maximum matrix.
SAFETY CHECK: The function isUnsafeState checks if the system is in an unsafe state
by simulating the execution of processes and checking if any process can finish with the
available resources.
OUTPUT: It outputs whether the system is in a safe state or an unsafe state based on the
result of the safety check.
ANALYSIS:
TIME COMPLEXITY: The time complexity of the Banker's Algorithm remains O(P *
R), where P is the number of processes and R is the number of resource types. The
algorithm iterates over the processes and resource types to check safety.
SPACE COMPLEXITY: The space complexity is O(P * R) due to the need for storing
the allocation, maximum, need matrices, and the finish array.
In the case of an unsafe state, the system may eventually reach deadlock if resources are
allocated in a way that prevents any process from completing. The unsafe state indicates that
deadlock is possible but not necessarily inevitable, depending on how resources are requested
and allocated in the future.