-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAlgo.c
More file actions
78 lines (68 loc) · 1.89 KB
/
Copy pathBankAlgo.c
File metadata and controls
78 lines (68 loc) · 1.89 KB
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
76
77
78
#include <stdio.h>
int main() {
int i, j, k;
int nop, nor, need[50][50] = {0}, max[50][50] = {0}, alloc[50][50] = {0};
int resources[50] = {0}, avlbl[50];
int finished[50] = {0};
printf("Enter the number of process : ");
scanf("%d", &nop);
printf("Enter the number of resources : ");
scanf("%d", &nor);
printf("Enter the allocation matrix\n");
for(i = 1; i <= nop; i++) {
for(j = 1; j <= nor; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the maximum allocation matrix\n");
for(i = 1; i <= nop; i++) {
for(j = 1; j <= nor; j++) {
scanf("%d", &max[i][j]);
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("Enter the resources instance\n");
for(i = 1; i <= nor; i++) {
printf("R%d :- ", i);
scanf("%d", &resources[i]);
}
for(i = 1; i <= nor; i++) {
int rsum = 0;
for(j = 1; j <= nop; j++) {
rsum += alloc[j][i];
}
avlbl[i] = resources[i] - rsum;
}
int count = 0;
printf("Safe sequence : ");
while(1) {
int progress = 0;
for(int i = 1; i <= nop; i++) {
if(finished[i] == 0) {
for(j = 1; j <= nor; j++) {
if(avlbl[j] < need[i][j]) {
break;
}
}
if(j > nor) {
for(k = 1; k <= nor; k++) {
avlbl[k] = avlbl[k] + alloc[i][k];
}
finished[i] = 1;
count++;
progress = 1;
printf("P%d ", i);
}
}
}
if(!progress || count >= nop) {
break;
}
}
if(count == nop) {
printf("No deadlock");
} else {
printf("Deadlock");
}
return 0;
}