diff options
author | toni <matzeton@googlemail.com> | 2015-11-16 23:56:07 +0100 |
---|---|---|
committer | toni <matzeton@googlemail.com> | 2015-11-17 01:04:39 +0100 |
commit | cb8ac81c8c538f4a02edb5c2762cfbc5f5adef88 (patch) | |
tree | 8a305bfa89f406a89e6b3e4fbd286bf242a72920 /tests/producer_consumer.c | |
parent | 089551924a7c6bae2b03bee9999e5563f9267571 (diff) |
better tests
Diffstat (limited to 'tests/producer_consumer.c')
-rw-r--r-- | tests/producer_consumer.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/producer_consumer.c b/tests/producer_consumer.c new file mode 100644 index 0000000..104004a --- /dev/null +++ b/tests/producer_consumer.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <semaphore.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <fcntl.h> +#include <assert.h> + +#include "semconfig.h" + +sem_t *mysem = NULL, *mycnt = NULL; +pid_t child; + + +int main(int argc, char **argv) { + int semval = 0; + + if ( (mysem = sem_open(TESTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL && + (mycnt = sem_open(CNTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL ) { + assert( sem_getvalue(mycnt, &semval) == 0 ); + printf("factory producing %d items\n", semval); + + while ( semval-- >= 0 ) { + usleep(250); + LOG("consumer: -1"); + assert( sem_wait(mysem) == 0 ); + printf("remaining: %d\n", semval); + } + } else { + exit(1); + } + assert( sem_close(mysem) == 0 ); + assert( sem_unlink(TESTSEM) == 0 && sem_unlink(CNTSEM) == 0 ); + + exit(0); +} + |