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

NAME

6       wait, waitpid - 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       pid_t waitpid(pid_t pid, int *status, int options);
14       int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
15

DESCRIPTION

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

RETURN VALUE

193       wait(): on success, returns the process ID of the terminated child;  on
194       error, -1 is returned.
195
196       waitpid():  on success, returns the process ID of the child whose state
197       has changed; on error, -1 is returned; if WNOHANG was specified and  no
198       child(ren) specified by pid has yet changed state, then 0 is returned.
199
200       waitid():  returns  0  on  success  or  if WNOHANG was specified and no
201       child(ren) specified by id has yet  changed  state;  on  error,  -1  is
202       returned.
203
204       Each  of  these calls sets errno to an appropriate value in the case of
205       an error.
206

ERRORS

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

NOTES

223       A child that terminates, but has not been waited for  becomes  a  "zom‐
224       bie".  The kernel maintains a minimal set of information about the zom‐
225       bie process (PID, termination status, resource  usage  information)  in
226       order to allow the parent to later perform a wait to obtain information
227       about the child.  As long as a zombie is not removed  from  the  system
228       via  a wait, it will consume a slot in the kernel process table, and if
229       this table fills, it will not be possible to create further  processes.
230       If a parent process terminates, then its "zombie" children (if any) are
231       adopted by init(8), which automatically performs a wait to  remove  the
232       zombies.
233
234       POSIX.1-2001  specifies  that  if  the disposition of SIGCHLD is set to
235       SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)),
236       then children that terminate do not become zombies and a call to wait()
237       or waitpid() will block until all children have  terminated,  and  then
238       fail  with  errno set to ECHILD.  (The original POSIX standard left the
239       behaviour of setting SIGCHLD to SIG_IGN unspecified.)  Linux  2.6  con‐
240       forms  to  this  specification.   However, Linux 2.4 (and earlier) does
241       not: if a wait() or waitpid() call  is  made  while  SIGCHLD  is  being
242       ignored,  the  call  behaves  just  as  though  SIGCHLD  were not being
243       ignored, that is, the call blocks until the next child  terminates  and
244       then returns the process ID and status of that child.
245

LINUX NOTES

247       In  the  Linux kernel, a kernel-scheduled thread is not a distinct con‐
248       struct from a process. Instead, a thread is simply a  process  that  is
249       created  using  the  Linux-unique  clone(2) system call; other routines
250       such as the  portable  pthread_create(3)  call  are  implemented  using
251       clone(2).   Before  Linux  2.4,  a  thread was just a special case of a
252       process, and as a consequence one thread could not wait on the children
253       of  another  thread,  even  when  the latter belongs to the same thread
254       group.  However, POSIX prescribes such functionality, and  since  Linux
255       2.4  a  thread  can,  and  by  default  will, wait on children of other
256       threads in the same thread group.
257
258       The following Linux specific options are for use with children  created
259       using clone(2); they cannot be used with waitid():
260
261       __WCLONE
262              Wait  for "clone" children only.  If omitted then wait for "non-
263              clone" children only.  (A "clone" child is one which delivers no
264              signal, or a signal other than SIGCHLD to its parent upon termi‐
265              nation.)  This option is ignored if __WALL is also specified.
266
267       __WALL (Since Linux 2.4) Wait for  all  children,  regardless  of  type
268              ("clone" or "non-clone").
269
270       __WNOTHREAD
271              (Since  Linux  2.4) Do not wait for children of other threads in
272              the same thread group. This was the default before Linux 2.4.
273

EXAMPLE

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

CONFORMING TO

340       SVr4, 4.3BSD, POSIX.1-2001.
341

SEE ALSO

343       _exit(2), clone(2), fork(2),  kill(2),  ptrace(2),  sigaction(2),  sig‐
344       nal(2), wait4(2), pthread_create(3), signal(7)
345
346
347
348Linux                             2004-11-11                           WAIT(2)
Impressum