1 /* fork a process that has created a detached thread. */
2 
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>
10 
11 static void *threadmain(void *dummy)
12 {
13 	sleep((unsigned long)dummy);
14 	return NULL;
15 }
16 
17 int main(int argc, char **argv)
18 {
19         int ctr;
20 	pid_t childpid;
21 	pthread_t childthread;
22 	void *res;
23 	int status;
24 
25 	pthread_create(&childthread, NULL, threadmain, (void *)2);
26 	pthread_detach(childthread);
27 
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;
40 	}
41 
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;
49 		}
50 	}
51 
52 	fprintf(stderr, "PASS\n");
53 
54 	return 0;
55 }
56