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
56       until 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     return immediately if no child has exited.
78
79       WUNTRACED   also return if a child has  stopped  (but  not  traced  via
80                   ptrace(2)).   Status for traced children which have stopped
81                   is 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
85                   of 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,
119              enclose 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
132              resumed 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     Wait for children that have terminated.
162
163       WSTOPPED    Wait for children that have been stopped by delivery  of  a
164                   signal.
165
166       WCONTINUED  Wait  for  (previously  stopped)  children  that  have been
167                   resumed by delivery of SIGCONT.
168
169       The following flags may additionally be ORed in options:
170
171       WNOHANG     As for waitpid().
172
173       WNOWAIT     Leave the child in a waitable state; a later wait call  can
174                   be used to again retrieve the child status information.
175
176       Upon  successful  return, waitid() fills in the following fields of the
177       siginfo_t structure pointed to by infop:
178
179       si_pid      The process ID of the child.
180
181       si_uid      The real user ID of the child.  (This field is not  set  on
182                   most other implementations.)
183
184       si_signo    Always set to SIGCHLD.
185
186       si_status   Either  the  exit status of the child, as given to _exit(2)
187                   (or exit(3)), or the signal that caused the child to termi‐
188                   nate,  stop, or continue.  The si_code field can be used to
189                   determine how to interpret this field.
190
191       si_code     Set  to  one  of:  CLD_EXITED  (child   called   _exit(2));
192                   CLD_KILLED  (child  killed  by  signal);  CLD_DUMPED (child
193                   killed by signal,  and  dumped  core);  CLD_STOPPED  (child
194                   stopped by signal); CLD_TRAPPED (traced child has trapped);
195                   or CLD_CONTINUED (child continued by SIGCONT).
196
197       If WNOHANG was specified in options and there were  no  children  in  a
198       waitable  state,  then  waitid() returns 0 immediately and the state of
199       the siginfo_t structure pointed to by infop depends on the  implementa‐
200       tion.   To (portably) distinguish this case from that where a child was
201       in a waitable state, zero out the si_pid  field  before  the  call  and
202       check for a nonzero value in this field after the call returns.
203
204       POSIX.1-2008  Technical  Corrigendum 1 (2013) adds the requirement that
205       when WNOHANG is specified in options and there were no  children  in  a
206       waitable  state,  then waitid() should zero out the si_pid and si_signo
207       fields of the structure.   On  Linux  and  other  implementations  that
208       adhere  to this requirement, it is not necessary to zero out the si_pid
209       field before calling waitid().  However, not all implementations follow
210       the POSIX.1 specification on this point.
211

RETURN VALUE

213       wait():  on success, returns the process ID of the terminated child; on
214       error, -1 is returned.
215
216       waitpid(): on success, returns the process ID of the child whose  state
217       has changed; if WNOHANG was specified and one or more child(ren) speci‐
218       fied by pid exist, but have not yet changed state, then 0 is  returned.
219       On error, -1 is returned.
220
221       waitid():  returns  0  on  success  or  if WNOHANG was specified and no
222       child(ren) specified by id has yet  changed  state;  on  error,  -1  is
223       returned.
224
225       Each  of  these calls sets errno to an appropriate value in the case of
226       an error.
227

ERRORS

229       ECHILD (for wait()) The calling process does not have any  unwaited-for
230              children.
231
232       ECHILD (for  waitpid() or waitid()) The process specified by pid (wait‐
233              pid()) or idtype and id (waitid()) does not exist or  is  not  a
234              child  of  the  calling process.  (This can happen for one's own
235              child if the action for SIGCHLD is set to SIG_IGN.  See also the
236              Linux Notes section about threads.)
237
238       EINTR  WNOHANG  was  not  set  and an unblocked signal or a SIGCHLD was
239              caught; see signal(7).
240
241       EINVAL The options argument was invalid.
242

CONFORMING TO

244       SVr4, 4.3BSD, POSIX.1-2001.
245

NOTES

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

BUGS

321       According to POSIX.1-2008, an application calling waitid() must  ensure
322       that infop points to a siginfo_t structure (i.e., that it is a non-null
323       pointer).  On Linux, if infop is NULL, waitid() succeeds,  and  returns
324       the  process  ID  of  the  waited-for child.  Applications should avoid
325       relying on this inconsistent, nonstandard, and unnecessary feature.
326

EXAMPLE

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

SEE ALSO

400       _exit(2), clone(2), fork(2),  kill(2),  ptrace(2),  sigaction(2),  sig‐
401       nal(2), wait4(2), pthread_create(3), core(5), credentials(7), signal(7)
402

COLOPHON

404       This  page  is  part of release 5.04 of the Linux man-pages project.  A
405       description of the project, information about reporting bugs,  and  the
406       latest     version     of     this    page,    can    be    found    at
407       https://www.kernel.org/doc/man-pages/.
408
409
410
411Linux                             2019-11-19                           WAIT(2)
Impressum