forked from sahilbansalweb/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
banker.c
130 lines (107 loc) · 3.08 KB
/
banker.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
#include <stdio.h>
int m, n, i, j, al[10][10], max[10][10], av[10], need[10][10], temp, z, y, p, k;
void main() {
printf("\n Enter no of processes : ");
scanf("%d", &m); // enter numbers of processes
printf("\n Enter no of resources : ");
scanf("%d", &n); // enter numbers of resources
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("\n Enter instances for al[%d][%d] = ", i,j); // al[][] matrix is for allocated instances
scanf("%d", &al[i][j]);
// al[i][j]=temp;
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("\n Enter instances for max[%d][%d] = ", i,j); // max[][] matrix is for max instances
scanf("%d", &max[i][j]);
}
}
for (i = 0; i < n; i++) {
printf("\n Available Resource for av[%d] = ",i); // av[] matrix is for available instances
scanf("%d", &av[i]);
}
// Print allocation values
printf("Alocation Values :\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf(" \t %d", al[i][j]); // printing allocation matrix
}
printf("\n");
}
printf("\n\n");
// Print max values
printf("Max Values :\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf(" \t %d", max[i][j]); // printing max matrix
}
printf("\n");
}
printf("\n\n");
// Print need values
printf("Need Values :\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
need[i][j] = max[i][j] - al[i][j]; // calculating need matrix
printf("\t %d", need[i][j]); // printing need matrix
}
printf("\n");
}
p = 1; // used for terminating while loop
y = 0;
while (p != 0) {
for (i = 0; i < m; i++) {
z = 0;
for (j = 0; j < n; j++) {
if (need[i][j] <= av[j] &&
(need[i][0] != -1)) { // comparing need with available instance and
// checking if the process is done
// or not
z++; // counter if condition TRUE
}
}
if (z == n) { // if need<=available TRUE for all resources then condition
// is TRUE
for (k = 0; k < n; k++) {
av[k] += al[i][k]; // new work = work + allocated
}
printf("\n SS process %d", i); // Print the Process
need[i][0] = -1; // assign -1 if Process done
y++; // cont if process done
}
} // end for loop
if (y == m) { // if all done then
p = 0; // exit while loop
}
} // end while
printf("\n");
}
/*
After Input of all required values
OUTPUT RESULT
Alocation Values :
0 0 1 2
1 0 0 0
1 3 5 4
0 6 3 2
0 0 1 4
Max Values :
0 0 1 2
1 7 5 0
2 3 5 6
0 6 5 2
0 6 5 6
Need Values :
0 0 0 0
0 7 5 0
1 0 0 2
0 0 2 0
0 6 4 2
SS process 0
SS process 2
SS process 3
SS process 4
SS process 1
*/