-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.c
132 lines (118 loc) · 3.4 KB
/
9.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//Banker's Algorithm
//For Deadlock Avoidance
//by Simply
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int avail[100];
int total_res[100];
int total_alloc[100];
int req[100];
struct process {
int pid;
int max[100];
int alloc[100];
int need[100];
int finish;
} p[20], temp;
void main() {
int i, j, m, n, flag = 0, x = 0, k = 0, sequence[20], availtemp[100], a = 0, f = 0, work[50];
printf("NUMBER OF RESOURCES: ");
scanf("%d", &m);
printf("MAXIMUM COUNT FOR:\n");
for (j = 0; j < m; j++) {
printf("\tRESOURCE %d: ", j);
scanf("%d", &total_res[j]);
}
printf("\nNUMBER OF PROCESSES: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
p[i].pid = i;
printf("\nALLOCATION FOR PROCESS %d: ", p[i].pid);
for (j = 0; j < m; j++)
scanf("%d", &p[i].alloc[j]);
printf("\nMAXIMUM FOR PROCESS %d: ", p[i].pid);
for (j = 0; j < m; j++)
scanf("%d", &p[i].max[j]);
for (j = 0; j < m; j++)
total_alloc[j] = total_alloc[j] + p[i].alloc[j];
for (j = 0; j < m; j++)
p[i].need[j] = p[i].max[j] - p[i].alloc[j];
p[i].finish = 0;
}
printf("\nTOTAL ALLOCATION\n");
for (i = 0; i < m; i++)
printf("%d", total_alloc[i]);
printf("\nAVAILABLE MATRIX\n");
for (i = 0; i < m; i++) {
avail[i] = total_res[i] - total_alloc[i];
work[i] = avail[i];
printf("%d", avail[i]);
}
for (i = 0; i < (n - 1) * m; i++)
availtemp[i] = 0;
while (a < m) {
for (i = 0; i < n; i++) {
if (p[i].finish != 1) {
for (j = 0; j < m; j++) {
if (work[j] >= p[i].need[j]) {
flag = 1;
} else {
flag = 0;
break;
}
}
if (flag != 0) {
sequence[x] = p[i].pid;
x++;
}
f = i * m;
for (j = 0; j < m; j++) {
if (flag != 0) {
work[j] += p[i].alloc[j];
availtemp[f] = work[j];
f++;
p[i].finish = 1;
}
}
}
}
a++;
}
printf("\nPROCESS\tMAXIMUM\t\tALLOCATED\tNEED\t\tAVAILABLE\n");
for (i = 0; i < n; i++) {
printf("\n%d\t", p[i].pid);
printf(" ");
for (j = 0; j < m; j++) {
printf("%d ", p[i].max[j]);
}
printf("\t ");
for (j = 0; j < m; j++) {
printf("%d ", p[i].alloc[j]);
}
printf("\t ");
for (j = 0; j < m; j++) {
printf("%d ", p[i].need[j]);
}
printf("\t ");
for (j = i * m; j < m * (i + 1); j++) {
printf("%d ", availtemp[j]);
}
printf("\n");
}
for (i = 0; i < n; i++) {
if (p[i].finish == 0)
f = 1;
else
f = 0;
}
if (f == 1)
printf("\nSYSTEM NOT SAFE");
else {
printf("SEQUENCE:\n");
for (i = 0; i < x; i++) {
printf("P%d->", sequence[i]);
}
printf("\nSYSTEM IN SAFE STATE");
}
}