Back to index

glibc  2.9
test-vfork.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <unistd.h>
00004 #include <error.h>
00005 #include <errno.h>
00006 #include <sys/wait.h>
00007 
00008 void __attribute_noinline__ noop (void);
00009 
00010 #define NR    2      /* Exit code of the child.  */
00011 
00012 int
00013 main (void)
00014 {
00015   pid_t pid;
00016   int status;
00017 
00018   printf ("Before vfork\n");
00019   fflush (stdout);
00020   pid = vfork ();
00021   if (pid == 0)
00022     {
00023       /* This will clobber the return pc from vfork in the parent on
00024         machines where it is stored on the stack, if vfork wasn't
00025         implemented correctly, */
00026       noop ();
00027       _exit (NR);
00028     }
00029   else if (pid < 0)
00030     error (1, errno, "vfork");
00031   printf ("After vfork (parent)\n");
00032   if (waitpid (0, &status, 0) != pid
00033       || !WIFEXITED (status) || WEXITSTATUS (status) != NR)
00034     exit (1);
00035 
00036   return 0;
00037 }
00038 
00039 void
00040 noop ()
00041 {
00042 }