]> git.saurik.com Git - apple/libc.git/blob - pthreads/tests/pthread_atfork_test.c
Libc-594.9.5.tar.gz
[apple/libc.git] / pthreads / tests / pthread_atfork_test.c
1 #include <assert.h>
2 #include <pthread.h>
3 #include <sys/types.h>
4 #include <sys/wait.h>
5
6 /*
7 * Parent and child handlers are called in the order they were registered
8 * prepare handlers are called in reverse order. The non-commutative
9 * operations will ensure that we are calling them in the proper order.
10 */
11
12 static int parentval = 0;
13 static int childval = 0;
14
15 static void prepare1(void)
16 {
17 parentval *= 2;
18 }
19
20 static void prepare2(void)
21 {
22 parentval = 3;
23 }
24
25 static void parent1(void)
26 {
27 parentval += 4;
28 }
29
30 static void parent2(void)
31 {
32 parentval *= 3;
33 }
34
35 static void child1(void)
36 {
37 childval = 5;
38 }
39
40 static void child2(void)
41 {
42 childval *= 3;
43 }
44
45 int
46 main(void)
47 {
48 pid_t pid, child;
49 int status;
50
51 assert(!pthread_atfork(prepare1, parent1, child1));
52 assert(!pthread_atfork(prepare2, parent2, child2));
53 pid = fork();
54 assert(pid >= 0);
55 if (pid == 0) {
56 _exit(childval);
57 } else {
58 child = waitpid(pid, &status, 0);
59 assert(child == pid);
60 assert(WIFEXITED(status));
61 assert(WEXITSTATUS(status) == 15);
62 assert(parentval == 30);
63 }
64 return 0;
65 }