-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
438 lines (349 loc) · 14.2 KB
/
main.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define _PHASE_COUNT 6
#define WORKERS_COUNT 1000
#define PAIR_SIZE 2
#define MAX_USING 6
#define NUZKY_TIME 100000
#define VRTACKA_TIME 200000
#define OHYBACKA_TIME 150000
#define SVARECKA_TIME 300000
#define LAKOVNA_TIME 400000
#define SROUBOVAK_TIME 250000
#define FREZA_TIME 500000
// Possible places & count of places
enum place {
NUZKY, VRTACKA, OHYBACKA, SVARECKA, LAKOVNA, SROUBOVAK, FREZA,
_PLACE_COUNT
};
// String value of possible places
const char *place_str[_PLACE_COUNT] = {
[NUZKY] = "nuzky",
[VRTACKA] = "vrtacka",
[OHYBACKA] = "ohybacka",
[SVARECKA] = "svarecka",
[LAKOVNA] = "lakovna",
[SROUBOVAK] = "sroubovak",
[FREZA] = "freza",
};
// Possible products
enum product {
A, B, C,
_PRODUCT_COUNT
};
// String value of possible products
const char *product_str[_PRODUCT_COUNT] = {
[A] = "A",
[B] = "B",
[C] = "C",
};
// Count of usage of current place
int using_count[_PLACE_COUNT] = {2,6,1,1,3,2,3};
// Returns PRODUCT(pair_i = 0) and PHASE(pair_i = 1) indexes in right order (latest phase in alphabet order)
// Usage of array: phases_index[worker.place][n < using_count[worker.place]][pair_i]
int phases_index[_PLACE_COUNT][MAX_USING][PAIR_SIZE] = {
{{1,1},{0,0},{-1,-1},{-1,-1},{-1,-1},{-1,-1}}, //NUZKY
{{0,4},{1,3},{2,3},{0,1},{2,1},{1,0}}, //VRTACKA
{{0,2},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}}, //OHYBACKA
{{0,3},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}}, // SVARECKA
{{0,5},{2,5},{1,4},{-1,-1},{-1,-1},{-1,-1}}, // LAKOVNA
{{1,5},{2,2},{-1,-1},{-1,-1},{-1,-1},{-1,-1}}, // SROUBOVAK
{{2,4},{1,2},{2,0},{-1,-1},{-1,-1},{-1,-1}} // FREZA
}; // (-1,-1) isn't pair
// get place what is in [PRODUCT] in [PHASE]
// A: 1:nuzky - 2:vrtacka - 3:ohybacka - 4:svarecka - 5:vrtacka - 6:lakovna
// B: 1:vrtacka - 2:nuzky - 3:freza - 4:vrtacka - 5:lakovna - 6:sroubovak
// C: 1:freza - 2:vrtacka - 3:sroubovak - 4:vrtacka - 5:freza - 6:lakovna
int phase2place[_PRODUCT_COUNT][_PLACE_COUNT] = {
{NUZKY,VRTACKA,OHYBACKA,SVARECKA,VRTACKA,LAKOVNA},
{VRTACKA,NUZKY,FREZA,VRTACKA,LAKOVNA,SROUBOVAK},
{FREZA,VRTACKA,SROUBOVAK,VRTACKA,FREZA,LAKOVNA}
};
// Work time in PLACE
const int work_time[_PLACE_COUNT] = {NUZKY_TIME,VRTACKA_TIME,OHYBACKA_TIME,SVARECKA_TIME,
LAKOVNA_TIME, SROUBOVAK_TIME, FREZA_TIME};
// find place/product index by its strign representation
int find_string_in_array(const char **array, int length, char *what){
for (int i = 0; i < length; i++)
if (strcmp(array[i], what) == 0)
return i;
return -1;
}
// count of empty places
int ready_places[_PLACE_COUNT];
//count of workers in current place
int ready_workers[_PLACE_COUNT];
// count of ready parts of PRODUCT in current PHASE
// parts[PRODUCT][PHASE]
int parts[_PRODUCT_COUNT][_PHASE_COUNT];
// Array with pthread_conds, conds[PLACE] is cond for worker which works at PLACE place
pthread_cond_t conds[_PLACE_COUNT];
// Mutex for accessing to all data
pthread_mutex_t mutex_factory;
// Structure represents worker
typedef struct Worker
{
char *name;
int worker_place;
int is_working; // 1 - is working, 0 - isn't working (end cmd), 2 - end of application
int *is_somebody_working;
pthread_t worker_thread;
} Worker;
// Initialisation of worker
Worker *init_worker(char *name,int worker_place, int *is_somebody_working){
Worker *worker = malloc(sizeof(Worker));
worker->worker_place = worker_place;
worker->name = strdup(name);
worker->is_working = 1;
worker->is_somebody_working = is_somebody_working;
return worker;
}
// Free alocated memory for worker
void free_worker(Worker *worker){
free(worker->name);
free(worker);
}
// Get worker index by name
int get_worker_index(Worker *workers[WORKERS_COUNT],int *workers_count,char *name){
for(int i = 0; i < *workers_count; i++){
if(strcmp(workers[i]->name,name) == 0){
return i;
}
}
return -1;
}
// remove worker with index worker_index from workers array and move last element to empty place
void remove_worker(Worker *workers[WORKERS_COUNT],Worker *workers_to_free[WORKERS_COUNT],
int *workers_count,int *workers_to_free_count,int worker_index){
*workers_count = *workers_count - 1;
workers_to_free[*workers_to_free_count] = workers[worker_index];
workers[worker_index] = workers[*workers_count];
*workers_to_free_count = *workers_to_free_count + 1;
}
// Return 1 if work can continue and 0 if it's finish (end of application)
int can_somebody_work(){
for(int i = 0; i < _PRODUCT_COUNT; i++){
for(int j = 0; j < _PHASE_COUNT; j++){
int cur_place = phase2place[i][j];
// if exists part and exist worker which can work with this part and exitst
// place where worker can work, then somebody can work.
if(parts[i][j] >= 1 && ready_workers[cur_place] >= 1 && ready_places[cur_place]){
return 1;
}
}
}
return 0;
}
// Routine for worker
void *factory(void *worker_void){
pthread_mutex_lock(&mutex_factory);
Worker *worker = (Worker*) worker_void;
int wp = worker->worker_place;
const char *wp_str = place_str[wp];
char *wn = worker->name;
int time_for_work = work_time[wp];
while (worker->is_working != 0)
{
int is_work = 1;
// 1. Cycle continue work if worker worked in previous iteration.
// 2. Check if place available (remove PLACE cmd just decrement value of ready_places[PLACE])
// and it can be below zero.
// 3. Check if worker can work
while (is_work && ready_places[wp] >= 1 && worker->is_working != 0)
{
// Take free place
ready_places[wp]--;
// is_work will remain zero if worker wouldn't work in this iteration
is_work = 0;
//Cycle for phases_index array (read information about that array)
for(int i = 0; i < using_count[wp]; i++){
int cur_product = phases_index[wp][i][0];
int cur_phase = phases_index[wp][i][1];
// Check if ready part in [PRODUCT] in [PHASE]
if(parts[cur_product][cur_phase] >= 1){
// Worker is working on this iteration, so is_work is 1
is_work = 1;
// Take ready part in [PRODUCT] in [PHASE] p
parts[cur_product][cur_phase]--;
// Mark that worker is working now
*worker->is_somebody_working = *worker->is_somebody_working + 1;
pthread_mutex_unlock(&mutex_factory);
// Print, that worker is working
flockfile(stdout);
printf("%s %s %d %c\n",wn,wp_str,cur_phase + 1,'A' + cur_product);
funlockfile(stdout);
// Main work
usleep(time_for_work);
// Print done product
if(cur_phase == _PHASE_COUNT-1){
flockfile(stdout);
printf("done %c\n",'A' + cur_product);
funlockfile(stdout);
}
pthread_mutex_lock(&mutex_factory);
// Mark that worker isn't working now
*worker->is_somebody_working = *worker->is_somebody_working - 1;
// save new part ot part array and signal to worker, which works on next phase
if(cur_phase != _PHASE_COUNT-1){
parts[cur_product][cur_phase+1]++;
int next_phase = phase2place[cur_product][cur_phase+1];
pthread_cond_signal(&conds[next_phase]);
}
// Exit thread in case of end cmd
if(worker->is_working == 0){
ready_places[wp]++;
pthread_mutex_unlock(&mutex_factory);
pthread_exit(NULL);
}
break;
}
}
// Return place for other workers
ready_places[wp]++;
}
// Check end of application
if(worker->is_working == 2 && can_somebody_work() == 0 && *worker->is_somebody_working == 0){
for(int i = 0; i < _PLACE_COUNT;i++){
pthread_cond_signal(&conds[i]);
}
break;
}
// Waiting for next signal
pthread_cond_wait(&conds[wp],&mutex_factory);
// Equivalent to:
// pthread_mutex_unlock(&mutex_factory);
// wait for signal on conds[wp]
// pthread_mutex_lock(&mutex_factory);
}
pthread_mutex_unlock(&mutex_factory);
pthread_exit(NULL);
}
int main(int argc, char **argv)
{
pthread_mutex_init(&mutex_factory,NULL);
for(int i = 0; i < _PLACE_COUNT; i++){
pthread_cond_init(&conds[i],NULL);
}
// array with workers
Worker *workers[WORKERS_COUNT];
int workers_count = 0;
Worker *workers_to_free[WORKERS_COUNT];
int workers_to_free_count = 0;
// worker increment this variable when he's working
int *is_somebody_working = malloc(sizeof(int));
*is_somebody_working = 0;
// Check input
while (1) {
char *line, *cmd, *arg1, *arg2, *arg3, *saveptr;
int s = scanf(" %m[^\n]", &line);
if (s == EOF)
break;
if (s == 0)
continue;
cmd = strtok_r(line, " ", &saveptr);
arg1 = strtok_r(NULL, " ", &saveptr);
arg2 = strtok_r(NULL, " ", &saveptr);
arg3 = strtok_r(NULL, " ", &saveptr);
if (strcmp(cmd, "start") == 0 && arg1 && arg2 && !arg3) {
pthread_mutex_lock(&mutex_factory);
int worker_place = find_string_in_array(place_str,_PLACE_COUNT,arg2);
if(worker_place == -1){
pthread_mutex_unlock(&mutex_factory);
free(line);
continue;
}
// Init new worker
workers[workers_count] = init_worker(arg1,worker_place,is_somebody_working);
workers_count++;
ready_workers[worker_place]++;
// start new thread for new worker
if(pthread_create(&(workers[workers_count-1]->worker_thread),NULL, &factory, workers[workers_count-1]) != 0){
return EXIT_FAILURE;
}
pthread_mutex_unlock(&mutex_factory);
} else if (strcmp(cmd, "make") == 0 && arg1 && !arg2) {
pthread_mutex_lock(&mutex_factory);
int product = find_string_in_array(product_str,_PRODUCT_COUNT,arg1);
if (product >= 0) {
// add the part to factory cycle
parts[product][0]++;
int first_place = phase2place[product][0];
// need to wakeup worker to start working if possible
pthread_cond_signal(&conds[first_place]);
pthread_mutex_unlock(&mutex_factory);
}
} else if (strcmp(cmd, "end") == 0 && arg1 && !arg2) {
/*
the worker has to finish their work first
you should not wait here for the worker to finish.
if the worker is waiting for work
you need to wakeup the worker
*/
pthread_mutex_lock(&mutex_factory);
int worker_index = get_worker_index(workers,&workers_count,arg1);
if(worker_index == -1){
free(line);
pthread_mutex_unlock(&mutex_factory);
continue;
}
// tell the worker to finish
workers[worker_index]->is_working = 0;
ready_workers[workers[worker_index]->worker_place]--;
// Pop worker from workers array and free it
remove_worker(workers,workers_to_free,&workers_count,&workers_to_free_count,worker_index);
// wakeup the workers which working in workers[worker_index]->worker_place
pthread_cond_broadcast(&conds[workers[worker_index]->worker_place]);
pthread_mutex_unlock(&mutex_factory);
} else if (strcmp(cmd, "add") == 0 && arg1 && !arg2) {
pthread_mutex_lock(&mutex_factory);
int place = find_string_in_array(place_str,_PLACE_COUNT,arg1);
if(place != -1){
// add new place
ready_places[place]++;
}
pthread_mutex_unlock(&mutex_factory);
// start working - wakeup worker
pthread_cond_signal(&conds[place]);
} else if (strcmp(cmd, "remove") == 0 && arg1 && !arg2) {
pthread_mutex_lock(&mutex_factory);
//get place index
int place = find_string_in_array(place_str,_PLACE_COUNT,arg1);
// remove place and worker will finish his work after it
ready_places[place]--;
pthread_mutex_unlock(&mutex_factory);
} else {
fprintf(stderr, "Invalid command: %s\n", line);
}
free(line);
}
pthread_mutex_lock(&mutex_factory);
// Set is_working variables of all workers to 2
for(int i = 0; i < workers_count; i++){
workers[i]->is_working = 2;
}
pthread_mutex_unlock(&mutex_factory);
// Wait for every worker to finish their work. Nobody should be able to continue.
for (int i = 0; i < workers_count; i++)
{
pthread_cond_broadcast(&conds[workers[i]->worker_place]);
pthread_join(workers[i]->worker_thread, NULL);
}
// Free all workers
for(int i = 0; i < workers_to_free_count; i++){
pthread_join(workers_to_free[i]->worker_thread, NULL);
free_worker(workers_to_free[i]);
}
for(int i = 0; i < workers_count; i++){
free_worker(workers[i]);
}
free(is_somebody_working);
// Destroy mutex and conds
pthread_mutex_destroy(&mutex_factory);
for(int i = 0; i < _PLACE_COUNT;i++){
pthread_cond_destroy(&conds[i]);
}
return EXIT_SUCCESS;
}