threaded-fork.c revision e739ac0589b4fb43561f801c4faba8c1b89f8680
1#include <stdlib.h>
2#include <sys/wait.h>
3#include <pthread.h>
4#include <sys/types.h>
5#include <unistd.h>
6#include <stdio.h>
7#include <signal.h>
8
9static void *threadmain( void *dummy )
10{
11	sleep( (unsigned long)dummy );
12	return NULL;
13}
14
15int main( int argc, char **argv )
16{
17        int ctr;
18	pid_t childpid;
19	pthread_t childthread;
20	void *res;
21	int status;
22
23	pthread_create( &childthread, NULL, threadmain, (void *)2 );
24
25	childpid = fork();
26	switch( childpid ) {
27	case 0:
28		pthread_create( &childthread, NULL, threadmain, 0 );
29		pthread_join( childthread, &res );
30		exit(0);
31		break;
32	case -1:
33		perror( "FAILED: fork failed\n" );
34		break;
35	default:
36		break;
37	}
38
39	pthread_join( childthread, &res );
40	ctr = 0;
41	while(waitpid(childpid, &status, 0) != childpid) {
42		sleep(1);
43		ctr++;
44		if (ctr >= 10) {
45		  printf("FAILED - timeout waiting for child\n");
46		  return 0;
47		}
48	}
49
50	printf("PASS\n");
51
52	return 0;
53}
54
55
56