forked from 11-yashasvi/Basic_Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankers.c
75 lines (64 loc) · 1.92 KB
/
Bankers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
int isSufficient(int need[], int available[], int n){
int sufficient = 1;
for (int i = 0; i < n; i++){
if(available[i]<need[i]) {
sufficient = 0;
break;
}
}
return sufficient;
}
int main(){
int row, col;
printf("Enter Number of Processes: ");
scanf("%d", &row);
printf("Enter Number of Resources: ");
scanf("%d", &col);
int max[row][col], allocation[row][col], need[row][col], available[col];
for (int i = 0; i < row; i++){
printf("Enter Max Values for Process %d:\n", i+1);
for (int j = 0; j < col; j++) scanf("%d", &max[i][j]);
}
printf("\n");
for (int i = 0; i < row; i++){
printf("Enter Allocation Values for Process %d:\n", i+1);
for (int j = 0; j < col; j++) scanf("%d", &max[i][j]);
}
printf("\n");
printf("Enter Available Resources\n");
for (int i = 0; i < col; i++) scanf("%d", &available[i]);
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
need[i][j] = max[i][j] - allocation[i][j];
}
}
int safestate[row];
int completed[row];
int safeprocesses = 0;
int solveable;
do{
solveable = -1;
for (int i = 0; i < row; i++){
if(completed[i]!=1){
if(isSufficient(need[i], available, row)){
solveable = 1;
safestate[safeprocesses] = i;
safeprocesses++;
completed[i] = 1;
for (int j = 0; j < row; j++){
available[j] += allocation[i][j];
}
}
}
}
}while(safeprocesses!=row && solveable!=-1);
if(safeprocesses!=row) printf("Not Safe");
else {
for (int i = 0; i < row; i++){
printf("%d ", safestate[i]);
}
}
return 0;
}