1 /* Test for vfork functions.
2 Copyright (C) 2004-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
24 #include <sys/types.h>
27 /* This test relies on non-POSIX functionality since the child
28 processes call write and getpid. */
41 /* First vfork() without previous getpid(). */
43 if ((p1
= vfork ()) == 0)
46 _exit (TEMP_FAILURE_RETRY (write (fd
[1], &p
, sizeof (p
))) != sizeof (p
));
50 puts ("1st vfork failed");
55 if (TEMP_FAILURE_RETRY (read (fd
[0], &p2
, sizeof (pid_t
))) != sizeof (pid_t
))
57 puts ("1st read failed");
61 if (TEMP_FAILURE_RETRY (waitpid (p1
, &r
, 0)) != p1
)
63 puts ("1st waitpid failed");
68 puts ("write in 1st child failed");
72 /* Main process' ID. */
75 /* vfork() again, but after a getpid() in the main process. */
77 if ((p3
= vfork ()) == 0)
80 _exit (TEMP_FAILURE_RETRY (write (fd
[1], &p
, sizeof (p
))) != sizeof (p
));
84 puts ("2nd vfork failed");
89 if (TEMP_FAILURE_RETRY (read (fd
[0], &p4
, sizeof (pid_t
))) != sizeof (pid_t
))
91 puts ("2nd read failed");
94 if (TEMP_FAILURE_RETRY (waitpid (p3
, &r
, 0)) != p3
)
96 puts ("2nd waitpid failed");
101 puts ("write in 2nd child failed");
105 /* And getpid in the main process again. */
106 pid_t p5
= getpid ();
108 /* Analysis of the results. */
111 printf ("p0(%ld) != p5(%ld)\n", (long int) p0
, (long int) p5
);
117 printf ("p0(%ld) == p1(%ld)\n", (long int) p0
, (long int) p1
);
123 printf ("p1(%ld) != p2(%ld)\n", (long int) p1
, (long int) p2
);
129 printf ("p0(%ld) == p3(%ld)\n", (long int) p0
, (long int) p3
);
135 printf ("p3(%ld) != p4(%ld)\n", (long int) p3
, (long int) p4
);
148 #define TEST_FUNCTION do_test ()
149 #include "../test-skeleton.c"