[go: up one dir, main page]

0% found this document useful (0 votes)
13 views4 pages

Lab 8 Os

The document describes the Banker's algorithm for deadlock avoidance. It includes code to implement the algorithm using C. The code initializes resource allocation and maximum demand matrices for processes and calculates the need matrix. It then checks for a safe sequence using the allocation, maximum demand, and available resources.

Uploaded by

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

Lab 8 Os

The document describes the Banker's algorithm for deadlock avoidance. It includes code to implement the algorithm using C. The code initializes resource allocation and maximum demand matrices for processes and calculates the need matrix. It then checks for a safe sequence using the allocation, maximum demand, and available resources.

Uploaded by

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

Nikhil

22104072

LAB 8

Q. Banker’s algorithm for deadlock avoidance?

#include <stdio.h>
#include <stdbool.h>
#define NUM_PROCESSES
3
#define NUM_RESOURCES 4
int available[NUM_RESOURCES];

int allocation[NUM_PROCESSES][NUM_RESOURCES];
int max_demand[NUM_PROCESSES][NUM_RESOURCES];
int need[NUM_PROCESSES][NUM_RESOURCES];
bool isSafe() {
int work[NUM_RESOURCES];

bool finish[NUM_PROCESSES] = {false};


for (int i = 0; i < NUM_RESOURCES; ++i) {
work[i] = available[i]
}
int count = 0;

while (count < NUM_PROCESSES)


{ bool found = false;
for (int i = 0; i < NUM_PROCESSES; ++i) {
if (!finish[i]) {
bool canExecute = true;

for (int j = 0; j < NUM_RESOURCES; ++j)


{ if (need[i][j] > work[j]) {
canExecute = false;
break;
}

if (canExecute) {
for (int j = 0; j < NUM_RESOURCES; ++j)
{ work[j] += allocation[i][j];
}
finish[i] = true;
found = true;
count++;
}

}
}
if (!found) {
break;
}

}
if (count == NUM_PROCESSES)
{ printf("Safe sequence: ");
for (int i = 0; i < NUM_PROCESSES; ++i)
{ printf("%d ", i);
}
printf("\n");
return true;
}
else {

printf("No safe sequence exists.\n");


return false;
}
}

int main() {
// Initialize available resources
available[0] = 1; available[1] = 1; available[2] = 1; available[3] = 1;
// Case 1: Safe Sequence
allocation[0][0] = 0; allocation[0][1] = 1; allocation[0][2] = 0; allocation[0][3] = 0;

allocation[1][0] = 2; allocation[1][1] = 0; allocation[1][2] = 0; allocation[1][3] = 1;


allocation[2][0] = 0; allocation[2][1] = 0; allocation[2][2] = 1; allocation[2][3] = 0;
max_demand[0][0] = 1; max_demand[0][1] = 2; max_demand[0][2] = 1; max_demand[0][3] =
2;
max_demand[1][0] = 3; max_demand[1][1] = 0; max_demand[1][2] = 1; max_demand[1][3] =
2;
max_demand[2][0] = 1; max_demand[2][1] = 1; max_demand[2][2] = 2; max_demand[2][3] =
1;
// Calculate need matrix

for (int i = 0; i < NUM_PROCESSES; ++i)


{ for (int j = 0; j < NUM_RESOURCES; ++j) {
need[i][j] = max_demand[i][j] - allocation[i][j];
}
}

printf("Case 1:\n");
isSafe();
// Case 2: No Safe Sequence
allocation[0][0] = 0; allocation[0][1] = 1; allocation[0][2] = 0; allocation[0][3] = 0;
allocation[1][0] = 2; allocation[1][1] = 0; allocation[1][2] = 2; allocation[1][3] = 1;

allocation[2][0] = 0; allocation[2][1] = 0; allocation[2][2] = 1; allocation[2][3] = 0;


max_demand[0][0] = 1; max_demand[0][1] = 2; max_demand[0][2] = 1; max_demand[0][3] =
2;
max_demand[1][0] = 3; max_demand[1][1] = 0; max_demand[1][2] = 3; max_demand[1][3] =
2;
max_demand[2][0] = 1; max_demand[2][1] = 1; max_demand[2][2] = 2; max_demand[2][3] =
1;
// Calculate need matrix
for (int i = 0; i < NUM_PROCESSES; ++i)
{ for (int j = 0; j < NUM_RESOURCES; ++j) {
need[i][j] = max_demand[i][j] - allocation[i][j];
}
}
printf("\nCase 2:\n");
isSafe();
return 0;
}

You might also like