1/* Copyright (C) 2002-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <errno.h>
19#include <pthread.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24#include <sys/wait.h>
25
26static void *
27thread_function (void * arg)
28{
29 int i = (intptr_t) arg;
30 int status;
31 pid_t pid;
32 pid_t pid2;
33
34 pid = fork ();
35 switch (pid)
36 {
37 case 0:
38 printf (format: "%ld for %d\n", (long int) getpid (), i);
39 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 * i };
40 nanosleep (requested_time: &ts, NULL);
41 _exit (i);
42 break;
43 case -1:
44 printf (format: "fork: %m\n");
45 return (void *) 1l;
46 break;
47 }
48
49 pid2 = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
50 if (pid2 != pid)
51 {
52 printf (format: "waitpid returned %ld, expected %ld\n",
53 (long int) pid2, (long int) pid);
54 return (void *) 1l;
55 }
56
57 printf (format: "%ld with %d, expected %d\n",
58 (long int) pid, WEXITSTATUS (status), i);
59
60 return WEXITSTATUS (status) == i ? NULL : (void *) 1l;
61}
62
63#define N 5
64static const int t[N] = { 7, 6, 5, 4, 3 };
65
66static int
67do_test (void)
68{
69 pthread_t th[N];
70 int i;
71 int result = 0;
72 pthread_attr_t at;
73
74 if (pthread_attr_init (attr: &at) != 0)
75 {
76 puts (s: "attr_init failed");
77 return 1;
78 }
79
80 if (pthread_attr_setstacksize (attr: &at, stacksize: 1 * 1024 * 1024) != 0)
81 {
82 puts (s: "attr_setstacksize failed");
83 return 1;
84 }
85
86 for (i = 0; i < N; ++i)
87 if (pthread_create (newthread: &th[i], NULL, start_routine: thread_function,
88 arg: (void *) (intptr_t) t[i]) != 0)
89 {
90 printf (format: "creation of thread %d failed\n", i);
91 exit (1);
92 }
93
94 if (pthread_attr_destroy (attr: &at) != 0)
95 {
96 puts (s: "attr_destroy failed");
97 return 1;
98 }
99
100 for (i = 0; i < N; ++i)
101 {
102 void *v;
103 if (pthread_join (th: th[i], thread_return: &v) != 0)
104 {
105 printf (format: "join of thread %d failed\n", i);
106 result = 1;
107 }
108 else if (v != NULL)
109 {
110 printf (format: "join %d successful, but child failed\n", i);
111 result = 1;
112 }
113 else
114 printf (format: "join %d successful\n", i);
115 }
116
117 return result;
118}
119
120#include <support/test-driver.c>
121

source code of glibc/sysdeps/pthread/tst-fork1.c