]> git.ipfire.org Git - thirdparty/glibc.git/blob - manual/process.texi
Update.
[thirdparty/glibc.git] / manual / process.texi
1 @node Processes
2 @chapter Processes
3
4 @cindex process
5 @dfn{Processes} are the primitive units for allocation of system
6 resources. Each process has its own address space and (usually) one
7 thread of control. A process executes a program; you can have multiple
8 processes executing the same program, but each process has its own copy
9 of the program within its own address space and executes it
10 independently of the other copies.
11
12 @cindex child process
13 @cindex parent process
14 Processes are organized hierarchically. Each process has a @dfn{parent
15 process} which explicitly arranged to create it. The processes created
16 by a given parent are called its @dfn{child processes}. A child
17 inherits many of its attributes from the parent process.
18
19 This chapter describes how a program can create, terminate, and control
20 child processes. Actually, there are three distinct operations
21 involved: creating a new child process, causing the new process to
22 execute a program, and coordinating the completion of the child process
23 with the original program.
24
25 The @code{system} function provides a simple, portable mechanism for
26 running another program; it does all three steps automatically. If you
27 need more control over the details of how this is done, you can use the
28 primitive functions to do each step individually instead.
29
30 @menu
31 * Running a Command:: The easy way to run another program.
32 * Process Creation Concepts:: An overview of the hard way to do it.
33 * Process Identification:: How to get the process ID of a process.
34 * Creating a Process:: How to fork a child process.
35 * Executing a File:: How to make a process execute another program.
36 * Process Completion:: How to tell when a child process has completed.
37 * Process Completion Status:: How to interpret the status value
38 returned from a child process.
39 * BSD Wait Functions:: More functions, for backward compatibility.
40 * Process Creation Example:: A complete example program.
41 @end menu
42
43
44 @node Running a Command
45 @section Running a Command
46 @cindex running a command
47
48 The easy way to run another program is to use the @code{system}
49 function. This function does all the work of running a subprogram, but
50 it doesn't give you much control over the details: you have to wait
51 until the subprogram terminates before you can do anything else.
52
53 @comment stdlib.h
54 @comment ISO
55 @deftypefun int system (const char *@var{command})
56 @pindex sh
57 This function executes @var{command} as a shell command. In the GNU C
58 library, it always uses the default shell @code{sh} to run the command.
59 In particular, it searches the directories in @code{PATH} to find
60 programs to execute. The return value is @code{-1} if it wasn't
61 possible to create the shell process, and otherwise is the status of the
62 shell process. @xref{Process Completion}, for details on how this
63 status code can be interpreted.
64
65 If the @var{command} argument is a null pointer a non-zero return value
66 indicates that a command processor is available and this function can be
67 used at all.
68
69 This function is a cancelation point in multi-threaded programs. This
70 is a problem if the thread allocates some resources (like memory, file
71 descriptors, semaphores or whatever) at the time @code{system} is
72 called. If the thread gets canceled these resources stay allocated
73 until the program ends. To avoid this calls to @code{system} should be
74 protected using cancelation handlers.
75 @c ref pthread_cleanup_push / pthread_cleanup_pop
76
77 @pindex stdlib.h
78 The @code{system} function is declared in the header file
79 @file{stdlib.h}.
80 @end deftypefun
81
82 @strong{Portability Note:} Some C implementations may not have any
83 notion of a command processor that can execute other programs. You can
84 determine whether a command processor exists by executing
85 @w{@code{system (NULL)}}; if the return value is nonzero, a command
86 processor is available.
87
88 The @code{popen} and @code{pclose} functions (@pxref{Pipe to a
89 Subprocess}) are closely related to the @code{system} function. They
90 allow the parent process to communicate with the standard input and
91 output channels of the command being executed.
92
93 @node Process Creation Concepts
94 @section Process Creation Concepts
95
96 This section gives an overview of processes and of the steps involved in
97 creating a process and making it run another program.
98
99 @cindex process ID
100 @cindex process lifetime
101 Each process is named by a @dfn{process ID} number. A unique process ID
102 is allocated to each process when it is created. The @dfn{lifetime} of
103 a process ends when its termination is reported to its parent process;
104 at that time, all of the process resources, including its process ID,
105 are freed.
106
107 @cindex creating a process
108 @cindex forking a process
109 @cindex child process
110 @cindex parent process
111 Processes are created with the @code{fork} system call (so the operation
112 of creating a new process is sometimes called @dfn{forking} a process).
113 The @dfn{child process} created by @code{fork} is a copy of the original
114 @dfn{parent process}, except that it has its own process ID.
115
116 After forking a child process, both the parent and child processes
117 continue to execute normally. If you want your program to wait for a
118 child process to finish executing before continuing, you must do this
119 explicitly after the fork operation, by calling @code{wait} or
120 @code{waitpid} (@pxref{Process Completion}). These functions give you
121 limited information about why the child terminated---for example, its
122 exit status code.
123
124 A newly forked child process continues to execute the same program as
125 its parent process, at the point where the @code{fork} call returns.
126 You can use the return value from @code{fork} to tell whether the program
127 is running in the parent process or the child.
128
129 @cindex process image
130 Having several processes run the same program is only occasionally
131 useful. But the child can execute another program using one of the
132 @code{exec} functions; see @ref{Executing a File}. The program that the
133 process is executing is called its @dfn{process image}. Starting
134 execution of a new program causes the process to forget all about its
135 previous process image; when the new program exits, the process exits
136 too, instead of returning to the previous process image.
137
138 @node Process Identification
139 @section Process Identification
140
141 The @code{pid_t} data type represents process IDs. You can get the
142 process ID of a process by calling @code{getpid}. The function
143 @code{getppid} returns the process ID of the parent of the current
144 process (this is also known as the @dfn{parent process ID}). Your
145 program should include the header files @file{unistd.h} and
146 @file{sys/types.h} to use these functions.
147 @pindex sys/types.h
148 @pindex unistd.h
149
150 @comment sys/types.h
151 @comment POSIX.1
152 @deftp {Data Type} pid_t
153 The @code{pid_t} data type is a signed integer type which is capable
154 of representing a process ID. In the GNU library, this is an @code{int}.
155 @end deftp
156
157 @comment unistd.h
158 @comment POSIX.1
159 @deftypefun pid_t getpid (void)
160 The @code{getpid} function returns the process ID of the current process.
161 @end deftypefun
162
163 @comment unistd.h
164 @comment POSIX.1
165 @deftypefun pid_t getppid (void)
166 The @code{getppid} function returns the process ID of the parent of the
167 current process.
168 @end deftypefun
169
170 @node Creating a Process
171 @section Creating a Process
172
173 The @code{fork} function is the primitive for creating a process.
174 It is declared in the header file @file{unistd.h}.
175 @pindex unistd.h
176
177 @comment unistd.h
178 @comment POSIX.1
179 @deftypefun pid_t fork (void)
180 The @code{fork} function creates a new process.
181
182 If the operation is successful, there are then both parent and child
183 processes and both see @code{fork} return, but with different values: it
184 returns a value of @code{0} in the child process and returns the child's
185 process ID in the parent process.
186
187 If process creation failed, @code{fork} returns a value of @code{-1} in
188 the parent process. The following @code{errno} error conditions are
189 defined for @code{fork}:
190
191 @table @code
192 @item EAGAIN
193 There aren't enough system resources to create another process, or the
194 user already has too many processes running. This means exceeding the
195 @code{RLIMIT_NPROC} resource limit, which can usually be increased;
196 @pxref{Limits on Resources}.
197
198 @item ENOMEM
199 The process requires more space than the system can supply.
200 @end table
201 @end deftypefun
202
203 The specific attributes of the child process that differ from the
204 parent process are:
205
206 @itemize @bullet
207 @item
208 The child process has its own unique process ID.
209
210 @item
211 The parent process ID of the child process is the process ID of its
212 parent process.
213
214 @item
215 The child process gets its own copies of the parent process's open file
216 descriptors. Subsequently changing attributes of the file descriptors
217 in the parent process won't affect the file descriptors in the child,
218 and vice versa. @xref{Control Operations}. However, the file position
219 associated with each descriptor is shared by both processes;
220 @pxref{File Position}.
221
222 @item
223 The elapsed processor times for the child process are set to zero;
224 see @ref{Processor Time}.
225
226 @item
227 The child doesn't inherit file locks set by the parent process.
228 @c !!! flock locks shared
229 @xref{Control Operations}.
230
231 @item
232 The child doesn't inherit alarms set by the parent process.
233 @xref{Setting an Alarm}.
234
235 @item
236 The set of pending signals (@pxref{Delivery of Signal}) for the child
237 process is cleared. (The child process inherits its mask of blocked
238 signals and signal actions from the parent process.)
239 @end itemize
240
241
242 @comment unistd.h
243 @comment BSD
244 @deftypefun pid_t vfork (void)
245 The @code{vfork} function is similar to @code{fork} but on systems it
246 is more efficient; however, there are restrictions you must follow to
247 use it safely.
248
249 While @code{fork} makes a complete copy of the calling process's
250 address space and allows both the parent and child to execute
251 independently, @code{vfork} does not make this copy. Instead, the
252 child process created with @code{vfork} shares its parent's address
253 space until it calls exits or one of the @code{exec} functions. In the
254 meantime, the parent process suspends execution.
255
256 You must be very careful not to allow the child process created with
257 @code{vfork} to modify any global data or even local variables shared
258 with the parent. Furthermore, the child process cannot return from (or
259 do a long jump out of) the function that called @code{vfork}! This
260 would leave the parent process's control information very confused. If
261 in doubt, use @code{fork} instead.
262
263 Some operating systems don't really implement @code{vfork}. The GNU C
264 library permits you to use @code{vfork} on all systems, but actually
265 executes @code{fork} if @code{vfork} isn't available. If you follow
266 the proper precautions for using @code{vfork}, your program will still
267 work even if the system uses @code{fork} instead.
268 @end deftypefun
269
270 @node Executing a File
271 @section Executing a File
272 @cindex executing a file
273 @cindex @code{exec} functions
274
275 This section describes the @code{exec} family of functions, for executing
276 a file as a process image. You can use these functions to make a child
277 process execute a new program after it has been forked.
278
279 @pindex unistd.h
280 The functions in this family differ in how you specify the arguments,
281 but otherwise they all do the same thing. They are declared in the
282 header file @file{unistd.h}.
283
284 @comment unistd.h
285 @comment POSIX.1
286 @deftypefun int execv (const char *@var{filename}, char *const @var{argv}@t{[]})
287 The @code{execv} function executes the file named by @var{filename} as a
288 new process image.
289
290 The @var{argv} argument is an array of null-terminated strings that is
291 used to provide a value for the @code{argv} argument to the @code{main}
292 function of the program to be executed. The last element of this array
293 must be a null pointer. By convention, the first element of this array
294 is the file name of the program sans directory names. @xref{Program
295 Arguments}, for full details on how programs can access these arguments.
296
297 The environment for the new process image is taken from the
298 @code{environ} variable of the current process image; see
299 @ref{Environment Variables}, for information about environments.
300 @end deftypefun
301
302 @comment unistd.h
303 @comment POSIX.1
304 @deftypefun int execl (const char *@var{filename}, const char *@var{arg0}, @dots{})
305 This is similar to @code{execv}, but the @var{argv} strings are
306 specified individually instead of as an array. A null pointer must be
307 passed as the last such argument.
308 @end deftypefun
309
310 @comment unistd.h
311 @comment POSIX.1
312 @deftypefun int execve (const char *@var{filename}, char *const @var{argv}@t{[]}, char *const @var{env}@t{[]})
313 This is similar to @code{execv}, but permits you to specify the environment
314 for the new program explicitly as the @var{env} argument. This should
315 be an array of strings in the same format as for the @code{environ}
316 variable; see @ref{Environment Access}.
317 @end deftypefun
318
319 @comment unistd.h
320 @comment POSIX.1
321 @deftypefun int execle (const char *@var{filename}, const char *@var{arg0}, char *const @var{env}@t{[]}, @dots{})
322 This is similar to @code{execl}, but permits you to specify the
323 environment for the new program explicitly. The environment argument is
324 passed following the null pointer that marks the last @var{argv}
325 argument, and should be an array of strings in the same format as for
326 the @code{environ} variable.
327 @end deftypefun
328
329 @comment unistd.h
330 @comment POSIX.1
331 @deftypefun int execvp (const char *@var{filename}, char *const @var{argv}@t{[]})
332 The @code{execvp} function is similar to @code{execv}, except that it
333 searches the directories listed in the @code{PATH} environment variable
334 (@pxref{Standard Environment}) to find the full file name of a
335 file from @var{filename} if @var{filename} does not contain a slash.
336
337 This function is useful for executing system utility programs, because
338 it looks for them in the places that the user has chosen. Shells use it
339 to run the commands that users type.
340 @end deftypefun
341
342 @comment unistd.h
343 @comment POSIX.1
344 @deftypefun int execlp (const char *@var{filename}, const char *@var{arg0}, @dots{})
345 This function is like @code{execl}, except that it performs the same
346 file name searching as the @code{execvp} function.
347 @end deftypefun
348
349 The size of the argument list and environment list taken together must
350 not be greater than @code{ARG_MAX} bytes. @xref{General Limits}. In
351 the GNU system, the size (which compares against @code{ARG_MAX})
352 includes, for each string, the number of characters in the string, plus
353 the size of a @code{char *}, plus one, rounded up to a multiple of the
354 size of a @code{char *}. Other systems may have somewhat different
355 rules for counting.
356
357 These functions normally don't return, since execution of a new program
358 causes the currently executing program to go away completely. A value
359 of @code{-1} is returned in the event of a failure. In addition to the
360 usual file name errors (@pxref{File Name Errors}), the following
361 @code{errno} error conditions are defined for these functions:
362
363 @table @code
364 @item E2BIG
365 The combined size of the new program's argument list and environment
366 list is larger than @code{ARG_MAX} bytes. The GNU system has no
367 specific limit on the argument list size, so this error code cannot
368 result, but you may get @code{ENOMEM} instead if the arguments are too
369 big for available memory.
370
371 @item ENOEXEC
372 The specified file can't be executed because it isn't in the right format.
373
374 @item ENOMEM
375 Executing the specified file requires more storage than is available.
376 @end table
377
378 If execution of the new file succeeds, it updates the access time field
379 of the file as if the file had been read. @xref{File Times}, for more
380 details about access times of files.
381
382 The point at which the file is closed again is not specified, but
383 is at some point before the process exits or before another process
384 image is executed.
385
386 Executing a new process image completely changes the contents of memory,
387 copying only the argument and environment strings to new locations. But
388 many other attributes of the process are unchanged:
389
390 @itemize @bullet
391 @item
392 The process ID and the parent process ID. @xref{Process Creation Concepts}.
393
394 @item
395 Session and process group membership. @xref{Concepts of Job Control}.
396
397 @item
398 Real user ID and group ID, and supplementary group IDs. @xref{Process
399 Persona}.
400
401 @item
402 Pending alarms. @xref{Setting an Alarm}.
403
404 @item
405 Current working directory and root directory. @xref{Working
406 Directory}. In the GNU system, the root directory is not copied when
407 executing a setuid program; instead the system default root directory
408 is used for the new program.
409
410 @item
411 File mode creation mask. @xref{Setting Permissions}.
412
413 @item
414 Process signal mask; see @ref{Process Signal Mask}.
415
416 @item
417 Pending signals; see @ref{Blocking Signals}.
418
419 @item
420 Elapsed processor time associated with the process; see @ref{Processor Time}.
421 @end itemize
422
423 If the set-user-ID and set-group-ID mode bits of the process image file
424 are set, this affects the effective user ID and effective group ID
425 (respectively) of the process. These concepts are discussed in detail
426 in @ref{Process Persona}.
427
428 Signals that are set to be ignored in the existing process image are
429 also set to be ignored in the new process image. All other signals are
430 set to the default action in the new process image. For more
431 information about signals, see @ref{Signal Handling}.
432
433 File descriptors open in the existing process image remain open in the
434 new process image, unless they have the @code{FD_CLOEXEC}
435 (close-on-exec) flag set. The files that remain open inherit all
436 attributes of the open file description from the existing process image,
437 including file locks. File descriptors are discussed in @ref{Low-Level I/O}.
438
439 Streams, by contrast, cannot survive through @code{exec} functions,
440 because they are located in the memory of the process itself. The new
441 process image has no streams except those it creates afresh. Each of
442 the streams in the pre-@code{exec} process image has a descriptor inside
443 it, and these descriptors do survive through @code{exec} (provided that
444 they do not have @code{FD_CLOEXEC} set). The new process image can
445 reconnect these to new streams using @code{fdopen} (@pxref{Descriptors
446 and Streams}).
447
448 @node Process Completion
449 @section Process Completion
450 @cindex process completion
451 @cindex waiting for completion of child process
452 @cindex testing exit status of child process
453
454 The functions described in this section are used to wait for a child
455 process to terminate or stop, and determine its status. These functions
456 are declared in the header file @file{sys/wait.h}.
457 @pindex sys/wait.h
458
459 @comment sys/wait.h
460 @comment POSIX.1
461 @deftypefun pid_t waitpid (pid_t @var{pid}, int *@var{status-ptr}, int @var{options})
462 The @code{waitpid} function is used to request status information from a
463 child process whose process ID is @var{pid}. Normally, the calling
464 process is suspended until the child process makes status information
465 available by terminating.
466
467 Other values for the @var{pid} argument have special interpretations. A
468 value of @code{-1} or @code{WAIT_ANY} requests status information for
469 any child process; a value of @code{0} or @code{WAIT_MYPGRP} requests
470 information for any child process in the same process group as the
471 calling process; and any other negative value @minus{} @var{pgid}
472 requests information for any child process whose process group ID is
473 @var{pgid}.
474
475 If status information for a child process is available immediately, this
476 function returns immediately without waiting. If more than one eligible
477 child process has status information available, one of them is chosen
478 randomly, and its status is returned immediately. To get the status
479 from the other eligible child processes, you need to call @code{waitpid}
480 again.
481
482 The @var{options} argument is a bit mask. Its value should be the
483 bitwise OR (that is, the @samp{|} operator) of zero or more of the
484 @code{WNOHANG} and @code{WUNTRACED} flags. You can use the
485 @code{WNOHANG} flag to indicate that the parent process shouldn't wait;
486 and the @code{WUNTRACED} flag to request status information from stopped
487 processes as well as processes that have terminated.
488
489 The status information from the child process is stored in the object
490 that @var{status-ptr} points to, unless @var{status-ptr} is a null pointer.
491
492 This function is a cancelation point in multi-threaded programs. This
493 is a problem if the thread allocates some resources (like memory, file
494 descriptors, semaphores or whatever) at the time @code{waitpid} is
495 called. If the thread gets canceled these resources stay allocated
496 until the program ends. To avoid this calls to @code{waitpid} should be
497 protected using cancelation handlers.
498 @c ref pthread_cleanup_push / pthread_cleanup_pop
499
500 The return value is normally the process ID of the child process whose
501 status is reported. If there are child processes but none of them is
502 waiting to be noticed, @code{waitpid} will block until one is. However,
503 if the @code{WNOHANG} option was specified, @code{waitpid} will return
504 zero instead of blocking.
505
506 If a specific PID to wait for was given to @code{waitpid}, it will
507 ignore all other children (if any). Therefore if there are children
508 waiting to be noticed but the child whose PID was specified is not one
509 of them, @code{waitpid} will block or return zero as described above.
510
511 A value of @code{-1} is returned in case of error. The following
512 @code{errno} error conditions are defined for this function:
513
514 @table @code
515 @item EINTR
516 The function was interrupted by delivery of a signal to the calling
517 process. @xref{Interrupted Primitives}.
518
519 @item ECHILD
520 There are no child processes to wait for, or the specified @var{pid}
521 is not a child of the calling process.
522
523 @item EINVAL
524 An invalid value was provided for the @var{options} argument.
525 @end table
526 @end deftypefun
527
528 These symbolic constants are defined as values for the @var{pid} argument
529 to the @code{waitpid} function.
530
531 @comment Extra blank lines make it look better.
532 @table @code
533 @item WAIT_ANY
534
535 This constant macro (whose value is @code{-1}) specifies that
536 @code{waitpid} should return status information about any child process.
537
538
539 @item WAIT_MYPGRP
540 This constant (with value @code{0}) specifies that @code{waitpid} should
541 return status information about any child process in the same process
542 group as the calling process.
543 @end table
544
545 These symbolic constants are defined as flags for the @var{options}
546 argument to the @code{waitpid} function. You can bitwise-OR the flags
547 together to obtain a value to use as the argument.
548
549 @table @code
550 @item WNOHANG
551
552 This flag specifies that @code{waitpid} should return immediately
553 instead of waiting, if there is no child process ready to be noticed.
554
555 @item WUNTRACED
556
557 This flag specifies that @code{waitpid} should report the status of any
558 child processes that have been stopped as well as those that have
559 terminated.
560 @end table
561
562 @comment sys/wait.h
563 @comment POSIX.1
564 @deftypefun pid_t wait (int *@var{status-ptr})
565 This is a simplified version of @code{waitpid}, and is used to wait
566 until any one child process terminates. The call:
567
568 @smallexample
569 wait (&status)
570 @end smallexample
571
572 @noindent
573 is exactly equivalent to:
574
575 @smallexample
576 waitpid (-1, &status, 0)
577 @end smallexample
578
579 This function is a cancelation point in multi-threaded programs. This
580 is a problem if the thread allocates some resources (like memory, file
581 descriptors, semaphores or whatever) at the time @code{wait} is
582 called. If the thread gets canceled these resources stay allocated
583 until the program ends. To avoid this calls to @code{wait} should be
584 protected using cancelation handlers.
585 @c ref pthread_cleanup_push / pthread_cleanup_pop
586 @end deftypefun
587
588 @comment sys/wait.h
589 @comment BSD
590 @deftypefun pid_t wait4 (pid_t @var{pid}, int *@var{status-ptr}, int @var{options}, struct rusage *@var{usage})
591 If @var{usage} is a null pointer, @code{wait4} is equivalent to
592 @code{waitpid (@var{pid}, @var{status-ptr}, @var{options})}.
593
594 If @var{usage} is not null, @code{wait4} stores usage figures for the
595 child process in @code{*@var{rusage}} (but only if the child has
596 terminated, not if it has stopped). @xref{Resource Usage}.
597
598 This function is a BSD extension.
599 @end deftypefun
600
601 Here's an example of how to use @code{waitpid} to get the status from
602 all child processes that have terminated, without ever waiting. This
603 function is designed to be a handler for @code{SIGCHLD}, the signal that
604 indicates that at least one child process has terminated.
605
606 @smallexample
607 @group
608 void
609 sigchld_handler (int signum)
610 @{
611 int pid;
612 int status;
613 while (1)
614 @{
615 pid = waitpid (WAIT_ANY, &status, WNOHANG);
616 if (pid < 0)
617 @{
618 perror ("waitpid");
619 break;
620 @}
621 if (pid == 0)
622 break;
623 notice_termination (pid, status);
624 @}
625 @}
626 @end group
627 @end smallexample
628
629 @node Process Completion Status
630 @section Process Completion Status
631
632 If the exit status value (@pxref{Program Termination}) of the child
633 process is zero, then the status value reported by @code{waitpid} or
634 @code{wait} is also zero. You can test for other kinds of information
635 encoded in the returned status value using the following macros.
636 These macros are defined in the header file @file{sys/wait.h}.
637 @pindex sys/wait.h
638
639 @comment sys/wait.h
640 @comment POSIX.1
641 @deftypefn Macro int WIFEXITED (int @var{status})
642 This macro returns a nonzero value if the child process terminated
643 normally with @code{exit} or @code{_exit}.
644 @end deftypefn
645
646 @comment sys/wait.h
647 @comment POSIX.1
648 @deftypefn Macro int WEXITSTATUS (int @var{status})
649 If @code{WIFEXITED} is true of @var{status}, this macro returns the
650 low-order 8 bits of the exit status value from the child process.
651 @xref{Exit Status}.
652 @end deftypefn
653
654 @comment sys/wait.h
655 @comment POSIX.1
656 @deftypefn Macro int WIFSIGNALED (int @var{status})
657 This macro returns a nonzero value if the child process terminated
658 because it received a signal that was not handled.
659 @xref{Signal Handling}.
660 @end deftypefn
661
662 @comment sys/wait.h
663 @comment POSIX.1
664 @deftypefn Macro int WTERMSIG (int @var{status})
665 If @code{WIFSIGNALED} is true of @var{status}, this macro returns the
666 signal number of the signal that terminated the child process.
667 @end deftypefn
668
669 @comment sys/wait.h
670 @comment BSD
671 @deftypefn Macro int WCOREDUMP (int @var{status})
672 This macro returns a nonzero value if the child process terminated
673 and produced a core dump.
674 @end deftypefn
675
676 @comment sys/wait.h
677 @comment POSIX.1
678 @deftypefn Macro int WIFSTOPPED (int @var{status})
679 This macro returns a nonzero value if the child process is stopped.
680 @end deftypefn
681
682 @comment sys/wait.h
683 @comment POSIX.1
684 @deftypefn Macro int WSTOPSIG (int @var{status})
685 If @code{WIFSTOPPED} is true of @var{status}, this macro returns the
686 signal number of the signal that caused the child process to stop.
687 @end deftypefn
688
689
690 @node BSD Wait Functions
691 @section BSD Process Wait Functions
692
693 The GNU library also provides these related facilities for compatibility
694 with BSD Unix. BSD uses the @code{union wait} data type to represent
695 status values rather than an @code{int}. The two representations are
696 actually interchangeable; they describe the same bit patterns. The GNU
697 C Library defines macros such as @code{WEXITSTATUS} so that they will
698 work on either kind of object, and the @code{wait} function is defined
699 to accept either type of pointer as its @var{status-ptr} argument.
700
701 These functions are declared in @file{sys/wait.h}.
702 @pindex sys/wait.h
703
704 @comment sys/wait.h
705 @comment BSD
706 @deftp {Data Type} {union wait}
707 This data type represents program termination status values. It has
708 the following members:
709
710 @table @code
711 @item int w_termsig
712 The value of this member is the same as the result of the
713 @code{WTERMSIG} macro.
714
715 @item int w_coredump
716 The value of this member is the same as the result of the
717 @code{WCOREDUMP} macro.
718
719 @item int w_retcode
720 The value of this member is the same as the result of the
721 @code{WEXITSTATUS} macro.
722
723 @item int w_stopsig
724 The value of this member is the same as the result of the
725 @code{WSTOPSIG} macro.
726 @end table
727
728 Instead of accessing these members directly, you should use the
729 equivalent macros.
730 @end deftp
731
732 The @code{wait3} function is the predecessor to @code{wait4}, which is
733 more flexible. @code{wait3} is now obsolete.
734
735 @comment sys/wait.h
736 @comment BSD
737 @deftypefun pid_t wait3 (union wait *@var{status-ptr}, int @var{options}, struct rusage *@var{usage})
738 If @var{usage} is a null pointer, @code{wait3} is equivalent to
739 @code{waitpid (-1, @var{status-ptr}, @var{options})}.
740
741 If @var{usage} is not null, @code{wait3} stores usage figures for the
742 child process in @code{*@var{rusage}} (but only if the child has
743 terminated, not if it has stopped). @xref{Resource Usage}.
744 @end deftypefun
745
746 @node Process Creation Example
747 @section Process Creation Example
748
749 Here is an example program showing how you might write a function
750 similar to the built-in @code{system}. It executes its @var{command}
751 argument using the equivalent of @samp{sh -c @var{command}}.
752
753 @smallexample
754 #include <stddef.h>
755 #include <stdlib.h>
756 #include <unistd.h>
757 #include <sys/types.h>
758 #include <sys/wait.h>
759
760 /* @r{Execute the command using this shell program.} */
761 #define SHELL "/bin/sh"
762
763 @group
764 int
765 my_system (const char *command)
766 @{
767 int status;
768 pid_t pid;
769 @end group
770
771 pid = fork ();
772 if (pid == 0)
773 @{
774 /* @r{This is the child process. Execute the shell command.} */
775 execl (SHELL, SHELL, "-c", command, NULL);
776 _exit (EXIT_FAILURE);
777 @}
778 else if (pid < 0)
779 /* @r{The fork failed. Report failure.} */
780 status = -1;
781 else
782 /* @r{This is the parent process. Wait for the child to complete.} */
783 if (waitpid (pid, &status, 0) != pid)
784 status = -1;
785 return status;
786 @}
787 @end smallexample
788
789 @comment Yes, this example has been tested.
790
791 There are a couple of things you should pay attention to in this
792 example.
793
794 Remember that the first @code{argv} argument supplied to the program
795 represents the name of the program being executed. That is why, in the
796 call to @code{execl}, @code{SHELL} is supplied once to name the program
797 to execute and a second time to supply a value for @code{argv[0]}.
798
799 The @code{execl} call in the child process doesn't return if it is
800 successful. If it fails, you must do something to make the child
801 process terminate. Just returning a bad status code with @code{return}
802 would leave two processes running the original program. Instead, the
803 right behavior is for the child process to report failure to its parent
804 process.
805
806 Call @code{_exit} to accomplish this. The reason for using @code{_exit}
807 instead of @code{exit} is to avoid flushing fully buffered streams such
808 as @code{stdout}. The buffers of these streams probably contain data
809 that was copied from the parent process by the @code{fork}, data that
810 will be output eventually by the parent process. Calling @code{exit} in
811 the child would output the data twice. @xref{Termination Internals}.