-
Notifications
You must be signed in to change notification settings - Fork 0
/
coord_commands.c
executable file
·380 lines (326 loc) · 12.2 KB
/
coord_commands.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "coord_commands.h"
int submit(char* operation) {
/* CREATE A NEW POOL IF NEEDED */
if(need_new_pool) create_pool();
/* SEND THE OPERATION TO THE MOST RECENTLY CREATED POOL */
send_pool(operation);
num_of_jobs++; // we have one more submitted job, in total
pool_arr[num_of_pools-1].num_of_jobs++;
// check whether the next time we will submit a job we are going to need a new pool
if(pool_arr[num_of_pools-1].num_of_jobs == jobs_pool) need_new_pool = 1;
else need_new_pool = 0;
return 0;
}
int status(char* operation) {
char *tok, *temp_op = NULL;
temp_op = malloc(strlen(operation)+1);
strcpy(temp_op, operation); // so that we don't lose operation while tokenizing
tok = strtok(temp_op, " "); // take "status"
tok = strtok(NULL, "\n"); // take <JobID> as a string
int jobid = atoi(tok), pool = ceil((float)jobid/(float)jobs_pool);
if(jobid > num_of_jobs) { // recognise bad JobID
strcpy(msgbuf, "JobID not found. Please, try again.");
send_console();
return 0; // then return
}
// printf("coord: looking for status of JobID = %d in Pool no.%d\n", jobid, pool);
if (pool_arr[pool-1].active) { // if pool is active, then pass the question to the pool
// printf("coord: sending pool: %s\n", operation);
send_pool(operation);
} else { // if pool is not active, then the job must have finished
/* formulate the response to the question */
char response[40];
strcpy(response, "JobID ");
strcat(response, tok);
strcat(response, " Status: Finished");
strcpy(msgbuf, response);
send_console(); // send the response to the console
}
free(temp_op);
return 0;
}
int status_all(char* operation) {
int pool_in_fd, pool_out_fd, i;
char *op = NULL;
op = malloc(strlen(operation)+1);
strcpy(op, operation); // keep original operation
/* tell console to wait for a batch of answers */
strcpy(msgbuf, "wait-batch");
send_console();
for(i = 0; i < num_of_pools; i++) {
check_pools(); // before moving forward, check the pools one more time
if(pool_arr[i].active) {
// printf("coord: sending %s op to pool no.%d\n", op, i);
/* open pool_in fifo */
while((pool_in_fd = open(pool_arr[i].pool_in, O_WRONLY)) < 0);
/* send operation to the pool */
strcpy(pool_msgbuf, op);
while(write(pool_in_fd, pool_msgbuf, MSGSIZE) < 0);
/* open pool_out fifo */
while((pool_out_fd = open(pool_arr[i].pool_out, O_RDONLY)) < 0);
int done = 0;
while(!done) {
/* wait for the pool's response */
while(read(pool_out_fd, pool_msgbuf, MSGSIZE) < 0);
if(strcmp(pool_msgbuf, "-") != 0 && strcmp(pool_msgbuf, "pool-batch-over")){
// printf("coord: status_all: pool no.%d says: %s\n", pool_arr[i].id, pool_msgbuf);
strcpy(msgbuf, pool_msgbuf);
send_console();
} else if(!strcmp(pool_msgbuf, "pool-batch-over")) done = 1;
strcpy(pool_msgbuf, "-");
}
}
}
free(op);
/* tell console that the batch of answers is over, so it can move on */
strcpy(msgbuf, "batch-over");
send_console();
return 0;
}
int show_active(char* operation) {
int pool_in_fd, pool_out_fd, i, job_index;
char *op = NULL;
op = malloc(strlen(operation)+1);
strcpy(op, operation); // keep original operation
/* tell console to wait for a batch of answers */
strcpy(msgbuf, "wait-batch");
send_console();
strcpy(msgbuf, "Active jobs:");
send_console();
job_index = 0;
for(i = 0; i < num_of_pools; i++) {
check_pools(); // before moving forward, check the pools one more time
if(pool_arr[i].active) { // a pool that is not active can't have active jobs
// printf("coord: sending %s op to pool no.%d\n", op, i);
/* open pool_in fifo */
while((pool_in_fd = open(pool_arr[i].pool_in, O_WRONLY)) < 0);
/* send operation to the pool */
strcpy(pool_msgbuf, op);
while(write(pool_in_fd, pool_msgbuf, MSGSIZE) < 0);
/* open pool_out fifo */
while((pool_out_fd = open(pool_arr[i].pool_out, O_RDONLY)) < 0);
int done = 0;
while(!done) {
/* wait for the pool's response */
while(read(pool_out_fd, pool_msgbuf, MSGSIZE) < 0);
if(strcmp(pool_msgbuf, "-") != 0 && strcmp(pool_msgbuf, "pool-batch-over")){
// printf("coord: status_all: pool no.%d says: %s\n", pool_arr[i].id, pool_msgbuf);
char response[40], index[10];
job_index++; // one more active job
sprintf(index, "%d. ", job_index);
strcpy(response, index);
strcat(response, pool_msgbuf);
strcpy(msgbuf, response);
send_console();
} else if(!strcmp(pool_msgbuf, "pool-batch-over")) done = 1;
strcpy(pool_msgbuf, "-");
}
}
}
free(op);
/* tell console that the batch of answers is over, so it can move on */
strcpy(msgbuf, "batch-over");
send_console();
return 0;
}
int show_pools(char* operation) {
int pool_in_fd, pool_out_fd, i, pool_index;
char *op = NULL;
op = malloc(strlen(operation)+1);
strcpy(op, operation); // keep original operation
/* tell console to wait for a batch of answers */
strcpy(msgbuf, "wait-batch");
send_console();
strcpy(msgbuf, "Pool & NumOfJobs:");
send_console();
pool_index = 0;
for(i = 0; i < num_of_pools; i++) {
check_pools(); // before moving forward, check the pools one more time
if(pool_arr[i].active) { // a pool that is not active can't have active jobs
// printf("coord: sending %s op to pool no.%d\n", op, i);
/* open pool_in fifo */
while((pool_in_fd = open(pool_arr[i].pool_in, O_WRONLY)) < 0);
/* send operation to the pool */
strcpy(pool_msgbuf, op);
while(write(pool_in_fd, pool_msgbuf, MSGSIZE) < 0);
/* open pool_out fifo */
while((pool_out_fd = open(pool_arr[i].pool_out, O_RDONLY)) < 0);
/* wait for the pool's response */
while(read(pool_out_fd, pool_msgbuf, MSGSIZE) < 0);
if(strcmp(pool_msgbuf, "-") != 0){
// printf("coord: status_all: pool no.%d says: %s\n", pool_arr[i].id, pool_msgbuf);
if(strcmp(pool_msgbuf, "0") != 0) { // if pool has jobs running
char response[40], pool_pid[10], index[10];
pool_index++; // one more active job
sprintf(index, "%d. ", pool_index);
strcpy(response, index);
sprintf(pool_pid, "%d ", pool_arr[i].pid);
strcat(response, pool_pid);
strcat(response, pool_msgbuf);
strcpy(msgbuf, response);
send_console();
}
}
strcpy(pool_msgbuf, "-");
}
}
free(op);
/* tell console that the batch of answers is over, so it can move on */
strcpy(msgbuf, "batch-over");
send_console();
return 0;
}
int show_finished(char* operation) {
int pool_in_fd, pool_out_fd, i, job_index;
char *op = NULL;
op = malloc(strlen(operation)+1);
strcpy(op, operation); // keep original operation
/* tell console to wait for a batch of answers */
strcpy(msgbuf, "wait-batch");
send_console();
strcpy(msgbuf, "Finished jobs:");
send_console();
job_index = 0;
for(i = 0; i < num_of_pools; i++) {
check_pools(); // before moving forward, check the pools one more time
if(pool_arr[i].active) { // a pool that is not active can't have active jobs
// printf("coord: sending %s op to pool no.%d\n", op, i);
/* open pool_in fifo */
while((pool_in_fd = open(pool_arr[i].pool_in, O_WRONLY)) < 0);
/* send operation to the pool */
strcpy(pool_msgbuf, op);
while(write(pool_in_fd, pool_msgbuf, MSGSIZE) < 0);
/* open pool_out fifo */
while((pool_out_fd = open(pool_arr[i].pool_out, O_RDONLY)) < 0);
int done = 0;
while(!done) {
/* wait for the pool's response */
while(read(pool_out_fd, pool_msgbuf, MSGSIZE) < 0);
if(strcmp(pool_msgbuf, "-") != 0 && strcmp(pool_msgbuf, "pool-batch-over")){
// printf("coord: status_all: pool no.%d says: %s\n", pool_arr[i].id, pool_msgbuf);
char response[40], index[10];
job_index++; // one more active job
sprintf(index, "%d. ", job_index);
strcpy(response, index);
strcat(response, pool_msgbuf);
strcpy(msgbuf, response);
send_console();
} else if(!strcmp(pool_msgbuf, "pool-batch-over")) done = 1;
strcpy(pool_msgbuf, "-");
}
} else {
int j;
for(j = 0; j < jobs_pool; j++) {
char response[40], num[10];
job_index++; // one more active job
sprintf(num, "%d. ", job_index);
strcpy(response, num);
strcat(response, "JobID ");
sprintf(num, "%d", pool_arr[i].id+j);
strcat(response, num);
strcpy(msgbuf, response);
send_console();
}
}
}
free(op);
/* tell console that the batch of answers is over, so it can move on */
strcpy(msgbuf, "batch-over");
send_console();
return 0;
}
int suspend(char* operation) {
char *tok, *temp_op = NULL;
temp_op = malloc(strlen(operation)+1);
strcpy(temp_op, operation); // so that we don't lose operation while tokenizing
tok = strtok(temp_op, " "); // take "suspend"
tok = strtok(NULL, "\n"); // take <JobID> as a string
int jobid = atoi(tok), pool = ceil((float)jobid/(float)jobs_pool);
if(jobid > num_of_jobs) { // recognise bad JobID
strcpy(msgbuf, "JobID not found. Please, try again.");
send_console();
return 0; // then return
}
check_pools(); // before moving forward, check the pools one more time
// printf("coord: looking to suspend JobID = %d in Pool no.%d\n", jobid, pool);
if (pool_arr[pool-1].active) { // if pool is active, then pass the question to the pool
// printf("coord: sending pool: %s\n", operation);
send_pool(operation);
} else { // if pool is not active, then the job must have already finished
/* formulate the response to the question */
char response[40];
strcpy(response, "JobID ");
strcat(response, tok);
strcat(response, " has already finished");
strcpy(msgbuf, response);
send_console(); // send the response to the console
}
free(temp_op);
return 0;
}
int resume(char* operation) {
char *tok, *temp_op = NULL;
temp_op = malloc(strlen(operation)+1);
strcpy(temp_op, operation); // so that we don't lose operation while tokenizing
tok = strtok(temp_op, " "); // take "suspend"
tok = strtok(NULL, "\n"); // take <JobID> as a string
int jobid = atoi(tok), pool = ceil((float)jobid/(float)jobs_pool);
if(jobid > num_of_jobs) { // recognise bad JobID
strcpy(msgbuf, "JobID not found. Please, try again.");
send_console();
return 0; // then return
}
check_pools(); // before moving forward, check the pools one more time
// printf("coord: looking to resume JobID = %d in Pool no.%d\n", jobid, pool);
if (pool_arr[pool-1].active) { // if pool is active, then pass the question to the pool
// printf("coord: sending pool: %s\n", operation);
send_pool(operation);
} else { // if pool is not active, then the job must have already finished
/* formulate the response to the question */
char response[40];
strcpy(response, "JobID ");
strcat(response, tok);
strcat(response, " has already finished");
strcpy(msgbuf, response);
send_console(); // send the response to the console
}
free(temp_op);
return 0;
}
int shutdown(char* operation) {
char *op = NULL;
op = malloc(strlen(operation)+1);
strcpy(op, operation); // keep original operation
/* tell console to wait for a batch of answers */
strcpy(msgbuf, "shutting-down");
send_console();
int i, still_in_progress = 0, status; //, in_prog;
for(i = 0; i < num_of_pools; i++) { // go through all the pools
check_pools(); // before moving forward, check the pools one more time
if(pool_arr[i].active) { // there is no point to terminate an already terminated pool
if(kill(pool_arr[i].pid, SIGTERM) < 0) { // send termination signal
perror("coord: kill with SIGTERM problem\n");
exit(EXIT_FAILURE);
}
waitpid(pool_arr[i].pid, &status, 0); // wait for child-pool to exit
pool_arr[i].active = 0;
if(WIFEXITED(status)) pool_arr[i].exit_status = WEXITSTATUS(status);
active_pools--; // one less active pool
}
}
// each pool's exit status represents how many jobs were still in progress by the time it was asked to shut down
for(i = 0; i < num_of_pools; i++) still_in_progress += pool_arr[i].exit_status;
/* formulate the response to the console */
char response[60], num[10];
strcpy(response, "Served ");
sprintf(num, "%d", num_of_jobs);
strcat(response, num);
strcat(response, " jobs, ");
sprintf(num, "%d", still_in_progress);
strcat(response, num);
strcat(response, " were still in progress");
strcpy(msgbuf, response);
send_console(); // send the response to the console
free(op);
return 1;
}