1WAIT(2)                    Linux Programmer's Manual                   WAIT(2)
2
3
4

NAME

6       wait, waitpid, waitid - wait for process to change state
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/wait.h>
11
12       pid_t wait(int *wstatus);
13
14       pid_t waitpid(pid_t pid, int *wstatus, int options);
15
16       int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
17                       /* This is the glibc and POSIX interface; see
18                          NOTES for information on the raw system call. */
19
20   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
21
22       waitid():
23           Since glibc 2.26: _XOPEN_SOURCE >= 500 ||
24               _POSIX_C_SOURCE >= 200809L
25           Glibc 2.25 and earlier:
26               _XOPEN_SOURCE
27                   || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
28                   || /* Glibc versions <= 2.19: */ _BSD_SOURCE
29

DESCRIPTION

31       All of these system calls are used to wait for state changes in a child
32       of the calling process, and obtain information about  the  child  whose
33       state  has changed.  A state change is considered to be: the child ter‐
34       minated; the child was stopped by a signal; or the child was resumed by
35       a  signal.  In the case of a terminated child, performing a wait allows
36       the system to release the resources associated with  the  child;  if  a
37       wait  is not performed, then the terminated child remains in a "zombie"
38       state (see NOTES below).
39
40       If a child has already changed state, then these calls  return  immedi‐
41       ately.   Otherwise,  they block until either a child changes state or a
42       signal handler interrupts the call (assuming that system calls are  not
43       automatically restarted using the SA_RESTART flag of sigaction(2)).  In
44       the remainder of this page, a child whose state has changed  and  which
45       has  not  yet  been  waited upon by one of these system calls is termed
46       waitable.
47
48   wait() and waitpid()
49       The wait() system call suspends execution of the calling  thread  until
50       one  of its children terminates.  The call wait(&wstatus) is equivalent
51       to:
52
53           waitpid(-1, &wstatus, 0);
54
55       The waitpid() system call suspends execution of the calling thread  un‐
56       til  a  child specified by pid argument has changed state.  By default,
57       waitpid() waits only for terminated children, but this behavior is mod‐
58       ifiable via the options argument, as described below.
59
60       The value of pid can be:
61
62       < -1   meaning  wait  for  any  child process whose process group ID is
63              equal to the absolute value of pid.
64
65       -1     meaning wait for any child process.
66
67       0      meaning wait for any child process whose  process  group  ID  is
68              equal  to that of the calling process at the time of the call to
69              waitpid().
70
71       > 0    meaning wait for the child whose process  ID  is  equal  to  the
72              value of pid.
73
74       The  value  of  options  is an OR of zero or more of the following con‐
75       stants:
76
77       WNOHANG
78              return immediately if no child has exited.
79
80       WUNTRACED
81              also  return  if  a  child  has  stopped  (but  not  traced  via
82              ptrace(2)).   Status  for  traced children which have stopped is
83              provided even if this option is not specified.
84
85       WCONTINUED (since Linux 2.6.10)
86              also return if a stopped child has been resumed by  delivery  of
87              SIGCONT.
88
89       (For Linux-only options, see below.)
90
91       If  wstatus  is not NULL, wait() and waitpid() store status information
92       in the int to which it points.  This integer can be inspected with  the
93       following  macros  (which take the integer itself as an argument, not a
94       pointer to it, as is done in wait() and waitpid()!):
95
96       WIFEXITED(wstatus)
97              returns true if the child terminated normally, that is, by call‐
98              ing exit(3) or _exit(2), or by returning from main().
99
100       WEXITSTATUS(wstatus)
101              returns  the  exit  status  of  the child.  This consists of the
102              least significant 8 bits of the status argument that  the  child
103              specified  in  a  call to exit(3) or _exit(2) or as the argument
104              for a return statement in main().  This macro should be employed
105              only if WIFEXITED returned true.
106
107       WIFSIGNALED(wstatus)
108              returns true if the child process was terminated by a signal.
109
110       WTERMSIG(wstatus)
111              returns  the  number of the signal that caused the child process
112              to terminate.  This macro should be employed only if WIFSIGNALED
113              returned true.
114
115       WCOREDUMP(wstatus)
116              returns  true  if  the child produced a core dump (see core(5)).
117              This macro should be employed only if WIFSIGNALED returned true.
118
119              This macro is not specified in POSIX.1-2001 and is not available
120              on some UNIX implementations (e.g., AIX, SunOS).  Therefore, en‐
121              close its use inside #ifdef WCOREDUMP ... #endif.
122
123       WIFSTOPPED(wstatus)
124              returns true if the child process was stopped by delivery  of  a
125              signal;  this  is  possible only if the call was done using WUN‐
126              TRACED or when the child is being traced (see ptrace(2)).
127
128       WSTOPSIG(wstatus)
129              returns the number of the signal which caused the child to stop.
130              This macro should be employed only if WIFSTOPPED returned true.
131
132       WIFCONTINUED(wstatus)
133              (since  Linux  2.6.10) returns true if the child process was re‐
134              sumed by delivery of SIGCONT.
135
136   waitid()
137       The waitid() system call (available since Linux  2.6.9)  provides  more
138       precise control over which child state changes to wait for.
139
140       The  idtype and id arguments select the child(ren) to wait for, as fol‐
141       lows:
142
143       idtype == P_PID
144              Wait for the child whose process ID matches id.
145
146       idtype == P_PIDFD (since Linux 5.4)
147              Wait for the child referred to by the PID file descriptor speci‐
148              fied  in  id.  (See pidfd_open(2) for further information on PID
149              file descriptors.)
150
151       idtype == P_PGID
152              Wait for any child whose process group  ID  matches  id.   Since
153              Linux 5.4, if id is zero, then wait for any child that is in the
154              same process group as the caller's process group at the time  of
155              the call.
156
157       idtype == P_ALL
158              Wait for any child; id is ignored.
159
160       The  child state changes to wait for are specified by ORing one or more
161       of the following flags in options:
162
163       WEXITED
164              Wait for children that have terminated.
165
166       WSTOPPED
167              Wait for children that have been stopped by delivery of  a  sig‐
168              nal.
169
170       WCONTINUED
171              Wait for (previously stopped) children that have been resumed by
172              delivery of SIGCONT.
173
174       The following flags may additionally be ORed in options:
175
176       WNOHANG
177              As for waitpid().
178
179       WNOWAIT
180              Leave the child in a waitable state; a later wait  call  can  be
181              used to again retrieve the child status information.
182
183       Upon  successful  return, waitid() fills in the following fields of the
184       siginfo_t structure pointed to by infop:
185
186       si_pid The process ID of the child.
187
188       si_uid The real user ID of the child.  (This field is not set  on  most
189              other implementations.)
190
191       si_signo
192              Always set to SIGCHLD.
193
194       si_status
195              Either  the  exit  status of the child, as given to _exit(2) (or
196              exit(3)), or the signal that  caused  the  child  to  terminate,
197              stop,  or  continue.  The si_code field can be used to determine
198              how to interpret this field.
199
200       si_code
201              Set to one of: CLD_EXITED (child  called  _exit(2));  CLD_KILLED
202              (child  killed  by  signal); CLD_DUMPED (child killed by signal,
203              and  dumped  core);  CLD_STOPPED  (child  stopped  by   signal);
204              CLD_TRAPPED  (traced child has trapped); or CLD_CONTINUED (child
205              continued by SIGCONT).
206
207       If WNOHANG was specified in options and there were  no  children  in  a
208       waitable  state,  then  waitid() returns 0 immediately and the state of
209       the siginfo_t structure pointed to by infop depends on the  implementa‐
210       tion.   To (portably) distinguish this case from that where a child was
211       in a waitable state, zero out the si_pid  field  before  the  call  and
212       check for a nonzero value in this field after the call returns.
213
214       POSIX.1-2008  Technical  Corrigendum 1 (2013) adds the requirement that
215       when WNOHANG is specified in options and there were no  children  in  a
216       waitable  state,  then waitid() should zero out the si_pid and si_signo
217       fields of the structure.  On Linux and other implementations  that  ad‐
218       here  to  this  requirement, it is not necessary to zero out the si_pid
219       field before calling waitid().  However, not all implementations follow
220       the POSIX.1 specification on this point.
221

RETURN VALUE

223       wait():  on success, returns the process ID of the terminated child; on
224       error, -1 is returned.
225
226       waitpid(): on success, returns the process ID of the child whose  state
227       has changed; if WNOHANG was specified and one or more child(ren) speci‐
228       fied by pid exist, but have not yet changed state, then 0 is  returned.
229       On error, -1 is returned.
230
231       waitid():  returns  0  on  success  or  if WNOHANG was specified and no
232       child(ren) specified by id has yet changed state; on error, -1  is  re‐
233       turned.
234
235       Each  of  these calls sets errno to an appropriate value in the case of
236       an error.
237

ERRORS

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

CONFORMING TO

254       SVr4, 4.3BSD, POSIX.1-2001.
255

NOTES

257       A child that terminates, but has not been waited for  becomes  a  "zom‐
258       bie".  The kernel maintains a minimal set of information about the zom‐
259       bie process (PID, termination status, resource  usage  information)  in
260       order to allow the parent to later perform a wait to obtain information
261       about the child.  As long as a zombie is not removed  from  the  system
262       via  a wait, it will consume a slot in the kernel process table, and if
263       this table fills, it will not be possible to create further  processes.
264       If a parent process terminates, then its "zombie" children (if any) are
265       adopted by init(1), (or by the nearest "subreaper" process  as  defined
266       through  the  use  of  the  prctl(2) PR_SET_CHILD_SUBREAPER operation);
267       init(1) automatically performs a wait to remove the zombies.
268
269       POSIX.1-2001 specifies that if the disposition of  SIGCHLD  is  set  to
270       SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)),
271       then children that terminate do not become zombies and a call to wait()
272       or  waitpid()  will  block until all children have terminated, and then
273       fail with errno set to ECHILD.  (The original POSIX standard  left  the
274       behavior  of  setting  SIGCHLD  to SIG_IGN unspecified.  Note that even
275       though the default disposition of SIGCHLD is "ignore", explicitly  set‐
276       ting  the disposition to SIG_IGN results in different treatment of zom‐
277       bie process children.)
278
279       Linux 2.6 conforms to the POSIX requirements.  However, Linux 2.4  (and
280       earlier)  does not: if a wait() or waitpid() call is made while SIGCHLD
281       is being ignored, the call behaves just as though SIGCHLD were not  be‐
282       ing  ignored,  that is, the call blocks until the next child terminates
283       and then returns the process ID and status of that child.
284
285   Linux notes
286       In the Linux kernel, a kernel-scheduled thread is not a  distinct  con‐
287       struct  from  a process.  Instead, a thread is simply a process that is
288       created using the Linux-unique clone(2)  system  call;  other  routines
289       such  as  the  portable  pthread_create(3)  call  are implemented using
290       clone(2).  Before Linux 2.4, a thread was just  a  special  case  of  a
291       process, and as a consequence one thread could not wait on the children
292       of another thread, even when the latter  belongs  to  the  same  thread
293       group.   However,  POSIX prescribes such functionality, and since Linux
294       2.4 a thread can, and by  default  will,  wait  on  children  of  other
295       threads in the same thread group.
296
297       The  following Linux-specific options are for use with children created
298       using clone(2); they can also, since Linux 4.7, be used with waitid():
299
300       __WCLONE
301              Wait for "clone" children only.  If omitted, then wait for "non-
302              clone" children only.  (A "clone" child is one which delivers no
303              signal, or a signal other than SIGCHLD to its parent upon termi‐
304              nation.)  This option is ignored if __WALL is also specified.
305
306       __WALL (since Linux 2.4)
307              Wait  for  all  children,  regardless  of type ("clone" or "non-
308              clone").
309
310       __WNOTHREAD (since Linux 2.4)
311              Do not wait for children of other threads  in  the  same  thread
312              group.  This was the default before Linux 2.4.
313
314       Since  Linux 4.7, the __WALL flag is automatically implied if the child
315       is being ptraced.
316
317   C library/kernel differences
318       wait() is actually a library function that (in glibc) is implemented as
319       a call to wait4(2).
320
321       On some architectures, there is no waitpid() system call; instead, this
322       interface is implemented via a C library wrapper  function  that  calls
323       wait4(2).
324
325       The  raw  waitid()  system  call takes a fifth argument, of type struct
326       rusage *.  If this argument is non-NULL, then it is used to return  re‐
327       source  usage  information  about  the  child,  in  the  same manner as
328       wait4(2).  See getrusage(2) for details.
329

BUGS

331       According to POSIX.1-2008, an application calling waitid() must  ensure
332       that infop points to a siginfo_t structure (i.e., that it is a non-null
333       pointer).  On Linux, if infop is NULL, waitid() succeeds,  and  returns
334       the  process ID of the waited-for child.  Applications should avoid re‐
335       lying on this inconsistent, nonstandard, and unnecessary feature.
336

EXAMPLES

338       The following program demonstrates the use of  fork(2)  and  waitpid().
339       The  program  creates  a child process.  If no command-line argument is
340       supplied to the program, then the child suspends  its  execution  using
341       pause(2),  to  allow the user to send signals to the child.  Otherwise,
342       if a command-line argument is supplied, then the  child  exits  immedi‐
343       ately,  using the integer supplied on the command line as the exit sta‐
344       tus.  The parent process executes a loop that monitors the child  using
345       waitpid(), and uses the W*() macros described above to analyze the wait
346       status value.
347
348       The following shell session demonstrates the use of the program:
349
350           $ ./a.out &
351           Child PID is 32360
352           [1] 32359
353           $ kill -STOP 32360
354           stopped by signal 19
355           $ kill -CONT 32360
356           continued
357           $ kill -TERM 32360
358           killed by signal 15
359           [1]+  Done                    ./a.out
360           $
361
362   Program source
363
364       #include <sys/wait.h>
365       #include <stdint.h>
366       #include <stdlib.h>
367       #include <unistd.h>
368       #include <stdio.h>
369
370       int
371       main(int argc, char *argv[])
372       {
373           pid_t cpid, w;
374           int wstatus;
375
376           cpid = fork();
377           if (cpid == -1) {
378               perror("fork");
379               exit(EXIT_FAILURE);
380           }
381
382           if (cpid == 0) {            /* Code executed by child */
383               printf("Child PID is %jd\n", (intmax_t) getpid());
384               if (argc == 1)
385                   pause();                    /* Wait for signals */
386               _exit(atoi(argv[1]));
387
388           } else {                    /* Code executed by parent */
389               do {
390                   w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
391                   if (w == -1) {
392                       perror("waitpid");
393                       exit(EXIT_FAILURE);
394                   }
395
396                   if (WIFEXITED(wstatus)) {
397                       printf("exited, status=%d\n", WEXITSTATUS(wstatus));
398                   } else if (WIFSIGNALED(wstatus)) {
399                       printf("killed by signal %d\n", WTERMSIG(wstatus));
400                   } else if (WIFSTOPPED(wstatus)) {
401                       printf("stopped by signal %d\n", WSTOPSIG(wstatus));
402                   } else if (WIFCONTINUED(wstatus)) {
403                       printf("continued\n");
404                   }
405               } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
406               exit(EXIT_SUCCESS);
407           }
408       }
409

SEE ALSO

411       _exit(2), clone(2), fork(2),  kill(2),  ptrace(2),  sigaction(2),  sig‐
412       nal(2), wait4(2), pthread_create(3), core(5), credentials(7), signal(7)
413

COLOPHON

415       This  page  is  part of release 5.10 of the Linux man-pages project.  A
416       description of the project, information about reporting bugs,  and  the
417       latest     version     of     this    page,    can    be    found    at
418       https://www.kernel.org/doc/man-pages/.
419
420
421
422Linux                             2020-11-01                           WAIT(2)
Impressum