1WAIT(2) Linux Programmer's Manual WAIT(2)
2
3
4
6 wait, waitpid, waitid - wait for process to change state
7
9 #include <sys/wait.h>
10
11 pid_t wait(int *wstatus);
12 pid_t waitpid(pid_t pid, int *wstatus, int options);
13
14 int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
15 /* This is the glibc and POSIX interface; see
16 NOTES for information on the raw system call. */
17
18 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20 waitid():
21 Since glibc 2.26:
22 _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L
23 Glibc 2.25 and earlier:
24 _XOPEN_SOURCE
25 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
26 || /* Glibc <= 2.19: */ _BSD_SOURCE
27
29 All of these system calls are used to wait for state changes in a child
30 of the calling process, and obtain information about the child whose
31 state has changed. A state change is considered to be: the child ter‐
32 minated; the child was stopped by a signal; or the child was resumed by
33 a signal. In the case of a terminated child, performing a wait allows
34 the system to release the resources associated with the child; if a
35 wait is not performed, then the terminated child remains in a "zombie"
36 state (see NOTES below).
37
38 If a child has already changed state, then these calls return immedi‐
39 ately. Otherwise, they block until either a child changes state or a
40 signal handler interrupts the call (assuming that system calls are not
41 automatically restarted using the SA_RESTART flag of sigaction(2)). In
42 the remainder of this page, a child whose state has changed and which
43 has not yet been waited upon by one of these system calls is termed
44 waitable.
45
46 wait() and waitpid()
47 The wait() system call suspends execution of the calling thread until
48 one of its children terminates. The call wait(&wstatus) is equivalent
49 to:
50
51 waitpid(-1, &wstatus, 0);
52
53 The waitpid() system call suspends execution of the calling thread un‐
54 til a child specified by pid argument has changed state. By default,
55 waitpid() waits only for terminated children, but this behavior is mod‐
56 ifiable via the options argument, as described below.
57
58 The value of pid can be:
59
60 < -1 meaning wait for any child process whose process group ID is
61 equal to the absolute value of pid.
62
63 -1 meaning wait for any child process.
64
65 0 meaning wait for any child process whose process group ID is
66 equal to that of the calling process at the time of the call to
67 waitpid().
68
69 > 0 meaning wait for the child whose process ID is equal to the
70 value of pid.
71
72 The value of options is an OR of zero or more of the following con‐
73 stants:
74
75 WNOHANG
76 return immediately if no child has exited.
77
78 WUNTRACED
79 also return if a child has stopped (but not traced via
80 ptrace(2)). Status for traced children which have stopped is
81 provided even if this option is not specified.
82
83 WCONTINUED (since Linux 2.6.10)
84 also return if a stopped child has been resumed by delivery of
85 SIGCONT.
86
87 (For Linux-only options, see below.)
88
89 If wstatus is not NULL, wait() and waitpid() store status information
90 in the int to which it points. This integer can be inspected with the
91 following macros (which take the integer itself as an argument, not a
92 pointer to it, as is done in wait() and waitpid()!):
93
94 WIFEXITED(wstatus)
95 returns true if the child terminated normally, that is, by call‐
96 ing exit(3) or _exit(2), or by returning from main().
97
98 WEXITSTATUS(wstatus)
99 returns the exit status of the child. This consists of the
100 least significant 8 bits of the status argument that the child
101 specified in a call to exit(3) or _exit(2) or as the argument
102 for a return statement in main(). This macro should be employed
103 only if WIFEXITED returned true.
104
105 WIFSIGNALED(wstatus)
106 returns true if the child process was terminated by a signal.
107
108 WTERMSIG(wstatus)
109 returns the number of the signal that caused the child process
110 to terminate. This macro should be employed only if WIFSIGNALED
111 returned true.
112
113 WCOREDUMP(wstatus)
114 returns true if the child produced a core dump (see core(5)).
115 This macro should be employed only if WIFSIGNALED returned true.
116
117 This macro is not specified in POSIX.1-2001 and is not available
118 on some UNIX implementations (e.g., AIX, SunOS). Therefore, en‐
119 close its use inside #ifdef WCOREDUMP ... #endif.
120
121 WIFSTOPPED(wstatus)
122 returns true if the child process was stopped by delivery of a
123 signal; this is possible only if the call was done using WUN‐
124 TRACED or when the child is being traced (see ptrace(2)).
125
126 WSTOPSIG(wstatus)
127 returns the number of the signal which caused the child to stop.
128 This macro should be employed only if WIFSTOPPED returned true.
129
130 WIFCONTINUED(wstatus)
131 (since Linux 2.6.10) returns true if the child process was re‐
132 sumed by delivery of SIGCONT.
133
134 waitid()
135 The waitid() system call (available since Linux 2.6.9) provides more
136 precise control over which child state changes to wait for.
137
138 The idtype and id arguments select the child(ren) to wait for, as fol‐
139 lows:
140
141 idtype == P_PID
142 Wait for the child whose process ID matches id.
143
144 idtype == P_PIDFD (since Linux 5.4)
145 Wait for the child referred to by the PID file descriptor speci‐
146 fied in id. (See pidfd_open(2) for further information on PID
147 file descriptors.)
148
149 idtype == P_PGID
150 Wait for any child whose process group ID matches id. Since
151 Linux 5.4, if id is zero, then wait for any child that is in the
152 same process group as the caller's process group at the time of
153 the call.
154
155 idtype == P_ALL
156 Wait for any child; id is ignored.
157
158 The child state changes to wait for are specified by ORing one or more
159 of the following flags in options:
160
161 WEXITED
162 Wait for children that have terminated.
163
164 WSTOPPED
165 Wait for children that have been stopped by delivery of a sig‐
166 nal.
167
168 WCONTINUED
169 Wait for (previously stopped) children that have been resumed by
170 delivery of SIGCONT.
171
172 The following flags may additionally be ORed in options:
173
174 WNOHANG
175 As for waitpid().
176
177 WNOWAIT
178 Leave the child in a waitable state; a later wait call can be
179 used to again retrieve the child status information.
180
181 Upon successful return, waitid() fills in the following fields of the
182 siginfo_t structure pointed to by infop:
183
184 si_pid The process ID of the child.
185
186 si_uid The real user ID of the child. (This field is not set on most
187 other implementations.)
188
189 si_signo
190 Always set to SIGCHLD.
191
192 si_status
193 Either the exit status of the child, as given to _exit(2) (or
194 exit(3)), or the signal that caused the child to terminate,
195 stop, or continue. The si_code field can be used to determine
196 how to interpret this field.
197
198 si_code
199 Set to one of: CLD_EXITED (child called _exit(2)); CLD_KILLED
200 (child killed by signal); CLD_DUMPED (child killed by signal,
201 and dumped core); CLD_STOPPED (child stopped by signal);
202 CLD_TRAPPED (traced child has trapped); or CLD_CONTINUED (child
203 continued by SIGCONT).
204
205 If WNOHANG was specified in options and there were no children in a
206 waitable state, then waitid() returns 0 immediately and the state of
207 the siginfo_t structure pointed to by infop depends on the implementa‐
208 tion. To (portably) distinguish this case from that where a child was
209 in a waitable state, zero out the si_pid field before the call and
210 check for a nonzero value in this field after the call returns.
211
212 POSIX.1-2008 Technical Corrigendum 1 (2013) adds the requirement that
213 when WNOHANG is specified in options and there were no children in a
214 waitable state, then waitid() should zero out the si_pid and si_signo
215 fields of the structure. On Linux and other implementations that ad‐
216 here to this requirement, it is not necessary to zero out the si_pid
217 field before calling waitid(). However, not all implementations follow
218 the POSIX.1 specification on this point.
219
221 wait(): on success, returns the process ID of the terminated child; on
222 failure, -1 is returned.
223
224 waitpid(): on success, returns the process ID of the child whose state
225 has changed; if WNOHANG was specified and one or more child(ren) speci‐
226 fied by pid exist, but have not yet changed state, then 0 is returned.
227 On failure, -1 is returned.
228
229 waitid(): returns 0 on success or if WNOHANG was specified and no
230 child(ren) specified by id has yet changed state; on failure, -1 is re‐
231 turned.
232
233 On failure, each of these calls sets errno to indicate the error.
234
236 ECHILD (for wait()) The calling process does not have any unwaited-for
237 children.
238
239 ECHILD (for waitpid() or waitid()) The process specified by pid (wait‐
240 pid()) or idtype and id (waitid()) does not exist or is not a
241 child of the calling process. (This can happen for one's own
242 child if the action for SIGCHLD is set to SIG_IGN. See also the
243 Linux Notes section about threads.)
244
245 EINTR WNOHANG was not set and an unblocked signal or a SIGCHLD was
246 caught; see signal(7).
247
248 EINVAL The options argument was invalid.
249
251 SVr4, 4.3BSD, POSIX.1-2001.
252
254 A child that terminates, but has not been waited for becomes a "zom‐
255 bie". The kernel maintains a minimal set of information about the zom‐
256 bie process (PID, termination status, resource usage information) in
257 order to allow the parent to later perform a wait to obtain information
258 about the child. As long as a zombie is not removed from the system
259 via a wait, it will consume a slot in the kernel process table, and if
260 this table fills, it will not be possible to create further processes.
261 If a parent process terminates, then its "zombie" children (if any) are
262 adopted by init(1), (or by the nearest "subreaper" process as defined
263 through the use of the prctl(2) PR_SET_CHILD_SUBREAPER operation);
264 init(1) automatically performs a wait to remove the zombies.
265
266 POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to
267 SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)),
268 then children that terminate do not become zombies and a call to wait()
269 or waitpid() will block until all children have terminated, and then
270 fail with errno set to ECHILD. (The original POSIX standard left the
271 behavior of setting SIGCHLD to SIG_IGN unspecified. Note that even
272 though the default disposition of SIGCHLD is "ignore", explicitly set‐
273 ting the disposition to SIG_IGN results in different treatment of zom‐
274 bie process children.)
275
276 Linux 2.6 conforms to the POSIX requirements. However, Linux 2.4 (and
277 earlier) does not: if a wait() or waitpid() call is made while SIGCHLD
278 is being ignored, the call behaves just as though SIGCHLD were not be‐
279 ing ignored, that is, the call blocks until the next child terminates
280 and then returns the process ID and status of that child.
281
282 Linux notes
283 In the Linux kernel, a kernel-scheduled thread is not a distinct con‐
284 struct from a process. Instead, a thread is simply a process that is
285 created using the Linux-unique clone(2) system call; other routines
286 such as the portable pthread_create(3) call are implemented using
287 clone(2). Before Linux 2.4, a thread was just a special case of a
288 process, and as a consequence one thread could not wait on the children
289 of another thread, even when the latter belongs to the same thread
290 group. However, POSIX prescribes such functionality, and since Linux
291 2.4 a thread can, and by default will, wait on children of other
292 threads in the same thread group.
293
294 The following Linux-specific options are for use with children created
295 using clone(2); they can also, since Linux 4.7, be used with waitid():
296
297 __WCLONE
298 Wait for "clone" children only. If omitted, then wait for "non-
299 clone" children only. (A "clone" child is one which delivers no
300 signal, or a signal other than SIGCHLD to its parent upon termi‐
301 nation.) This option is ignored if __WALL is also specified.
302
303 __WALL (since Linux 2.4)
304 Wait for all children, regardless of type ("clone" or "non-
305 clone").
306
307 __WNOTHREAD (since Linux 2.4)
308 Do not wait for children of other threads in the same thread
309 group. This was the default before Linux 2.4.
310
311 Since Linux 4.7, the __WALL flag is automatically implied if the child
312 is being ptraced.
313
314 C library/kernel differences
315 wait() is actually a library function that (in glibc) is implemented as
316 a call to wait4(2).
317
318 On some architectures, there is no waitpid() system call; instead, this
319 interface is implemented via a C library wrapper function that calls
320 wait4(2).
321
322 The raw waitid() system call takes a fifth argument, of type struct
323 rusage *. If this argument is non-NULL, then it is used to return re‐
324 source usage information about the child, in the same manner as
325 wait4(2). See getrusage(2) for details.
326
328 According to POSIX.1-2008, an application calling waitid() must ensure
329 that infop points to a siginfo_t structure (i.e., that it is a non-null
330 pointer). On Linux, if infop is NULL, waitid() succeeds, and returns
331 the process ID of the waited-for child. Applications should avoid re‐
332 lying on this inconsistent, nonstandard, and unnecessary feature.
333
335 The following program demonstrates the use of fork(2) and waitpid().
336 The program creates a child process. If no command-line argument is
337 supplied to the program, then the child suspends its execution using
338 pause(2), to allow the user to send signals to the child. Otherwise,
339 if a command-line argument is supplied, then the child exits immedi‐
340 ately, using the integer supplied on the command line as the exit sta‐
341 tus. The parent process executes a loop that monitors the child using
342 waitpid(), and uses the W*() macros described above to analyze the wait
343 status value.
344
345 The following shell session demonstrates the use of the program:
346
347 $ ./a.out &
348 Child PID is 32360
349 [1] 32359
350 $ kill -STOP 32360
351 stopped by signal 19
352 $ kill -CONT 32360
353 continued
354 $ kill -TERM 32360
355 killed by signal 15
356 [1]+ Done ./a.out
357 $
358
359 Program source
360
361 #include <sys/wait.h>
362 #include <stdint.h>
363 #include <stdlib.h>
364 #include <unistd.h>
365 #include <stdio.h>
366
367 int
368 main(int argc, char *argv[])
369 {
370 pid_t cpid, w;
371 int wstatus;
372
373 cpid = fork();
374 if (cpid == -1) {
375 perror("fork");
376 exit(EXIT_FAILURE);
377 }
378
379 if (cpid == 0) { /* Code executed by child */
380 printf("Child PID is %jd\n", (intmax_t) getpid());
381 if (argc == 1)
382 pause(); /* Wait for signals */
383 _exit(atoi(argv[1]));
384
385 } else { /* Code executed by parent */
386 do {
387 w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
388 if (w == -1) {
389 perror("waitpid");
390 exit(EXIT_FAILURE);
391 }
392
393 if (WIFEXITED(wstatus)) {
394 printf("exited, status=%d\n", WEXITSTATUS(wstatus));
395 } else if (WIFSIGNALED(wstatus)) {
396 printf("killed by signal %d\n", WTERMSIG(wstatus));
397 } else if (WIFSTOPPED(wstatus)) {
398 printf("stopped by signal %d\n", WSTOPSIG(wstatus));
399 } else if (WIFCONTINUED(wstatus)) {
400 printf("continued\n");
401 }
402 } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
403 exit(EXIT_SUCCESS);
404 }
405 }
406
408 _exit(2), clone(2), fork(2), kill(2), ptrace(2), sigaction(2), sig‐
409 nal(2), wait4(2), pthread_create(3), core(5), credentials(7), signal(7)
410
412 This page is part of release 5.12 of the Linux man-pages project. A
413 description of the project, information about reporting bugs, and the
414 latest version of this page, can be found at
415 https://www.kernel.org/doc/man-pages/.
416
417
418
419Linux 2021-03-22 WAIT(2)