blob: fdecf2d2ed4e123f6608692db464da6960c8856d (
plain)
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
|
#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;
pid_t child;
int main(int argc, char **argv) {
sem_unlink(TESTSEM);
if ( (mysem = sem_open(TESTSEM, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) != NULL ) {
if ( (child = fork()) == 0 ) {
/* child */
usleep(250);
LOG("child: sempost");
sem_post(mysem);
LOG("child: done");
usleep(250);
exit(0);
} else if (child > 0) {
/* parent */
LOG("parent: semwait");
sem_wait(mysem);
LOG("parent: waitpid");
waitpid(child, NULL, 0);
} else if (child < 0) {
perror("fork");
}
} else {
sem_close(mysem);
exit(1);
}
int sval;
assert ( sem_getvalue(mysem, &sval) == 0 );
assert (sval == 0);
sem_unlink(TESTSEM);
exit(0);
}
|