Remove unused localedata/th_TH.in
[glibc.git] / sysdeps / pthread / tst-fork1.c
blobf2d819b39f0b443caf50ae8df69404f6955ca6b5
1 /* Copyright (C) 2002-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
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>
26 static void *
27 thread_function (void * arg)
29 int i = (intptr_t) arg;
30 int status;
31 pid_t pid;
32 pid_t pid2;
34 pid = fork ();
35 switch (pid)
37 case 0:
38 printf ("%ld for %d\n", (long int) getpid (), i);
39 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 * i };
40 nanosleep (&ts, NULL);
41 _exit (i);
42 break;
43 case -1:
44 printf ("fork: %m\n");
45 return (void *) 1l;
46 break;
49 pid2 = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
50 if (pid2 != pid)
52 printf ("waitpid returned %ld, expected %ld\n",
53 (long int) pid2, (long int) pid);
54 return (void *) 1l;
57 printf ("%ld with %d, expected %d\n",
58 (long int) pid, WEXITSTATUS (status), i);
60 return WEXITSTATUS (status) == i ? NULL : (void *) 1l;
63 #define N 5
64 static const int t[N] = { 7, 6, 5, 4, 3 };
66 static int
67 do_test (void)
69 pthread_t th[N];
70 int i;
71 int result = 0;
72 pthread_attr_t at;
74 if (pthread_attr_init (&at) != 0)
76 puts ("attr_init failed");
77 return 1;
80 if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
82 puts ("attr_setstacksize failed");
83 return 1;
86 for (i = 0; i < N; ++i)
87 if (pthread_create (&th[i], NULL, thread_function,
88 (void *) (intptr_t) t[i]) != 0)
90 printf ("creation of thread %d failed\n", i);
91 exit (1);
94 if (pthread_attr_destroy (&at) != 0)
96 puts ("attr_destroy failed");
97 return 1;
100 for (i = 0; i < N; ++i)
102 void *v;
103 if (pthread_join (th[i], &v) != 0)
105 printf ("join of thread %d failed\n", i);
106 result = 1;
108 else if (v != NULL)
110 printf ("join %d successful, but child failed\n", i);
111 result = 1;
113 else
114 printf ("join %d successful\n", i);
117 return result;
120 #include <support/test-driver.c>