X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/75fbadefcaf2c0e4311ae3204a7bd3e30e256eb7..244ffce80a9e2390e24b9788114de037e4ccc0ae:/examples/sthread/pthread-producer-consumer.c diff --git a/examples/sthread/pthread-producer-consumer.c b/examples/sthread/pthread-producer-consumer.c index bc0755036f..fe3fe5d960 100644 --- a/examples/sthread/pthread-producer-consumer.c +++ b/examples/sthread/pthread-producer-consumer.c @@ -10,18 +10,19 @@ #include #include #include +#include -#define AmountProduced 3 /* Amount of items produced by a producer */ -#define AmountConsumed 3 /* Amount of items consumed by a consumer */ -#define ProducerCount 2 /* Amount of producer threads*/ -#define ConsumerCount 2 /* Amount of consumer threads*/ -#define BufferSize 4 /* Size of the buffer */ +int AmountProduced = 3; /* Amount of items produced by a producer */ +int AmountConsumed = 3; /* Amount of items consumed by a consumer */ +int ProducerCount = 2; /* Amount of producer threads*/ +int ConsumerCount = 2; /* Amount of consumer threads*/ +int BufferSize = 4; /* Size of the buffer */ sem_t empty; sem_t full; int in = 0; int out = 0; -int buffer[BufferSize]; +int* buffer; pthread_mutex_t mutex; int do_output = 1; @@ -44,9 +45,10 @@ static void* consumer(void* id) for (int i = 0; i < AmountConsumed; i++) { sem_wait(&full); pthread_mutex_lock(&mutex); - int item = buffer[out]; - if (do_output) + if (do_output) { + int item = buffer[out]; fprintf(stderr, "Consumer %d: Remove Item %d from %d\n", *((int*)id), item, out); + } out = (out + 1) % BufferSize; pthread_mutex_unlock(&mutex); sem_post(&empty); @@ -56,19 +58,44 @@ static void* consumer(void* id) int main(int argc, char** argv) { - if (argc == 2 && strcmp(argv[1], "-q") == 0) - do_output = 0; - pthread_t pro[2], con[2]; + int opt; + while ((opt = getopt(argc, argv, "c:C:p:P:q")) != -1) { + switch (opt) { + case 'q': + do_output = 0; + break; + case 'c': + AmountConsumed = atoi(optarg); + break; + case 'C': + ConsumerCount = atoi(optarg); + break; + case 'p': + AmountProduced = atoi(optarg); + break; + case 'P': + ProducerCount = atoi(optarg); + break; + default: /* '?' */ + printf("unknown option: %c\n", optopt); + break; + } + } + pthread_t* pro = malloc(ProducerCount * sizeof(pthread_t)); + pthread_t* con = malloc(ConsumerCount * sizeof(pthread_t)); + buffer = malloc(sizeof(int) * BufferSize); pthread_mutex_init(&mutex, NULL); sem_init(&empty, 0, BufferSize); sem_init(&full, 0, 0); - int ids[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // The identity of each thread (for debug messages) + int* ids = malloc(sizeof(int) * (ProducerCount + ConsumerCount)); + for (int i = 0; i < ProducerCount + ConsumerCount; i++) + ids[i] = i + 1; // The identity of each thread (for debug messages) for (int i = 0; i < ProducerCount; i++) - pthread_create(&pro[i], NULL, (void*)producer, (void*)&ids[i]); + pthread_create(&pro[i], NULL, producer, &ids[i]); for (int i = 0; i < ConsumerCount; i++) - pthread_create(&con[i], NULL, (void*)consumer, (void*)&ids[i]); + pthread_create(&con[i], NULL, consumer, &ids[i]); for (int i = 0; i < ProducerCount; i++) pthread_join(pro[i], NULL); @@ -78,6 +105,10 @@ int main(int argc, char** argv) pthread_mutex_destroy(&mutex); sem_destroy(&empty); sem_destroy(&full); + free(pro); + free(con); + free(buffer); + free(ids); return 0; -} \ No newline at end of file +}