diff options
Diffstat (limited to 'tests/producer.c')
-rw-r--r-- | tests/producer.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/producer.c b/tests/producer.c new file mode 100644 index 0000000..f1a568d --- /dev/null +++ b/tests/producer.c @@ -0,0 +1,38 @@ +#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 = 10; + + if (argc == 2) { + semval = atoi(argv[1]); + } + + if ( (mysem = sem_open(TESTSEM, O_CREAT, S_IRUSR | S_IWUSR, 0)) != NULL && + (mycnt = sem_open(CNTSEM, O_CREAT, S_IRUSR | S_IWUSR, semval)) != NULL ) { + while (semval-- >= 0) { + usleep(250); + printf("producer: +1"); + assert( sem_post(mysem) == 0 ); + printf("remaining: %d\n", semval); + } + assert( sem_close(mysem) == 0 && sem_close(mycnt) == 0 ); + } else { + exit(1); + } + exit(0); +} + |