LAB9
LAB9
SCXVCVSS
2024 S
SUBJECT: COMPUTER
SCIENCE
SUBMITTED BY
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.
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
3. NEED MATRIX: This shows how many more resources each process requires to finish
its execution. It is calculated as: Need=Maximum−Allocation\text{Need} = \
text{Maximum} - \text{Allocation}Need=Maximum−Allocation
4. SAFETY CHECK: The algorithm checks whether the system can find a sequence of
processes that will allow all processes to eventually complete. This is done by simulating
resource allocation to each process and checking if there exists a safe sequence.
1. Initialize:
Available: The available resources in the system.
Allocation: The matrix of resources allocated to each process.
Finish: Initially set to false for all processes, indicating that none of them have
finished yet.
3. Find a Process:
Need[i] <= Work (the process can finish with the current available
resources)
4. Allocate Resources:
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
Process A B C
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
P1 5 4 2
P2 1 1 1
P3 6 0 0
1. INITIALIZE:
Work = Available = (3, 3, 2)
2. FIRST ITERATION:
3. SECOND ITERATION:
4. THIRD ITERATION:
#include <iostream>
#include <vector>
using namespace std;
int completedProcesses = 0;
// Try to find a process that can finish
while (completedProcesses < numProcesses) {
bool progressMade = false;
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 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.
All processes are finished, in which case the system is in a safe 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
Process A B C
P1 4 3 1
P2 1 1 1
P3 6 0 0
o Finish = {false, false, false} (none of the processes have finished yet)
2. FIRST ITERATION:
3. SECOND ITERATION:
4. THIRD ITERATION:
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.