bankers algorithm
bankers algorithm
h>
#include <conio.h>
#define MAX 10
int main() {
int alloc[MAX][MAX], max[MAX][MAX], need[MAX][MAX];
int avail[MAX], completed[MAX], safeSeq[MAX];
int p, r, i, j, process, count = 0;
int flag;
clrscr();
// Banker's Algorithm
while (count < p) {
flag = 0;
for (i = 0; i < p; i++) {
if (!completed[i]) {
int canAllocate = 1;
for (j = 0; j < r; j++) {
if (need[i][j] > avail[j]) {
canAllocate = 0;
break;
}
}
if (canAllocate) {
for (j = 0; j < r; j++) {
avail[j] += alloc[i][j];
}
safeSeq[count++] = i;
completed[i] = 1;
flag = 1;
}
}
}
if (!flag) {
break;
}
}
// Result
if (count == p) {
printf("\nSystem is in a safe state.\nSafe sequence is: ");
for (i = 0; i < p; i++) {
printf("P%d ", safeSeq[i]);
}
} else {
printf("\nSystem is not in a safe state. Deadlock may occur.");
}
getch();
return 0;
}