-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbankerProblem.c
270 lines (232 loc) · 7.88 KB
/
bankerProblem.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define NUMBER_OF_CUSTOMERS 5
#define NUMBER_OF_RESOURCES 4
const int P = 5;
const int R = 4;
int processes[] = {0, 1, 2, 3, 4};
/* the available amount of each resource */
int available[NUMBER_OF_RESOURCES];
/*the maximum demand of each customer */
int maximum[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
/* the amount currently allocated to each customer */
int allocation[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
/* the remaining need of each customer */
int need[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
bool request_resources(int customer_num, int request[]);
void release_resources(int customer_num, int release[]);
/* resource checking*/
int request[NUMBER_OF_RESOURCES];
int release[NUMBER_OF_RESOURCES];
pthread_mutex_t mutex;
bool isSafe(int processes[], int avail[], int maxm[][R],
int allot[][R]);
bool request_resources(int customer_num, int request[]) {
printf("P%d requested: %d, %d, %d, %d\n", customer_num, request[0], request[1], request[2], request[3]);
printf("Available: ");
for(int i = 0; i < NUMBER_OF_RESOURCES; i++){
printf("%d ",available[i]);
}
for(int i = 0; i < NUMBER_OF_RESOURCES; i++){
if(request[i] <= need[customer_num][i]){
if(request[i] > available[i]){
printf("Unsafe request\n");
return false;
}
else{
for(int k = 0; k < NUMBER_OF_RESOURCES; k++){
allocation[customer_num][k] += request[k];
available[k] -= request[k];
need[customer_num][k] -= request[k];
}
if( isSafe(processes, available, maximum, allocation) ){
printf("Safe request, resources given\n");
printf("Available: \n");
for(int i = 0; i < NUMBER_OF_RESOURCES; i++){
printf("%d ",available[i]);
}
return true;
}
else{
printf("Unsafe Request 2\n");
for(int k = 0; k < NUMBER_OF_RESOURCES; k++){
available[k] += request[k];
need[customer_num][k] += request[k];
allocation[customer_num][k] -= request[k];
}
return false;
}
}
} else{
return false;
}
}
return true;
}
void calculateNeed(int need[P][R], int maxm[P][R],
int allot[P][R]) //copy pasted to make the isSafe functon work
{
// Calculating Need of each P
for (int i = 0 ; i < P ; i++)
for (int j = 0 ; j < R ; j++)
// Need of instance = maxm instance -
// allocated instance
need[i][j] = maxm[i][j] - allot[i][j];
}
bool isSafe(int processes[], int avail[], int maxm[][R],
int allot[][R]) //copy pasted and edited from tutorial for java/c++ because all my implementations wouldnt work... this doesnt work properly either
{
int need[P][R];
// Function to calculate need matrix
calculateNeed(need, maxm, allot);
int finish[P];
// Mark all processes as infinish
for(int i = 0; i < P; i++)
{
finish[P] = 0;
}
// To store safe sequence
int safeSeq[P];
// Make a copy of available resources
int work[R];
for (int i = 0; i < R ; i++)
work[i] = avail[i];
// While all processes are not finished
// or system is not in safe state.
int count = 0;
while (count < P)
{
// Find a process which is not finish and
// whose needs can be satisfied with current
// work[] resources.
bool found = false;
for (int p = 0; p < P; p++)
{
// First check if a process is finished,
// if no, go for next condition
if (finish[p] == 0)
{
// Check if for all resources of
// current P need is less
// than work
int j;
for (j = 0; j < R; j++)
if (need[p][j] > work[j])
break;
// If all needs of p were satisfied.
if (j == R)
{
// Add the allocated resources of
// current P to the available/work
// resources i.e.free the resources
for (int k = 0 ; k < R ; k++)
work[k] += allot[p][k];
// Add this process to safe sequence.
safeSeq[count++] = p;
// Mark this p as finished
finish[p] = 1;
found = true;
}
}
}
// If we could not find a next process in safe
// sequence.
if (found == false)
{
printf( "System is not in safe state\n");
return false;
}
}
// If system is in safe state then
// safe sequence will be as below
printf("System is in safe state.\nSafe sequence is: ");
// for (int i = 0; i < P ; i++)
return true;
}
void release_resources(int customer_num, int release[]) {
for(int i=0; i<NUMBER_OF_RESOURCES; i++) {
available[i] = available[i] - request[i];
allocation[customer_num][i] = allocation[customer_num][i] + request[i];
need[customer_num][i] = need[customer_num][i] - request[i];
}
}
void *requestThread(void *params) {
pthread_mutex_lock(&mutex);
request_resources(params, request);
pthread_mutex_unlock(&mutex);
pthread_exit(0);
}
void *releaseThread(void *params) {
pthread_mutex_lock(&mutex);
release_resources(params, request);
pthread_mutex_unlock(&mutex);
pthread_exit(0);
}
void printArrays(){
printf("Max Array:\n");
for(int i = 0; i < NUMBER_OF_CUSTOMERS; i++)
{
printf("%d, %d, %d, %d\n", maximum[i][0], maximum[i][1], maximum[i][2], maximum[i][3]);
}
printf("Available Array:\n");
for(int i = 0; i < NUMBER_OF_RESOURCES; i++)
{
printf("%d ", available[i]);
}
printf("\nAlocation Array:\n");
for(int i = 0; i < NUMBER_OF_CUSTOMERS; i++)
{
printf("%d, %d, %d, %d\n", allocation[i][0], allocation[i][1], allocation[i][2], allocation[i][3]);
}
printf("Need Array:\n");
for(int i = 0; i < NUMBER_OF_CUSTOMERS; i++)
{
printf("%d, %d, %d, %d\n", need[i][0], need[i][1], need[i][2], need[i][3]);
}
}
int main(int argc, char* argv[]) {
char command[2];
int customerNum = 0, resource1 = 0, resource2 = 0, resource3 = 0, resource4 = 0;
FILE *fp;
fp = fopen("bankerInput.txt", "r");
for(int i = 0; i < NUMBER_OF_CUSTOMERS; i++)
{
for(int k = 0; k < NUMBER_OF_RESOURCES; k++)
{
fscanf(fp, "%d", &maximum[i][k]);
available[k] = atoi(argv[k + 1]);
need[i][k] = maximum[i][k];
}
fgetc(fp);
}
fclose(fp);
pthread_t Customer[NUMBER_OF_CUSTOMERS];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_mutex_init(&mutex, NULL);
while(strcmp(command, "quit") != 0)
{
printf("Type 'quit' to quit\n");
scanf("%s", &command);
if(strcmp(command, "*") == 0)
{
printArrays();
}
else if(strcmp(command, "RQ") == 0)
{
scanf("%d %d %d %d %d",&customerNum, &request[0], &request[1], &request[2], &request[3]);
printf("request\n");
pthread_create(&Customer[customerNum], &attr, requestThread, customerNum);
}
else if(strcmp(command, "RL") == 0)
{
scanf("%d %d %d %d %d",&customerNum, &request[0], &request[1], &request[2], &request[3]);
printf("release\n");
pthread_create(&Customer[customerNum], &attr, releaseThread, customerNum);
}
}
pthread_mutex_destroy(&mutex);
}