Updated to fedora-glibc-20060109T2152
[glibc/history.git] / nptl / tst-fork2.c
blob07d6c22668262888cfbf9c8b6dee13310b93e2b2
1 /* Copyright (C) 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Roland McGrath <roland@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/wait.h>
28 static pid_t initial_pid;
31 static void *
32 tf (void *arg)
34 if (getppid () != initial_pid)
36 printf ("getppid in thread returned %ld, expected %ld\n",
37 (long int) getppid (), (long int) initial_pid);
38 return (void *) -1;
41 return NULL;
45 int
46 main (void)
48 initial_pid = getpid ();
50 pid_t child = fork ();
51 if (child == 0)
53 if (getppid () != initial_pid)
55 printf ("first getppid returned %ld, expected %ld\n",
56 (long int) getppid (), (long int) initial_pid);
57 exit (1);
60 pthread_t th;
61 if (pthread_create (&th, NULL, tf, NULL) != 0)
63 puts ("pthread_create failed");
64 exit (1);
67 void *result;
68 if (pthread_join (th, &result) != 0)
70 puts ("pthread_join failed");
71 exit (1);
74 exit (result == NULL ? 0 : 1);
76 else if (child == -1)
78 puts ("initial fork failed");
79 return 1;
82 int status;
83 if (TEMP_FAILURE_RETRY (waitpid (child, &status, 0)) != child)
85 printf ("waitpid failed: %m\n");
86 return 1;
89 return status;