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 *status);
13
14       pid_t waitpid(pid_t pid, int *status, int options);
15
16       int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
17
18   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20       waitid():
21           _SVID_SOURCE || _XOPEN_SOURCE >= 500 ||
22           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
23           || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
24

DESCRIPTION

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

RETURN VALUE

190       wait():  on success, returns the process ID of the terminated child; on
191       error, -1 is returned.
192
193       waitpid(): on success, returns the process ID of the child whose  state
194       has changed; if WNOHANG was specified and one or more child(ren) speci‐
195       fied by pid exist, but have not yet changed state, then 0 is  returned.
196       On error, -1 is returned.
197
198       waitid():  returns  0  on  success  or  if WNOHANG was specified and no
199       child(ren) specified by id has yet  changed  state;  on  error,  -1  is
200       returned.   Each  of  these calls sets errno to an appropriate value in
201       the case of an error.
202

ERRORS

204       ECHILD (for wait()) The calling process does not have any  unwaited-for
205              children.
206
207       ECHILD (for  waitpid() or waitid()) The process specified by pid (wait‐
208              pid()) or idtype and id (waitid()) does not exist or  is  not  a
209              child  of  the  calling process.  (This can happen for one's own
210              child if the action for SIGCHLD is set to SIG_IGN.  See also the
211              Linux Notes section about threads.)
212
213       EINTR  WNOHANG  was  not  set  and an unblocked signal or a SIGCHLD was
214              caught; see signal(7).
215
216       EINVAL The options argument was invalid.
217

CONFORMING TO

219       SVr4, 4.3BSD, POSIX.1-2001.
220

NOTES

222       A child that terminates, but has not been waited for  becomes  a  "zom‐
223       bie".  The kernel maintains a minimal set of information about the zom‐
224       bie process (PID, termination status, resource  usage  information)  in
225       order to allow the parent to later perform a wait to obtain information
226       about the child.  As long as a zombie is not removed  from  the  system
227       via  a wait, it will consume a slot in the kernel process table, and if
228       this table fills, it will not be possible to create further  processes.
229       If a parent process terminates, then its "zombie" children (if any) are
230       adopted by init(8), which automatically performs a wait to  remove  the
231       zombies.
232
233       POSIX.1-2001  specifies  that  if  the disposition of SIGCHLD is set to
234       SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)),
235       then children that terminate do not become zombies and a call to wait()
236       or waitpid() will block until all children have  terminated,  and  then
237       fail  with  errno set to ECHILD.  (The original POSIX standard left the
238       behavior of setting SIGCHLD to SIG_IGN  unspecified.   Note  that  even
239       though  the default disposition of SIGCHLD is "ignore", explicitly set‐
240       ting the disposition to SIG_IGN results in different treatment of  zom‐
241       bie process children.)  Linux 2.6 conforms to this specification.  How‐
242       ever, Linux 2.4 (and earlier) does not: if a wait() or  waitpid()  call
243       is made while SIGCHLD is being ignored, the call behaves just as though
244       SIGCHLD were not being ignored, that is, the call blocks until the next
245       child  terminates  and  then  returns the process ID and status of that
246       child.
247
248   Linux notes
249       In the Linux kernel, a kernel-scheduled thread is not a  distinct  con‐
250       struct  from  a process.  Instead, a thread is simply a process that is
251       created using the Linux-unique clone(2)  system  call;  other  routines
252       such  as  the  portable  pthread_create(3)  call  are implemented using
253       clone(2).  Before Linux 2.4, a thread was just  a  special  case  of  a
254       process, and as a consequence one thread could not wait on the children
255       of another thread, even when the latter  belongs  to  the  same  thread
256       group.   However,  POSIX prescribes such functionality, and since Linux
257       2.4 a thread can, and by  default  will,  wait  on  children  of  other
258       threads in the same thread group.
259
260       The  following Linux-specific options are for use with children created
261       using clone(2); they cannot be used with waitid():
262
263       __WCLONE
264              Wait for "clone" children only.  If omitted then wait for  "non-
265              clone" children only.  (A "clone" child is one which delivers no
266              signal, or a signal other than SIGCHLD to its parent upon termi‐
267              nation.)  This option is ignored if __WALL is also specified.
268
269       __WALL (since Linux 2.4)
270              Wait  for  all  children,  regardless  of type ("clone" or "non-
271              clone").
272
273       __WNOTHREAD (since Linux 2.4)
274              Do not wait for children of other threads  in  the  same  thread
275              group.  This was the default before Linux 2.4.
276

BUGS

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

EXAMPLE

285       The  following  program  demonstrates the use of fork(2) and waitpid().
286       The program creates a child process.  If no  command-line  argument  is
287       supplied  to  the  program, then the child suspends its execution using
288       pause(2), to allow the user to send signals to the  child.   Otherwise,
289       if  a  command-line  argument is supplied, then the child exits immedi‐
290       ately, using the integer supplied on the command line as the exit  sta‐
291       tus.   The parent process executes a loop that monitors the child using
292       waitpid(), and uses the W*() macros described above to analyze the wait
293       status value.
294
295       The following shell session demonstrates the use of the program:
296
297           $ ./a.out &
298           Child PID is 32360
299           [1] 32359
300           $ kill -STOP 32360
301           stopped by signal 19
302           $ kill -CONT 32360
303           continued
304           $ kill -TERM 32360
305           killed by signal 15
306           [1]+  Done                    ./a.out
307           $
308
309   Program source
310
311       #include <sys/wait.h>
312       #include <stdlib.h>
313       #include <unistd.h>
314       #include <stdio.h>
315
316       int
317       main(int argc, char *argv[])
318       {
319           pid_t cpid, w;
320           int status;
321
322           cpid = fork();
323           if (cpid == -1) {
324               perror("fork");
325               exit(EXIT_FAILURE);
326           }
327
328           if (cpid == 0) {            /* Code executed by child */
329               printf("Child PID is %ld\n", (long) getpid());
330               if (argc == 1)
331                   pause();                    /* Wait for signals */
332               _exit(atoi(argv[1]));
333
334           } else {                    /* Code executed by parent */
335               do {
336                   w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
337                   if (w == -1) {
338                       perror("waitpid");
339                       exit(EXIT_FAILURE);
340                   }
341
342                   if (WIFEXITED(status)) {
343                       printf("exited, status=%d\n", WEXITSTATUS(status));
344                   } else if (WIFSIGNALED(status)) {
345                       printf("killed by signal %d\n", WTERMSIG(status));
346                   } else if (WIFSTOPPED(status)) {
347                       printf("stopped by signal %d\n", WSTOPSIG(status));
348                   } else if (WIFCONTINUED(status)) {
349                       printf("continued\n");
350                   }
351               } while (!WIFEXITED(status) && !WIFSIGNALED(status));
352               exit(EXIT_SUCCESS);
353           }
354       }
355

SEE ALSO

357       _exit(2),  clone(2),  fork(2),  kill(2),  ptrace(2), sigaction(2), sig‐
358       nal(2), wait4(2), pthread_create(3), credentials(7), signal(7)
359

COLOPHON

361       This page is part of release 3.53 of the Linux  man-pages  project.   A
362       description  of  the project, and information about reporting bugs, can
363       be found at http://www.kernel.org/doc/man-pages/.
364
365
366
367Linux                             2012-12-21                           WAIT(2)
Impressum