1#include <errno.h>
2#include <pthread.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <unistd.h>
7#include <sys/wait.h>
8
9
10static pid_t pid;
11
12static void *
13pid_thread (void *arg)
14{
15 if (pid != getpid ())
16 {
17 printf (format: "pid wrong in thread: should be %d, is %d\n",
18 (int) pid, (int) getpid ());
19 return (void *) 1L;
20 }
21
22 return NULL;
23}
24
25static int
26do_test (void)
27{
28 pid = getpid ();
29
30 pthread_t thr;
31 int ret = pthread_create (newthread: &thr, NULL, start_routine: pid_thread, NULL);
32 if (ret)
33 {
34 printf (format: "pthread_create failed: %d\n", ret);
35 return 1;
36 }
37
38 void *thr_ret;
39 ret = pthread_join (th: thr, thread_return: &thr_ret);
40 if (ret)
41 {
42 printf (format: "pthread_create failed: %d\n", ret);
43 return 1;
44 }
45 else if (thr_ret)
46 {
47 printf (format: "thread getpid failed\n");
48 return 1;
49 }
50
51 pid_t child = fork ();
52 if (child == -1)
53 {
54 printf (format: "fork failed: %m\n");
55 return 1;
56 }
57 else if (child == 0)
58 {
59 if (pid == getpid ())
60 {
61 puts (s: "pid did not change after fork");
62 exit (1);
63 }
64
65 pid = getpid ();
66 ret = pthread_create (newthread: &thr, NULL, start_routine: pid_thread, NULL);
67 if (ret)
68 {
69 printf (format: "pthread_create failed: %d\n", ret);
70 return 1;
71 }
72
73 ret = pthread_join (th: thr, thread_return: &thr_ret);
74 if (ret)
75 {
76 printf (format: "pthread_create failed: %d\n", ret);
77 return 1;
78 }
79 else if (thr_ret)
80 {
81 printf (format: "thread getpid failed\n");
82 return 1;
83 }
84
85 return 0;
86 }
87
88 int status;
89 if (TEMP_FAILURE_RETRY (waitpid (child, &status, 0)) != child)
90 {
91 puts (s: "waitpid failed");
92 kill (pid: child, SIGKILL);
93 return 1;
94 }
95
96 if (!WIFEXITED (status))
97 {
98 if (WIFSIGNALED (status))
99 printf (format: "died from signal %s\n", strsignal (WTERMSIG (status)));
100 else
101 puts (s: "did not terminate correctly");
102 return 1;
103 }
104 if (WEXITSTATUS (status) != 0)
105 {
106 printf (format: "exit code %d\n", WEXITSTATUS (status));
107 return 1;
108 }
109
110 return 0;
111}
112
113#define TEST_FUNCTION do_test ()
114#include "../test-skeleton.c"
115

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