1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
/* $OpenBSD: sem_destroy.c,v 1.1.1.1 2012/01/04 17:36:40 mpi Exp $ */
/*
* Martin Pieuchot <mpi@openbsd.org>, 2011. Public Domain.
*/
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include "test.h"
int val;
sem_t cons_sem;
sem_t prod_sem;
void *producer(void *arg);
void *consumer(void *arg);
int
main(int argc, char **argv)
{
pthread_t prod_th, cons_th;
long counter = 4;
CHECKn(sem_destroy(&cons_sem));
ASSERT(errno == EINVAL);
val = 0;
CHECKr(sem_init(&cons_sem, 0, 0));
CHECKr(sem_init(&prod_sem, 0, 1));
CHECKr(pthread_create(&prod_th, NULL, producer, &counter));
CHECKr(pthread_create(&cons_th, NULL, consumer, &counter));
CHECKr(pthread_join(prod_th, NULL));
CHECKr(pthread_join(cons_th, NULL));
pthread_exit(NULL);
CHECKr(sem_destroy(&prod_sem));
CHECKr(sem_destroy(&cons_sem));
SUCCEED;
}
void *
producer(void *arg)
{
long *counter = arg;
int i;
for (i = 0; i < *counter; i++) {
sem_wait(&prod_sem);
val++;
sem_post(&cons_sem);
}
return (NULL);
}
void *
consumer(void *arg)
{
long *counter = arg;
int i;
for (i = 0; i < *counter; i++) {
sem_wait(&cons_sem);
val--;
sem_post(&prod_sem);
}
return (NULL);
}
|