Back to index

glibc  2.9
tst-atfork1.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@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 int val;
00029 
00030 
00031 static void
00032 prepare1 (void)
00033 {
00034   val *= 2;
00035 }
00036 
00037 static void
00038 prepare2 (void)
00039 {
00040   ++val;
00041 }
00042 
00043 static void
00044 parent1 (void)
00045 {
00046   val += 4;
00047 }
00048 
00049 static void
00050 parent2 (void)
00051 {
00052   val *= 4;
00053 }
00054 
00055 static void
00056 child1 (void)
00057 {
00058   val += 8;
00059 }
00060 
00061 static void
00062 child2 (void)
00063 {
00064   val *= 8;
00065 }
00066 
00067 
00068 static int
00069 do_test (void)
00070 {
00071   pid_t pid;
00072   int status = 0;
00073 
00074   if (pthread_atfork (prepare1, parent1, child1) != 0)
00075     {
00076       puts ("1st atfork failed");
00077       exit (1);
00078     }
00079   if (pthread_atfork (prepare2, parent2, child2) != 0)
00080     {
00081       puts ("2nd atfork failed");
00082       exit (1);
00083     }
00084 
00085   pid = fork ();
00086   if (pid == -1)
00087     {
00088       puts ("fork failed");
00089       exit (1);
00090     }
00091 
00092   if (pid != 0)
00093     {
00094       /* Parent.  */
00095       if (val != 24)
00096        {
00097          printf ("expected val=%d, got %d\n", 24, val);
00098          exit (1);
00099        }
00100 
00101       if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
00102        {
00103          puts ("waitpid failed");
00104          exit (1);
00105        }
00106     }
00107   else
00108     {
00109       /* Child.  */
00110       if (val != 80)
00111        {
00112          printf ("expected val=%d, got %d\n", 80, val);
00113          exit (2);
00114        }
00115     }
00116 
00117   return status;
00118 }
00119 
00120 #define TEST_FUNCTION do_test ()
00121 #include "../test-skeleton.c"