drd/tests/swapcontext: Improve the portability of this test further
[valgrind.git] / drd / tests / threaded-fork.c
bloba83bf35dd204d3392c17869f6b161598c1a1ee0f
1 /* fork a process that has created a detached thread. */
3 #include <stdlib.h>
4 #include <sys/wait.h>
5 #include <pthread.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <signal.h>
11 static void *threadmain(void *dummy)
13 sleep((unsigned long)dummy);
14 return NULL;
17 int main(int argc, char **argv)
19 int ctr;
20 pid_t childpid;
21 pthread_t childthread;
22 void *res;
23 int status;
25 pthread_create(&childthread, NULL, threadmain, (void *)2);
26 pthread_detach(childthread);
28 childpid = fork();
29 switch (childpid) {
30 case 0:
31 pthread_create(&childthread, NULL, threadmain, 0);
32 pthread_join(childthread, &res);
33 exit(0);
34 break;
35 case -1:
36 perror("FAILED: fork failed\n");
37 break;
38 default:
39 break;
42 ctr = 0;
43 while (waitpid(childpid, &status, 0) != childpid) {
44 sleep(1);
45 ctr++;
46 if (ctr >= 10) {
47 fprintf(stderr, "FAILED - timeout waiting for child\n");
48 return 0;
52 fprintf(stderr, "PASS\n");
54 return 0;