Back to index

glibc  2.9
tst-fork3.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Roland McGrath <roland@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <pthread.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <sys/wait.h>
00026 
00027 
00028 static pid_t initial_pid;
00029 
00030 
00031 static void *
00032 tf2 (void *arg)
00033 {
00034   if (getppid () != initial_pid)
00035     {
00036       printf ("getppid in thread returned %ld, expected %ld\n",
00037              (long int) getppid (), (long int) initial_pid);
00038       return (void *) -1;
00039     }
00040 
00041   return NULL;
00042 }
00043 
00044 
00045 static void *
00046 tf1 (void *arg)
00047 {
00048   pid_t child = fork ();
00049   if (child == 0)
00050     {
00051       if (getppid () != initial_pid)
00052        {
00053          printf ("first getppid returned %ld, expected %ld\n",
00054                 (long int) getppid (), (long int) initial_pid);
00055          exit (1);
00056        }
00057 
00058       pthread_t th2;
00059       if (pthread_create (&th2, NULL, tf2, NULL) != 0)
00060        {
00061          puts ("child: pthread_create failed");
00062          exit (1);
00063        }
00064 
00065       void *result;
00066       if (pthread_join (th2, &result) != 0)
00067        {
00068          puts ("pthread_join failed");
00069          exit  (1);
00070        }
00071 
00072       exit (result == NULL ? 0 : 1);
00073     }
00074   else if (child == -1)
00075     {
00076       puts ("initial fork failed");
00077       exit (1);
00078     }
00079 
00080   int status;
00081   if (TEMP_FAILURE_RETRY (waitpid (child, &status, 0)) != child)
00082     {
00083       printf ("waitpid failed: %m\n");
00084       exit (1);
00085     }
00086 
00087   exit (status);
00088 }
00089 
00090 
00091 int
00092 main (void)
00093 {
00094   initial_pid = getpid ();
00095 
00096   pthread_t th1;
00097   if (pthread_create (&th1, NULL, tf1, NULL) != 0)
00098     {
00099       puts ("parent: pthread_create failed");
00100       exit (1);
00101     }
00102 
00103   /* This call should never return.  */
00104   pthread_join (th1, NULL);
00105 
00106   return 1;
00107 }