#include <stdio.
h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input();
void show();
void cal();
int main() {
printf("********** Banker's Algorithm ************\n");
input();
show();
cal();
printf("Press Enter to exit...");
getchar(); // Wait for the user to press Enter
return 0;
}
void input() {
int i, j;
printf("Enter the number of Processes: ");
scanf("%d", &n);
printf("Enter the number of resource instances: ");
scanf("%d", &r);
printf("Enter the Max Matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &max[i][j]);
}********** Banker's Algorithm ************
Enter the number of Processes: 3
Enter the number of resource instances: 2
Enter the Max Matrix:
7 5
3 2
9 0
Enter the Allocation Matrix:
0 1
2 0
3 2
Enter the Available Resources:
0 1
Process Allocation Max Available
P1 0 1 7 5 0 1
P2 2 0 3 2
P3 3 2 9 0
P1 -> P2 ->
Processes are in deadlock.
The system is in an unsafe state.
Press Enter to exit...
printf("Enter the Allocation Matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the Available Resources:\n");
for (j = 0; j < r; j++) {
scanf("%d", &avail[j]);
}
}
void show() {
int i, j;
printf("Process\t Allocation\t Max\t Available\n");
for (i = 0; i < n; i++) {
printf("P%d\t ", i + 1);
for (j = 0; j < r; j++) {
printf("%d ", alloc[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
if (i == 0) {
for (j = 0; j < r; j++) {
printf("%d ", avail[j]);
}
}
printf("\n");
}
}
void cal() {
int finish[100], k, flag = 1, c1 = 0;
int safe[100];
int i, j;
for (i = 0; i < n; i++) {
finish[i] = 0;
}
// Find need matrix
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("\n");
while (flag) {
flag = 0;
for (i = 0; i < n; i++) {
int c = 0;
for (j = 0; j < r; j++) {
if (finish[i] == 0 && need[i][j] <= avail[j]) {
c++;
}
}
if (c == r) {
for (k = 0; k < r; k++) {
avail[k] += alloc[i][k];
}
finish[i] = 1;
flag = 1;
printf("P%d -> ", i + 1);
}
}
}********** Banker's Algorithm ************
Enter the number of Processes: 3
Enter the number of resource instances: 2
Enter the Max Matrix:
7 5
3 2
9 0
Enter the Allocation Matrix:
0 1
2 0
3 2
Enter the Available Resources:
0 1
Process Allocation Max Available
P1 0 1 7 5 0 1
P2 2 0 3 2
P3 3 2 9 0
P1 -> P2 ->
Processes are in deadlock.
The system is in an unsafe state.
Press Enter to exit...
// Check if all processes are finished
for (i = 0; i < n; i++) {
if (finish[i] == 1) {
c1++;
} else {
printf("P%d -> ", i + 1);
}
}
if (c1 == n) {
printf("\nThe system is in a safe state.\n");
} else {
printf("\nProcesses are in deadlock.\n");
printf("The system is in an unsafe state.\n");
}
}
CASE 1:
********** Banker's Algorithm ************
Enter the number of Processes: 3
Enter the number of resource instances: 2
Enter the Max Matrix:
7 5
3 2
9 0
Enter the Allocation Matrix:
0 1
2 0
3 0
Enter the Available Resources:
3 3
Process Allocation Max Available
P1 0 1 7 5 3 3
P2 2 0 3 2
P3 3 0 9 0
P1 -> P2 -> P3 ->
The system is in a safe state.
Press Enter to exit...
CASE 2:
********** Banker's Algorithm ************
Enter the number of Processes: 3
Enter the number of resource instances: 2
Enter the Max Matrix:
7 5
3 2
9 0
Enter the Allocation Matrix:
0 1
2 0
3 2
Enter the Available Resources:
0 1
Process Allocation Max Available
P1 0 1 7 5 0 1
P2 2 0 3 2
P3 3 2 9 0
P1 -> P2 ->
Processes are in deadlock.
The system is in an unsafe state.
Press Enter to exit...