1 /* Test for vfork functions.
2 Copyright (C) 2004-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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, see
17 <https://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
26 /* This test relies on non-POSIX functionality since the child
27 processes call write and getpid. */
40 /* First vfork() without previous getpid(). */
42 if ((p1
= vfork ()) == 0)
45 _exit (TEMP_FAILURE_RETRY (write (fd
[1], &p
, sizeof (p
))) != sizeof (p
));
49 puts ("1st vfork failed");
54 if (TEMP_FAILURE_RETRY (read (fd
[0], &p2
, sizeof (pid_t
))) != sizeof (pid_t
))
56 puts ("1st read failed");
60 if (TEMP_FAILURE_RETRY (waitpid (p1
, &r
, 0)) != p1
)
62 puts ("1st waitpid failed");
67 puts ("write in 1st child failed");
71 /* Main process' ID. */
74 /* vfork() again, but after a getpid() in the main process. */
76 if ((p3
= vfork ()) == 0)
79 _exit (TEMP_FAILURE_RETRY (write (fd
[1], &p
, sizeof (p
))) != sizeof (p
));
83 puts ("2nd vfork failed");
88 if (TEMP_FAILURE_RETRY (read (fd
[0], &p4
, sizeof (pid_t
))) != sizeof (pid_t
))
90 puts ("2nd read failed");
93 if (TEMP_FAILURE_RETRY (waitpid (p3
, &r
, 0)) != p3
)
95 puts ("2nd waitpid failed");
100 puts ("write in 2nd child failed");
104 /* And getpid in the main process again. */
105 pid_t p5
= getpid ();
107 /* Analysis of the results. */
110 printf ("p0(%ld) != p5(%ld)\n", (long int) p0
, (long int) p5
);
116 printf ("p0(%ld) == p1(%ld)\n", (long int) p0
, (long int) p1
);
122 printf ("p1(%ld) != p2(%ld)\n", (long int) p1
, (long int) p2
);
128 printf ("p0(%ld) == p3(%ld)\n", (long int) p0
, (long int) p3
);
134 printf ("p3(%ld) != p4(%ld)\n", (long int) p3
, (long int) p4
);
147 #define TEST_FUNCTION do_test ()
148 #include "../test-skeleton.c"