Sync usage with man page.
[netbsd-mini2440.git] / regress / lib / libpthread / fork / fork.c
blobb0856d29adeef9755284e07f42bf3d3de5a3c248
1 /* $NetBSD: fork.c,v 1.2 2003/04/11 12:56:25 lha Exp $ */
3 /*
4 * Check that child process doesn't get threads, also make sure sleep
5 * works in child.
7 * Written by Love Hörnquist Åstrand <lha@NetBSD.org>, March 2003.
8 * Public domain.
9 */
11 #include <sys/types.h>
12 #include <sys/wait.h>
14 #include <err.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <signal.h>
18 #include <pthread.h>
19 #include <unistd.h>
21 static pid_t parent;
22 static int thread_survived = 0;
24 static void *
25 print_pid(void *arg)
27 sleep(3);
29 thread_survived = 1;
30 if (parent != getpid()) {
31 kill(parent, SIGKILL);
32 errx(1, "child thread running");
34 return NULL;
37 int
38 main(int argc, char **argv)
40 pthread_t p;
41 pid_t fork_pid;
42 int ret;
44 parent = getpid();
46 pthread_create(&p, NULL, print_pid, NULL);
48 fork_pid = fork();
49 if (fork_pid == -1)
50 err(1, "fork failed");
52 if (fork_pid) {
53 ret = pthread_join(p, NULL);
54 if (ret && fork_pid != 0)
55 errx(1, "join failed in parent");
57 if (!thread_survived)
58 errx(1, "child_thread did NOT survive");
60 if (fork_pid != 0) {
61 int status;
62 waitpid(fork_pid, &status, 0);
63 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
64 errx(1, "child died wrongly");
66 } else {
67 sleep(5);
69 if (thread_survived) {
70 kill(parent, SIGKILL);
71 errx(1, "child_thread survived");
75 return 0;