From cb8ac81c8c538f4a02edb5c2762cfbc5f5adef88 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 16 Nov 2015 23:56:07 +0100 Subject: better tests --- tests/producer_consumer.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/producer_consumer.c (limited to 'tests/producer_consumer.c') 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 +#include +#include +#include +#include +#include +#include +#include +#include + +#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); +} + -- cgit v1.2.3