|
@@ -1,42 +1,45 @@
|
|
| 1 |
// Copyright 2021 Jeisson Hidalgo-Cespedes <jeisson.hidalgo@ucr.ac.cr> CC-BY-4
|
| 2 |
|
| 3 |
#ifndef COMMON_H
|
| 4 |
#define COMMON_H
|
| 5 |
|
| 6 |
#include <semaphore.h>
|
| 7 |
#include <stdlib.h>
|
| 8 |
#include <unistd.h>
|
| 9 |
|
| 10 |
#include "queue.h"
|
| 11 |
|
| 12 |
enum {
|
| 13 |
ERR_NOMEM_SHARED = EXIT_FAILURE + 1,
|
| 14 |
ERR_NOMEM_BUFFER,
|
| 15 |
ERR_NO_ARGS,
|
| 16 |
ERR_UNIT_COUNT,
|
| 17 |
ERR_PRODUCER_COUNT,
|
| 18 |
ERR_CONSUMER_COUNT,
|
| 19 |
ERR_MIN_PROD_DELAY,
|
| 20 |
ERR_MAX_PROD_DELAY,
|
| 21 |
ERR_MIN_CONS_DELAY,
|
| 22 |
ERR_MAX_CONS_DELAY,
|
| 23 |
ERR_CREATE_THREAD,
|
| 24 |
};
|
| 25 |
|
| 26 |
typedef struct simulation {
|
| 27 |
size_t unit_count;
|
| 28 |
size_t producer_count;
|
| 29 |
size_t consumer_count;
|
| 30 |
useconds_t producer_min_delay;
|
| 31 |
useconds_t producer_max_delay;
|
| 32 |
useconds_t consumer_min_delay;
|
| 33 |
useconds_t consumer_max_delay;
|
| 34 |
|
| 35 |
queue_t queue;
|
|
|
|
| 36 |
size_t next_unit;
|
|
|
|
|
|
|
| 37 |
size_t consumed_count;
|
| 38 |
} simulation_t;
|
| 39 |
|
| 40 |
useconds_t random_between(useconds_t min, useconds_t max);
|
| 41 |
|
| 42 |
#endif // COMMON_H
|
| 1 |
// Copyright 2021 Jeisson Hidalgo-Cespedes <jeisson.hidalgo@ucr.ac.cr> CC-BY-4
|
| 2 |
|
| 3 |
#ifndef COMMON_H
|
| 4 |
#define COMMON_H
|
| 5 |
|
| 6 |
#include <semaphore.h>
|
| 7 |
#include <stdlib.h>
|
| 8 |
#include <unistd.h>
|
| 9 |
|
| 10 |
#include "queue.h"
|
| 11 |
|
| 12 |
enum {
|
| 13 |
ERR_NOMEM_SHARED = EXIT_FAILURE + 1,
|
| 14 |
ERR_NOMEM_BUFFER,
|
| 15 |
ERR_NO_ARGS,
|
| 16 |
ERR_UNIT_COUNT,
|
| 17 |
ERR_PRODUCER_COUNT,
|
| 18 |
ERR_CONSUMER_COUNT,
|
| 19 |
ERR_MIN_PROD_DELAY,
|
| 20 |
ERR_MAX_PROD_DELAY,
|
| 21 |
ERR_MIN_CONS_DELAY,
|
| 22 |
ERR_MAX_CONS_DELAY,
|
| 23 |
ERR_CREATE_THREAD,
|
| 24 |
};
|
| 25 |
|
| 26 |
typedef struct simulation {
|
| 27 |
size_t unit_count;
|
| 28 |
size_t producer_count;
|
| 29 |
size_t consumer_count;
|
| 30 |
useconds_t producer_min_delay;
|
| 31 |
useconds_t producer_max_delay;
|
| 32 |
useconds_t consumer_min_delay;
|
| 33 |
useconds_t consumer_max_delay;
|
| 34 |
|
| 35 |
queue_t queue;
|
| 36 |
+
pthread_mutex_t can_access_next_unit;
|
| 37 |
size_t next_unit;
|
| 38 |
+
sem_t can_consume;
|
| 39 |
+
pthread_mutex_t can_access_consumed_count;
|
| 40 |
size_t consumed_count;
|
| 41 |
} simulation_t;
|
| 42 |
|
| 43 |
useconds_t random_between(useconds_t min, useconds_t max);
|
| 44 |
|
| 45 |
#endif // COMMON_H
|