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 EAGAIN The PID file descriptor specified in id is nonblocking and the
237 process that it refers to has not terminated.
238
239 ECHILD (for wait()) The calling process does not have any unwaited-for
240 children.
241
242 ECHILD (for waitpid() or waitid()) The process specified by pid (wait‐
243 pid()) or idtype and id (waitid()) does not exist or is not a
244 child of the calling process. (This can happen for one's own
245 child if the action for SIGCHLD is set to SIG_IGN. See also the
246 Linux Notes section about threads.)
247
248 EINTR WNOHANG was not set and an unblocked signal or a SIGCHLD was
249 caught; see signal(7).
250
251 EINVAL The options argument was invalid.
252
253 ESRCH (for wait() or waitpid()) pid is equal to INT_MIN.
254
256 SVr4, 4.3BSD, POSIX.1-2001.
257
259 A child that terminates, but has not been waited for becomes a "zom‐
260 bie". The kernel maintains a minimal set of information about the zom‐
261 bie process (PID, termination status, resource usage information) in
262 order to allow the parent to later perform a wait to obtain information
263 about the child. As long as a zombie is not removed from the system
264 via a wait, it will consume a slot in the kernel process table, and if
265 this table fills, it will not be possible to create further processes.
266 If a parent process terminates, then its "zombie" children (if any) are
267 adopted by init(1), (or by the nearest "subreaper" process as defined
268 through the use of the prctl(2) PR_SET_CHILD_SUBREAPER operation);
269 init(1) automatically performs a wait to remove the zombies.
270
271 POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to
272 SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)),
273 then children that terminate do not become zombies and a call to wait()
274 or waitpid() will block until all children have terminated, and then
275 fail with errno set to ECHILD. (The original POSIX standard left the
276 behavior of setting SIGCHLD to SIG_IGN unspecified. Note that even
277 though the default disposition of SIGCHLD is "ignore", explicitly set‐
278 ting the disposition to SIG_IGN results in different treatment of zom‐
279 bie process children.)
280
281 Linux 2.6 conforms to the POSIX requirements. However, Linux 2.4 (and
282 earlier) does not: if a wait() or waitpid() call is made while SIGCHLD
283 is being ignored, the call behaves just as though SIGCHLD were not be‐
284 ing ignored, that is, the call blocks until the next child terminates
285 and then returns the process ID and status of that child.
286
287 Linux notes
288 In the Linux kernel, a kernel-scheduled thread is not a distinct con‐
289 struct from a process. Instead, a thread is simply a process that is
290 created using the Linux-unique clone(2) system call; other routines
291 such as the portable pthread_create(3) call are implemented using
292 clone(2). Before Linux 2.4, a thread was just a special case of a
293 process, and as a consequence one thread could not wait on the children
294 of another thread, even when the latter belongs to the same thread
295 group. However, POSIX prescribes such functionality, and since Linux
296 2.4 a thread can, and by default will, wait on children of other
297 threads in the same thread group.
298
299 The following Linux-specific options are for use with children created
300 using clone(2); they can also, since Linux 4.7, be used with waitid():
301
302 __WCLONE
303 Wait for "clone" children only. If omitted, then wait for "non-
304 clone" children only. (A "clone" child is one which delivers no
305 signal, or a signal other than SIGCHLD to its parent upon termi‐
306 nation.) This option is ignored if __WALL is also specified.
307
308 __WALL (since Linux 2.4)
309 Wait for all children, regardless of type ("clone" or "non-
310 clone").
311
312 __WNOTHREAD (since Linux 2.4)
313 Do not wait for children of other threads in the same thread
314 group. This was the default before Linux 2.4.
315
316 Since Linux 4.7, the __WALL flag is automatically implied if the child
317 is being ptraced.
318
319 C library/kernel differences
320 wait() is actually a library function that (in glibc) is implemented as
321 a call to wait4(2).
322
323 On some architectures, there is no waitpid() system call; instead, this
324 interface is implemented via a C library wrapper function that calls
325 wait4(2).
326
327 The raw waitid() system call takes a fifth argument, of type struct
328 rusage *. If this argument is non-NULL, then it is used to return re‐
329 source usage information about the child, in the same manner as
330 wait4(2). See getrusage(2) for details.
331
333 According to POSIX.1-2008, an application calling waitid() must ensure
334 that infop points to a siginfo_t structure (i.e., that it is a non-null
335 pointer). On Linux, if infop is NULL, waitid() succeeds, and returns
336 the process ID of the waited-for child. Applications should avoid re‐
337 lying on this inconsistent, nonstandard, and unnecessary feature.
338
340 The following program demonstrates the use of fork(2) and waitpid().
341 The program creates a child process. If no command-line argument is
342 supplied to the program, then the child suspends its execution using
343 pause(2), to allow the user to send signals to the child. Otherwise,
344 if a command-line argument is supplied, then the child exits immedi‐
345 ately, using the integer supplied on the command line as the exit sta‐
346 tus. The parent process executes a loop that monitors the child using
347 waitpid(), and uses the W*() macros described above to analyze the wait
348 status value.
349
350 The following shell session demonstrates the use of the program:
351
352 $ ./a.out &
353 Child PID is 32360
354 [1] 32359
355 $ kill -STOP 32360
356 stopped by signal 19
357 $ kill -CONT 32360
358 continued
359 $ kill -TERM 32360
360 killed by signal 15
361 [1]+ Done ./a.out
362 $
363
364 Program source
365
366 #include <sys/wait.h>
367 #include <stdint.h>
368 #include <stdlib.h>
369 #include <unistd.h>
370 #include <stdio.h>
371
372 int
373 main(int argc, char *argv[])
374 {
375 pid_t cpid, w;
376 int wstatus;
377
378 cpid = fork();
379 if (cpid == -1) {
380 perror("fork");
381 exit(EXIT_FAILURE);
382 }
383
384 if (cpid == 0) { /* Code executed by child */
385 printf("Child PID is %jd\n", (intmax_t) getpid());
386 if (argc == 1)
387 pause(); /* Wait for signals */
388 _exit(atoi(argv[1]));
389
390 } else { /* Code executed by parent */
391 do {
392 w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
393 if (w == -1) {
394 perror("waitpid");
395 exit(EXIT_FAILURE);
396 }
397
398 if (WIFEXITED(wstatus)) {
399 printf("exited, status=%d\n", WEXITSTATUS(wstatus));
400 } else if (WIFSIGNALED(wstatus)) {
401 printf("killed by signal %d\n", WTERMSIG(wstatus));
402 } else if (WIFSTOPPED(wstatus)) {
403 printf("stopped by signal %d\n", WSTOPSIG(wstatus));
404 } else if (WIFCONTINUED(wstatus)) {
405 printf("continued\n");
406 }
407 } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
408 exit(EXIT_SUCCESS);
409 }
410 }
411
413 _exit(2), clone(2), fork(2), kill(2), ptrace(2), sigaction(2), sig‐
414 nal(2), wait4(2), pthread_create(3), core(5), credentials(7), signal(7)
415
417 This page is part of release 5.13 of the Linux man-pages project. A
418 description of the project, information about reporting bugs, and the
419 latest version of this page, can be found at
420 https://www.kernel.org/doc/man-pages/.
421
422
423
424Linux 2021-08-27 WAIT(2)