summaryrefslogtreecommitdiff
path: root/lib/libc_r/TEST/test_fork.c
blob: efaafb0a7baa9bb31879cda1e791ad0cf013f46e (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* ==== test_fork.c ============================================================
 * Copyright (c) 1994 by Chris Provenzano, proven@athena.mit.edu
 *
 * Description : Test fork() and dup2() calls.
 *
 *  1.00 94/04/29 proven
 *      -Started coding this file.
 */

#include <pthread.h>
#include <pthread_np.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include "test.h"

void *
sleeper(void *arg)
{
	pthread_set_name_np(pthread_self(), "slpr");
	printf("sleeper\n");
	sleep(10);
	PANIC("sleeper timed out");
}

static void
sigchld(sig)
	int sig;
{
	int status;

	ASSERT(sig == SIGCHLD);
	CHECKe(wait(&status));
	ASSERT(WIFEXITED(status));
	ASSERT(WEXITSTATUS(status) == 0);
	SUCCEED;
}

int
main()
{
	int flags;
	pid_t pid;
	pthread_t sleeper_thread;

	CHECKe(flags = fcntl(STDOUT_FILENO, F_GETFL));
	if ((flags & (O_NONBLOCK | O_NDELAY))) {
		CHECKe(fcntl(STDOUT_FILENO, F_SETFL, 
		    flags & ~(O_NONBLOCK | O_NDELAY)));
	}

	CHECKr(pthread_create(&sleeper_thread, NULL, sleeper, NULL));
	sleep(1);

	CHECKe(signal(SIGCHLD, sigchld));

	printf("forking\n");

	CHECKe(pid = fork());
	switch(pid) {
	case 0:
		sleep(1);
		printf("child process %d\n", getpid());
		_thread_dump_info();
		printf("\n");
		_exit(0);
		PANIC("_exit");
	default:
		printf("parent process %d [child %d]\n", getpid(), pid);
		_thread_dump_info();
		printf("\n");
		CHECKe(pause());
		PANIC("pause");
	}
}