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 process  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  process
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.
69
70       > 0    meaning  wait  for  the  child  whose process ID is equal to the
71              value of pid.
72
73       The value of options is an OR of zero or more  of  the  following  con‐
74       stants:
75
76       WNOHANG     return immediately if no child has exited.
77
78       WUNTRACED   also  return  if  a  child  has stopped (but not traced via
79                   ptrace(2)).  Status for traced children which have  stopped
80                   is provided even if this option is not specified.
81
82       WCONTINUED (since Linux 2.6.10)
83                   also return if a stopped child has been resumed by delivery
84                   of SIGCONT.
85
86       (For Linux-only options, see below.)
87
88       If wstatus is not NULL, wait() and waitpid() store  status  information
89       in  the int to which it points.  This integer can be inspected with the
90       following macros (which take the integer itself as an argument,  not  a
91       pointer to it, as is done in wait() and waitpid()!):
92
93       WIFEXITED(wstatus)
94              returns true if the child terminated normally, that is, by call‐
95              ing exit(3) or _exit(2), or by returning from main().
96
97       WEXITSTATUS(wstatus)
98              returns the exit status of the  child.   This  consists  of  the
99              least  significant  8 bits of the status argument that the child
100              specified in a call to exit(3) or _exit(2) or  as  the  argument
101              for a return statement in main().  This macro should be employed
102              only if WIFEXITED returned true.
103
104       WIFSIGNALED(wstatus)
105              returns true if the child process was terminated by a signal.
106
107       WTERMSIG(wstatus)
108              returns the number of the signal that caused the  child  process
109              to terminate.  This macro should be employed only if WIFSIGNALED
110              returned true.
111
112       WCOREDUMP(wstatus)
113              returns true if the child produced  a  core  dump.   This  macro
114              should be employed only if WIFSIGNALED returned true.
115
116              This macro is not specified in POSIX.1-2001 and is not available
117              on some UNIX implementations  (e.g.,  AIX,  SunOS).   Therefore,
118              enclose its use inside #ifdef WCOREDUMP ... #endif.
119
120       WIFSTOPPED(wstatus)
121              returns  true  if the child process was stopped by delivery of a
122              signal; this is possible only if the call was  done  using  WUN‐
123              TRACED or when the child is being traced (see ptrace(2)).
124
125       WSTOPSIG(wstatus)
126              returns the number of the signal which caused the child to stop.
127              This macro should be employed only if WIFSTOPPED returned true.
128
129       WIFCONTINUED(wstatus)
130              (since Linux 2.6.10) returns  true  if  the  child  process  was
131              resumed by delivery of SIGCONT.
132
133   waitid()
134       The  waitid()  system  call (available since Linux 2.6.9) provides more
135       precise control over which child state changes to wait for.
136
137       The idtype and id arguments select the child(ren) to wait for, as  fol‐
138       lows:
139
140       idtype == P_PID
141              Wait for the child whose process ID matches id.
142
143       idtype == P_PGID
144              Wait for any child whose process group ID matches id.
145
146       idtype == P_ALL
147              Wait for any child; id is ignored.
148
149       The  child state changes to wait for are specified by ORing one or more
150       of the following flags in options:
151
152       WEXITED     Wait for children that have terminated.
153
154       WSTOPPED    Wait for children that have been stopped by delivery  of  a
155                   signal.
156
157       WCONTINUED  Wait  for  (previously  stopped)  children  that  have been
158                   resumed by delivery of SIGCONT.
159
160       The following flags may additionally be ORed in options:
161
162       WNOHANG     As for waitpid().
163
164       WNOWAIT     Leave the child in a waitable state; a later wait call  can
165                   be used to again retrieve the child status information.
166
167       Upon  successful  return, waitid() fills in the following fields of the
168       siginfo_t structure pointed to by infop:
169
170       si_pid      The process ID of the child.
171
172       si_uid      The real user ID of the child.  (This field is not  set  on
173                   most other implementations.)
174
175       si_signo    Always set to SIGCHLD.
176
177       si_status   Either  the  exit status of the child, as given to _exit(2)
178                   (or exit(3)), or the signal that caused the child to termi‐
179                   nate,  stop, or continue.  The si_code field can be used to
180                   determine how to interpret this field.
181
182       si_code     Set  to  one  of:  CLD_EXITED  (child   called   _exit(2));
183                   CLD_KILLED  (child  killed  by  signal);  CLD_DUMPED (child
184                   killed by signal,  and  dumped  core);  CLD_STOPPED  (child
185                   stopped by signal); CLD_TRAPPED (traced child has trapped);
186                   or CLD_CONTINUED (child continued by SIGCONT).
187
188       If WNOHANG was specified in options and there were  no  children  in  a
189       waitable  state,  then  waitid() returns 0 immediately and the state of
190       the siginfo_t structure pointed to by infop depends on the  implementa‐
191       tion.   To (portably) distinguish this case from that where a child was
192       in a waitable state, zero out the si_pid  field  before  the  call  and
193       check for a nonzero value in this field after the call returns.
194
195       POSIX.1-2008  Technical  Corrigendum 1 (2013) adds the requirement that
196       when WNOHANG is specified in options and there were no  children  in  a
197       waitable  state,  then waitid() should zero out the si_pid and si_signo
198       fields of the structure.   On  Linux  and  other  implementations  that
199       adhere  to this requirement, it is not necessary to zero out the si_pid
200       field before calling waitid().  However, not all implementations follow
201       the POSIX.1 specification on this point.
202

RETURN VALUE

204       wait():  on success, returns the process ID of the terminated child; on
205       error, -1 is returned.
206
207       waitpid(): on success, returns the process ID of the child whose  state
208       has changed; if WNOHANG was specified and one or more child(ren) speci‐
209       fied by pid exist, but have not yet changed state, then 0 is  returned.
210       On error, -1 is returned.
211
212       waitid():  returns  0  on  success  or  if WNOHANG was specified and no
213       child(ren) specified by id has yet  changed  state;  on  error,  -1  is
214       returned.
215
216       Each  of  these calls sets errno to an appropriate value in the case of
217       an error.
218

ERRORS

220       ECHILD (for wait()) The calling process does not have any  unwaited-for
221              children.
222
223       ECHILD (for  waitpid() or waitid()) The process specified by pid (wait‐
224              pid()) or idtype and id (waitid()) does not exist or  is  not  a
225              child  of  the  calling process.  (This can happen for one's own
226              child if the action for SIGCHLD is set to SIG_IGN.  See also the
227              Linux Notes section about threads.)
228
229       EINTR  WNOHANG  was  not  set  and an unblocked signal or a SIGCHLD was
230              caught; see signal(7).
231
232       EINVAL The options argument was invalid.
233

CONFORMING TO

235       SVr4, 4.3BSD, POSIX.1-2001.
236

NOTES

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

BUGS

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

EXAMPLE

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

SEE ALSO

391       _exit(2), clone(2), fork(2),  kill(2),  ptrace(2),  sigaction(2),  sig‐
392       nal(2), wait4(2), pthread_create(3), credentials(7), signal(7)
393

COLOPHON

395       This  page  is  part of release 4.15 of the Linux man-pages project.  A
396       description of the project, information about reporting bugs,  and  the
397       latest     version     of     this    page,    can    be    found    at
398       https://www.kernel.org/doc/man-pages/.
399
400
401
402Linux                             2017-09-15                           WAIT(2)
Impressum