-
Notifications
You must be signed in to change notification settings - Fork 0
/
prod-cons.c
569 lines (512 loc) · 15.8 KB
/
prod-cons.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
* File : pc.c
*
* Title : Demo Producer/Consumer.
*
* Short : A solution to the producer consumer problem using
* pthreads.
*
* Long :
*
* Author : Andrae Muys
*
* Date : 18 September 1997
*
* Revised and tailroed by: Christina Koutsou
*/
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>
#include <stdbool.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#define QUEUESIZE 5;
#define LOOP 100000000
#define P 4
#define Q 4
#define NUM_TASKS 1 // number of timers
#define PERIOD 1 // 1 sec
#define MAX_YEARS_DELAY 584941 // Max value of years in the future the timer can be set to due to overflow
void *producer(void *args);
void *consumer(void *args);
pthread_cond_t *end;
bool exit_flag = false;
typedef struct
{
void *(*work)(void *);
void *arg;
struct timeval *start;
int id;
} workFunction;
typedef struct
{
__uint32_t Period; // period of task execution in usec
__uint32_t TasksToExecute; // number of tasks to be executed
__uint8_t StartDelay; // start executing tasks after a StartDelay delay
void *(*StartFcn)(void *); // initiate data to be used by TimerFcn
void *(*StopFcn)(__uint32_t id); // function to be executed after the last call of the TimerFcn (TasksToExecute==0)
void *(*TimerFcn)(void *); // function to be executed at the start of each period
void *arg;
void *(*ErrorFcn)(void *, void *); // function to be executed in case the queue is full
__uint32_t id;
struct timeval *add_queue; // pointer to save the timestamp of adding the object to the queue
struct timeval *del_queue; // pointer to save the timestamp of deleting the object from the queue
} Timer;
void *stop(__uint32_t id)
{
printf("End of Timer with id: %d\n", id);
return (NULL);
}
void *start(void *T)
{
Timer *timer = (Timer *)T;
usleep(1000000 * timer->StartDelay);
timer->TimerFcn(timer->arg);
timer->TasksToExecute--;
return (NULL);
}
void *startat(Timer *T, __uint16_t y, __uint8_t m, __uint8_t d, __uint8_t h, __uint8_t min, __uint8_t sec)
{
if (m > 12 || d > 31 || h > 24 || min > 60 || sec > 60)
{
printf("Not a valid timestamp\n");
return (NULL);
}
time_t currentTime = time(NULL);
// Convert the system time to a local time struct
struct tm *localTime = localtime(¤tTime);
// Extract the date components
int year = localTime->tm_year + 1900; // Year since 1900
int month = localTime->tm_mon + 1; // Month
int day = localTime->tm_mday; // Day of the month
// check if the timestamp has passed or is now
if (year < y || (year == y &&
(month < m || (month == m &&
(day < d || (day == d &&
(localTime->tm_hour < h || (localTime->tm_hour == h &&
(localTime->tm_min < min || (localTime->tm_min == min &&
localTime->tm_sec < sec))))))))))
{
if ((year - y) > MAX_YEARS_DELAY) // avoid overflow
{
printf("The timestamp is too far in the future\n");
return (NULL);
}
else
{
__uint64_t us_of_day = 24 * 3600 * (1e6);
__uint64_t wait = (year - y) * 365 * us_of_day;
wait += abs(month - m) * 30 * us_of_day;
wait += abs(day - d) * us_of_day;
wait += abs(localTime->tm_hour - h) * 3600 * (int)(1e6);
wait += abs(localTime->tm_min - min) * 60 * (int)(1e6);
wait += abs(localTime->tm_sec - sec) * (int)(1e6);
printf("wait for %ld us\n", wait);
usleep(wait);
T->TimerFcn(T->arg); // first call of the TimerFcn
T->TasksToExecute--;
}
}
else
{
printf("The timestamp has passed\n");
}
return (NULL);
}
typedef struct
{
int *numbers;
int size;
} find_primes_args;
bool is_prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i += 6)
{
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
void *find_primes(void *args)
{
find_primes_args fpa = *(find_primes_args *)args;
int count = 0;
int *prime_numbers = (int *)malloc(fpa.size * sizeof(int));
for (int i = 0; i < fpa.size; i++)
{
if (is_prime(fpa.numbers[i]))
{
prime_numbers[count] = fpa.numbers[i];
count++;
}
}
// printf("Prime numbers: ");
// for (int i = 0; i < count; i++) {
// printf("%d ", prime_numbers[i]);
// }
// printf("\n");
printf("Function execution\n");
free(prime_numbers);
return (NULL);
}
typedef struct
{
Timer *buf;
long head, tail;
int full, empty, size, num_tasks;
pthread_mutex_t **prod_mut, *queue_mut;
pthread_cond_t *notFull, *notEmpty;
} queue;
queue *queueInit(int size, __uint8_t num_tasks);
void queueDelete(queue *q);
void queueAdd(queue *q, Timer in);
void queueDel(queue *q, Timer *out);
// Arguments for the producer thread
typedef struct
{
queue *fifo;
Timer *T;
bool start_at;
__uint16_t y;
__uint8_t m, d, h, min, sec;
} Arguments;
void *errorFnc(void *q, void *id)
{
queue *fifo = (queue *)q;
__uint8_t *task = (__uint8_t *)id;
while (fifo->full) // `while` to ensure that a check after the signal happens as well
{
pthread_cond_wait(fifo->notFull, fifo->prod_mut[*task - 1]);
}
return (NULL);
}
int main(int argc, char *argv[])
{
// Initialize the arguments
int p = P, q = Q, queuesize = QUEUESIZE;
__uint8_t num_tasks = NUM_TASKS;
time_t currentTime = time(NULL);
struct tm *localTime = localtime(¤tTime);
__uint16_t year = localTime->tm_year + 1900;
__uint8_t month = localTime->tm_mon + 1, day = localTime->tm_mday,
hour = localTime->tm_hour, min = localTime->tm_min, sec = (localTime->tm_sec + 5) > 60 ? 2 : localTime->tm_sec + 5;
float period[3] = {PERIOD, PERIOD, PERIOD};
bool start_at = false; // used to choose a start function for the timer
if (argc > 1)
{
for (__uint8_t i = 1; i < argc; i++)
{
switch (argv[i][0])
{
case 'p':
p = atoi(argv[i] + 2); // the arguments is in the form of p=$number$
break;
case 'q':
q = atoi(argv[i] + 2);
break;
case 'n':
queuesize = atoi(argv[i] + 2);
break;
case 't':
period[num_tasks - 1] = atof(argv[i] + 2);
num_tasks++;
break;
case 'y':
year = atoi(argv[i] + 2);
start_at = true;
break;
case 'm':
month = atoi(argv[i] + 2);
start_at = true;
break;
case 'd':
day = atoi(argv[i] + 2);
start_at = true;
break;
case 'h':
hour = atoi(argv[i] + 2);
start_at = true;
break;
case 'i':
min = atoi(argv[i] + 2);
start_at = true;
break;
case 's':
sec = atoi(argv[i] + 2);
start_at = true;
break;
default:
printf("Wrong argument, using default feature\n");
}
}
}
num_tasks--; // reverse the last increment of num_tasks
pthread_t pro[p], con[q];
queue *fifo;
fifo = queueInit(queuesize, num_tasks);
static int numbers[] = {17, 23, 34, 47, 53, 67, 79, 81, 97};
static find_primes_args fpa;
fpa.numbers = numbers;
fpa.size = 9;
Timer T[num_tasks];
Arguments prod_args[num_tasks];
// Initiate Timer characteristics
for (__uint8_t i = 0; i < num_tasks; i++)
{
T[i].TimerFcn = find_primes;
T[i].arg = &fpa;
T[i].Period = 1000000 * period[i];
T[i].TasksToExecute = 11;
T[i].id = i + 1;
T[i].ErrorFcn = errorFnc;
if (start_at)
{
prod_args[i].start_at = true;
prod_args[i].y = year;
prod_args[i].m = month;
prod_args[i].d = day;
prod_args[i].h = hour;
prod_args[i].min = min;
prod_args[i].sec = sec;
}
else
{
T[i].StartFcn = start;
T[i].StartDelay = 1; // use delay only when time to start the timer is not specified
}
T[i].StopFcn = stop;
prod_args[i].fifo = fifo;
prod_args[i].T = &T[i];
}
int thread_chunk = p / num_tasks; // equal number of threads for each task
int limit = 0; // used to set the limit of threads for each task
if (fifo == NULL)
{
fprintf(stderr, "main: Queue Init failed.\n");
exit(1);
}
for (__uint8_t task = 0; task < num_tasks; task++)
{
if (task == (num_tasks - 1))
{
limit = limit + p - ((num_tasks - 1) * thread_chunk); // the last task gets the remaining threads in case the division is not exact
}
else
{
limit = (task + 1) * thread_chunk;
}
for (int i = task * thread_chunk; i < limit; ++i)
{
printf("Creating producer thread %d for task %d\n", i, task);
if (pthread_create(&pro[i], NULL, producer, &prod_args[task]) != 0)
{
fprintf(stderr, "Failed to create producer thread %d\n", i);
return 1;
}
}
}
for (int i = 0; i < q; ++i)
{
if (pthread_create(&con[i], NULL, consumer, fifo) != 0)
{
fprintf(stderr, "Failed to create consumer thread %d\n", i);
return 1;
}
}
for (int i = 0; i < p; ++i)
{
pthread_cond_broadcast(fifo->notFull); // broadcasting signals is used to wake any producer or consumer threads that are waiting for a condition that
// has been met due to a producer exiting
pthread_cond_broadcast(fifo->notEmpty);
pthread_join(pro[i], NULL);
}
printf("All producers finished\n");
exit_flag = true; // set the exit flag to true to signal the consumer threads that from now on no item will be added to the queue
for (int i = 0; i < q; ++i)
{
pthread_cond_broadcast(fifo->notEmpty); // broadcasting signals is used to wake any consumer threads that are waiting for a condition that
// has been met due to the producers or a consumer exiting
pthread_join(con[i], NULL);
}
printf("deleting fifo\n");
queueDelete(fifo);
return 0;
}
void *producer(void *args)
{
Arguments *prod_args = (Arguments *)args;
queue *fifo = prod_args->fifo;
Timer *T = prod_args->T;
__uint8_t total_tasks = T->TasksToExecute; // initialize timer executions/iterations
static struct timeval start; // initialize start time
gettimeofday(&start, NULL);
int i;
for (i = 0; i < LOOP; i++)
{
pthread_mutex_lock(fifo->prod_mut[T->id - 1]); // lock the mutex that corresponds to this producer thread,
// so not any other producer thread of this timer can access the queue
if (T->TasksToExecute == total_tasks) // checks if the timer has not yet started
{
printf("Start function of timer %d\n", T->id);
if (prod_args->start_at)
{
startat(T, prod_args->y, prod_args->m, prod_args->d, prod_args->h, prod_args->min, prod_args->sec);
}
else
{
T->StartFcn(T);
}
gettimeofday(&start, NULL); // re-calculate the current time since the start functions induce a delay
}
if (fifo->full)
{
printf("producer: queue FULL. \n");
T->ErrorFcn(fifo, &T->id); // wait for signal from consumer
}
if (T->TasksToExecute <= 0) // this timer is finished so this thread serves no purpose anymore
{
printf("producer exits for id %d\n", T->id);
pthread_mutex_unlock(fifo->prod_mut[T->id - 1]);
return (NULL);
}
T->TasksToExecute--; // update the number of tasks to be executed
printf("TasksToExecute of Timer with id %d: %d\n", T->id, (T->TasksToExecute + 1));
struct timeval previous = start; // save the previous time this producer thread was executed
gettimeofday(&start, NULL);
if (T->TasksToExecute != (total_tasks - 1)) // avoid adding the first item to the queue since it is already executed by the start function
{
pthread_mutex_lock(fifo->queue_mut); // this mutex is used among all producers and consumers to ensure that each operation on the queue is atomic
T->add_queue = &start; // note the time the item is passed on the queue to calculate afterwards the time it spents in the queue
queueAdd(fifo, *T);
pthread_mutex_unlock(fifo->queue_mut);
pthread_cond_broadcast(fifo->notEmpty);
}
long int drift = (1000000 * (start.tv_sec - previous.tv_sec) + (start.tv_usec - previous.tv_usec)) - T->Period; // calculate and fix the drift of the timer due to the mutex locks
if (drift < 0)
{
drift = 0; // the drift was fixed previously and the producer was called earlier
}
printf("Drift for Timer %d: %ld us\n", T->id, drift);
long int sleep = T->Period - drift;
if (sleep > 0)
usleep(sleep);
else
usleep(0); // add the delay before the next execution of the timer
pthread_mutex_unlock(fifo->prod_mut[T->id - 1]); // let another thread of this timer access the queue after a period passes
}
return (NULL);
}
void *consumer(void *q)
{
queue *fifo;
Timer d;
int i;
fifo = (queue *)q;
for (i = 0;; i++)
{
pthread_mutex_lock(fifo->queue_mut);
while (fifo->empty)
{
if (exit_flag)
{
printf("consumer exits\n"); // the queue is empty and no more items will be added to it from now on
pthread_mutex_unlock(fifo->queue_mut);
return (NULL);
}
printf("consumer: queue EMPTY.\n");
pthread_cond_wait(fifo->notEmpty, fifo->queue_mut);
}
queueDel(fifo, &d);
pthread_mutex_unlock(fifo->queue_mut);
pthread_cond_broadcast(fifo->notFull);
// printf ("consumer: received %d.\n", i++);
struct timeval end;
gettimeofday(&end, NULL);
time_t interval = 1000000 * (end.tv_sec - d.add_queue->tv_sec) + end.tv_usec - d.add_queue->tv_usec; // calculate the time the item spents in the queue
// printf("Time interval: %ld us\n", interval);
fflush(stdout);
d.TimerFcn(d.arg);
if (d.TasksToExecute == 0) // the queue is a FIFO queue so the last item to be added signifies that no tasks are left
{
d.StopFcn(d.id);
}
}
return (NULL);
}
queue *queueInit(int size, __uint8_t num_tasks)
{
queue *q;
q = (queue *)malloc(sizeof(queue));
if (q == NULL)
return (NULL);
q->buf = (Timer *)malloc(size * sizeof(Timer));
q->empty = 1;
q->full = 0;
q->head = 0;
q->tail = 0;
q->size = size;
q->prod_mut = (pthread_mutex_t **)malloc(num_tasks * sizeof(pthread_mutex_t *));
// Allocate and init each mutex for each timer
for (__uint8_t task = 0; task < num_tasks; task++)
{
q->prod_mut[task] = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(q->prod_mut[task], NULL);
}
q->queue_mut = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(q->queue_mut, NULL);
q->notFull = (pthread_cond_t *)malloc(sizeof(pthread_cond_t));
pthread_cond_init(q->notFull, NULL);
q->notEmpty = (pthread_cond_t *)malloc(sizeof(pthread_cond_t));
pthread_cond_init(q->notEmpty, NULL);
return (q);
}
void queueDelete(queue *q)
{
// Destroy and free the space of each mutex for each timer
for (__uint8_t task = 0; task < q->num_tasks; task++)
{
pthread_mutex_destroy(q->prod_mut[task]);
free(q->prod_mut[task]);
}
free(q->prod_mut);
pthread_mutex_destroy(q->queue_mut);
free(q->queue_mut);
pthread_cond_destroy(q->notFull);
free(q->notFull);
pthread_cond_destroy(q->notEmpty);
free(q->notEmpty);
free(q->buf);
free(q);
}
void queueAdd(queue *q, Timer in)
{
q->buf[q->tail] = in;
q->tail++; // atomic operation
if (q->tail == q->size)
q->tail = 0;
if (q->tail == q->head)
q->full = 1;
q->empty = 0;
return;
}
void queueDel(queue *q, Timer *out)
{
*out = q->buf[q->head];
q->head++;
if (q->head == q->size)
q->head = 0;
if (q->head == q->tail)
q->empty = 1;
q->full = 0;
return;
}