0% found this document useful (0 votes)
10 views

LAB9

Uploaded by

Zurghuna Gul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

LAB9

Uploaded by

Zurghuna Gul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

SESSION:2023-2024

SCXVCVSSS
SUBJECT: COMPUTER SCIENCE
ESSION:2024
2222202420
SUBMITTED BY
23-24

 NAME: ZURGHUNA GUL

 ROLL NO: 13646

 ASSIGNMENT:1

 SUBMISSION DATE: 27TH OCT

 SUBMITTED TO: SIR IMRAN

 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].

SAFE STATE IN BANKER'S ALGORITHM

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. AVAILABLE RESOURCES: This is the number of resources currently available in the


system.
2. Allocation Matrix: This shows how many resources each process is currently holding.
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.

STEPS OF THE BANKER'S ALGORITHM FOR SAFE STATE

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

Maximum Need Table


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

Remaining Need Table (Max - Allocation)

Process A B C

P1 5 4 2

P2 1 1 1

P3 6 0 0

STEPS TO CHECK IF THE SYSTEM IS IN A SAFE STATE


1. INITIALIZE:
 Work = Available = (3, 3, 2)
 Finish = {false, false, false}
2. FIRST ITERATION:
 Check process P1:
 Need(P1) = (5, 4, 2) → Cannot finish because Need(P1) > Work
 Check process P2:
 Need(P2) = (1, 1, 1) → Can finish because Need(P2) <= Work
 Allocate resources to P2:
 Work = Work + Allocation(P2) = (3, 3, 2) + (2, 1, 1) = (5, 4, 3)
 Mark P2 as finished: Finish = {false, true, false}
3. SECOND ITERATION:
 Check process P1:
 Need(P1) = (5, 4, 2) → Can finish because Need(P1) <= Work
 Allocate resources to P1:
 Work = Work + Allocation(P1) = (5, 4, 3) + (2, 1, 1) = (7, 5, 4)
 Mark P1 as finished: Finish = {true, true, false}
4. THIRD ITERATION:
 Check process P3:
 Need(P3) = (6, 0, 0) → Can finish because Need(P3) <= Work
 Allocate resources to P3:
 Work = Work + Allocation(P3) = (7, 5, 4) + (3, 2, 2) = (10, 7, 6)
 Mark P3 as finished: Finish = {true, true, true}

Since all processes can finish, the system is in a safe state.

CODE FOR BANKER'S ALGORITHM

#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();

// Work vector to track available resources


vector<int> work = available;
vector<bool> finish(numProcesses, false);

int completedProcesses = 0;

// Try to find a process that can finish


while (completedProcesses < numProcesses) {
bool progressMade = false;

for (int i = 0; i < numProcesses; ++i) {


if (!finish[i]) {
bool canFinish = true;
for (int j = 0; j < numResources; ++j) {
if (need[i][j] > work[j]) {
canFinish = false;
break;
}
}

// If process can finish


if (canFinish) {
for (int j = 0; j < numResources; ++j) {
work[j] += allocation[i][j]; // Simulate allocation
}
finish[i] = true;
completedProcesses++;
progressMade = true;
break;
}
}
}

// If no process could make progress, the system is in an unsafe state


if (!progressMade) {
return false;
}
}

return true; // All processes finished, system is in a safe state


}

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();

// Calculate the Need matrix


vector<vector<int>> need(numProcesses, vector<int>(numResources));
for (int i = 0; i < numProcesses; ++i) {
for (int j = 0; j < numResources; ++j) {
need[i][j] = max[i][j] - allocation[i][j];
}
}

// Check if the system is in a safe state


if (isSafeState(allocation, need, available)) {
cout << "The system is in a safe state." << endl;
} else {
cout << "The system is in an unsafe state." << endl;
}

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

UNSAFE STATE IN BANKER'S ALGORITHM

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.

STEPS TO DETERMINE UNSAFE STATE (BANKER'S ALGORITHM)

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.

EXAMPLE OF UNSAFE STATE

Consider the same system with 3 processes (P1, P2, P3) and 3 resource types (A, B, C).

Resource Type ABC


Available 32 2
Maximum Need Table

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

STEPS TO CHECK IF THE SYSTEM IS IN AN UNSAFE STATE

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;

// Function to check if the system is in an unsafe state


bool isUnsafeState(vector<vector<int>>& allocation, vector<vector<int>>& need, vector<int>&
available) {
int numProcesses = allocation.size();
int numResources = available.size();
// Work vector to track available resources
vector<int> work = available;
vector<bool> finish(numProcesses, false);

int completedProcesses = 0;

// Try to find a process that can finish


while (completedProcesses < numProcesses) {
bool progressMade = false;

for (int i = 0; i < numProcesses; ++i) {


if (!finish[i]) {
bool canFinish = true;
for (int j = 0; j < numResources; ++j) {
if (need[i][j] > work[j]) {
canFinish = false;
break;
}
}

// If process can finish


if (canFinish) {
for (int j = 0; j < numResources; ++j) {
work[j] += allocation[i][j]; // Simulate allocation
}
finish[i] = true;
completedProcesses++;
progressMade = true;
break;
}
}
}

// If no process could make progress, the system is in an unsafe state


if (!progressMade) {
return true; // System is unsafe
}
}

return false; // All processes finished, system is safe


}

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();

// Calculate the Need matrix


vector<vector<int>> need(numProcesses, vector<int>(numResources));
for (int i = 0; i < numProcesses; ++i) {
for (int j = 0; j < numResources; ++j) {
need[i][j] = max[i][j] - allocation[i][j];
}
}

// Check if the system is in an unsafe state


if (isUnsafeState(allocation, need, available)) {
cout << "The system is in an unsafe state." << endl;
} else {
cout << "The system is in a safe state." << endl;
}

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.

You might also like