Updated to fedora-glibc-20071017T2007
[glibc.git] / nptl / tst-fork3.c
blobbc73853a54c3a831a733621bb8de7a57d3524b61
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 tf2 (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 static void *
46 tf1 (void *arg)
48 pid_t child = fork ();
49 if (child == 0)
51 if (getppid () != initial_pid)
53 printf ("first getppid returned %ld, expected %ld\n",
54 (long int) getppid (), (long int) initial_pid);
55 exit (1);
58 pthread_t th2;
59 if (pthread_create (&th2, NULL, tf2, NULL) != 0)
61 puts ("child: pthread_create failed");
62 exit (1);
65 void *result;
66 if (pthread_join (th2, &result) != 0)
68 puts ("pthread_join failed");
69 exit (1);
72 exit (result == NULL ? 0 : 1);
74 else if (child == -1)
76 puts ("initial fork failed");
77 exit (1);
80 int status;
81 if (TEMP_FAILURE_RETRY (waitpid (child, &status, 0)) != child)
83 printf ("waitpid failed: %m\n");
84 exit (1);
87 exit (status);
91 int
92 main (void)
94 initial_pid = getpid ();
96 pthread_t th1;
97 if (pthread_create (&th1, NULL, tf1, NULL) != 0)
99 puts ("parent: pthread_create failed");
100 exit (1);
103 /* This call should never return. */
104 pthread_join (th1, NULL);
106 return 1;