1 @node Processes, Inter-Process Communication, Program Basics, Top
2 @c %MENU% How to create processes and run other programs
6 @dfn{Processes} are the primitive units for allocation of system
7 resources. Each process has its own address space and (usually) one
8 thread of control. A process executes a program; you can have multiple
9 processes executing the same program, but each process has its own copy
10 of the program within its own address space and executes it
11 independently of the other copies.
14 @cindex parent process
15 Processes are organized hierarchically. Each process has a @dfn{parent
16 process} which explicitly arranged to create it. The processes created
17 by a given parent are called its @dfn{child processes}. A child
18 inherits many of its attributes from the parent process.
20 This chapter describes how a program can create, terminate, and control
21 child processes. Actually, there are three distinct operations
22 involved: creating a new child process, causing the new process to
23 execute a program, and coordinating the completion of the child process
24 with the original program.
26 The @code{system} function provides a simple, portable mechanism for
27 running another program; it does all three steps automatically. If you
28 need more control over the details of how this is done, you can use the
29 primitive functions to do each step individually instead.
32 * Running a Command:: The easy way to run another program.
33 * Process Creation Concepts:: An overview of the hard way to do it.
34 * Process Identification:: How to get the process ID of a process.
35 * Creating a Process:: How to fork a child process.
36 * Executing a File:: How to make a process execute another program.
37 * Process Completion:: How to tell when a child process has completed.
38 * Process Completion Status:: How to interpret the status value
39 returned from a child process.
40 * BSD Wait Functions:: More functions, for backward compatibility.
41 * Process Creation Example:: A complete example program.
45 @node Running a Command
46 @section Running a Command
47 @cindex running a command
49 The easy way to run another program is to use the @code{system}
50 function. This function does all the work of running a subprogram, but
51 it doesn't give you much control over the details: you have to wait
52 until the subprogram terminates before you can do anything else.
56 @deftypefun int system (const char *@var{command})
58 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{}}}
59 @c system @ascuplugin @ascuheap @asulock @aculock @acsmem
60 @c do_system @ascuplugin @ascuheap @asulock @aculock @acsmem
62 @c libc_lock_lock @asulock @aculock
66 @c libc_lock_unlock @aculock
69 @c CLEANUP_HANDLER @ascuplugin @ascuheap @acsmem
70 @c libc_cleanup_region_start @ascuplugin @ascuheap @acsmem
71 @c pthread_cleanup_push_defer @ascuplugin @ascuheap @acsmem
72 @c CANCELLATION_P @ascuplugin @ascuheap @acsmem
73 @c CANCEL_ENABLED_AND_CANCELED ok
74 @c do_cancel @ascuplugin @ascuheap @acsmem
80 @c libc_lock_unlock ok
85 @c libc_cleanup_region_end ok
86 @c pthread_cleanup_pop_restore ok
88 @c LIBC_CANCEL_ASYNC @ascuplugin @ascuheap @acsmem
89 @c libc_enable_asynccancel @ascuplugin @ascuheap @acsmem
90 @c CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS dup ok
91 @c do_cancel dup @ascuplugin @ascuheap @acsmem
92 @c LIBC_CANCEL_RESET ok
93 @c libc_disable_asynccancel ok
94 @c lll_futex_wait dup ok
95 This function executes @var{command} as a shell command. In @theglibc{},
96 it always uses the default shell @code{sh} to run the command.
97 In particular, it searches the directories in @code{PATH} to find
98 programs to execute. The return value is @code{-1} if it wasn't
99 possible to create the shell process, and otherwise is the status of the
100 shell process. @xref{Process Completion}, for details on how this
101 status code can be interpreted.
103 If the @var{command} argument is a null pointer, a return value of zero
104 indicates that no command processor is available.
106 This function is a cancellation point in multi-threaded programs. This
107 is a problem if the thread allocates some resources (like memory, file
108 descriptors, semaphores or whatever) at the time @code{system} is
109 called. If the thread gets canceled these resources stay allocated
110 until the program ends. To avoid this calls to @code{system} should be
111 protected using cancellation handlers.
112 @c ref pthread_cleanup_push / pthread_cleanup_pop
115 The @code{system} function is declared in the header file
119 @strong{Portability Note:} Some C implementations may not have any
120 notion of a command processor that can execute other programs. You can
121 determine whether a command processor exists by executing
122 @w{@code{system (NULL)}}; if the return value is nonzero, a command
123 processor is available.
125 The @code{popen} and @code{pclose} functions (@pxref{Pipe to a
126 Subprocess}) are closely related to the @code{system} function. They
127 allow the parent process to communicate with the standard input and
128 output channels of the command being executed.
130 @node Process Creation Concepts
131 @section Process Creation Concepts
133 This section gives an overview of processes and of the steps involved in
134 creating a process and making it run another program.
137 @cindex process lifetime
138 Each process is named by a @dfn{process ID} number. A unique process ID
139 is allocated to each process when it is created. The @dfn{lifetime} of
140 a process ends when its termination is reported to its parent process;
141 at that time, all of the process resources, including its process ID,
144 @cindex creating a process
145 @cindex forking a process
146 @cindex child process
147 @cindex parent process
148 Processes are created with the @code{fork} system call (so the operation
149 of creating a new process is sometimes called @dfn{forking} a process).
150 The @dfn{child process} created by @code{fork} is a copy of the original
151 @dfn{parent process}, except that it has its own process ID.
153 After forking a child process, both the parent and child processes
154 continue to execute normally. If you want your program to wait for a
155 child process to finish executing before continuing, you must do this
156 explicitly after the fork operation, by calling @code{wait} or
157 @code{waitpid} (@pxref{Process Completion}). These functions give you
158 limited information about why the child terminated---for example, its
161 A newly forked child process continues to execute the same program as
162 its parent process, at the point where the @code{fork} call returns.
163 You can use the return value from @code{fork} to tell whether the program
164 is running in the parent process or the child.
166 @cindex process image
167 Having several processes run the same program is only occasionally
168 useful. But the child can execute another program using one of the
169 @code{exec} functions; see @ref{Executing a File}. The program that the
170 process is executing is called its @dfn{process image}. Starting
171 execution of a new program causes the process to forget all about its
172 previous process image; when the new program exits, the process exits
173 too, instead of returning to the previous process image.
175 @node Process Identification
176 @section Process Identification
178 The @code{pid_t} data type represents process IDs. You can get the
179 process ID of a process by calling @code{getpid}. The function
180 @code{getppid} returns the process ID of the parent of the current
181 process (this is also known as the @dfn{parent process ID}). Your
182 program should include the header files @file{unistd.h} and
183 @file{sys/types.h} to use these functions.
189 @deftp {Data Type} pid_t
190 The @code{pid_t} data type is a signed integer type which is capable
191 of representing a process ID. In @theglibc{}, this is an @code{int}.
196 @deftypefun pid_t getpid (void)
197 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
198 The @code{getpid} function returns the process ID of the current process.
203 @deftypefun pid_t getppid (void)
204 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
205 The @code{getppid} function returns the process ID of the parent of the
209 @node Creating a Process
210 @section Creating a Process
212 The @code{fork} function is the primitive for creating a process.
213 It is declared in the header file @file{unistd.h}.
218 @deftypefun pid_t fork (void)
219 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuplugin{}}@acunsafe{@aculock{}}}
220 @c The nptl/.../linux implementation safely collects fork_handlers into
221 @c an alloca()ed linked list and increments ref counters; it uses atomic
222 @c ops and retries, avoiding locking altogether. It then takes the
223 @c IO_list lock, resets the thread-local pid, and runs fork. The parent
224 @c restores the thread-local pid, releases the lock, and runs parent
225 @c handlers, decrementing the ref count and signaling futex wait if
226 @c requested by unregister_atfork. The child bumps the fork generation,
227 @c sets the thread-local pid, resets cpu clocks, initializes the robust
228 @c mutex list, the stream locks, the IO_list lock, the dynamic loader
229 @c lock, runs the child handlers, reseting ref counters to 1, and
230 @c initializes the fork lock. These are all safe, unless atfork
231 @c handlers themselves are unsafe.
232 The @code{fork} function creates a new process.
234 If the operation is successful, there are then both parent and child
235 processes and both see @code{fork} return, but with different values: it
236 returns a value of @code{0} in the child process and returns the child's
237 process ID in the parent process.
239 If process creation failed, @code{fork} returns a value of @code{-1} in
240 the parent process. The following @code{errno} error conditions are
241 defined for @code{fork}:
245 There aren't enough system resources to create another process, or the
246 user already has too many processes running. This means exceeding the
247 @code{RLIMIT_NPROC} resource limit, which can usually be increased;
248 @pxref{Limits on Resources}.
251 The process requires more space than the system can supply.
255 The specific attributes of the child process that differ from the
260 The child process has its own unique process ID.
263 The parent process ID of the child process is the process ID of its
267 The child process gets its own copies of the parent process's open file
268 descriptors. Subsequently changing attributes of the file descriptors
269 in the parent process won't affect the file descriptors in the child,
270 and vice versa. @xref{Control Operations}. However, the file position
271 associated with each descriptor is shared by both processes;
272 @pxref{File Position}.
275 The elapsed processor times for the child process are set to zero;
276 see @ref{Processor Time}.
279 The child doesn't inherit file locks set by the parent process.
280 @c !!! flock locks shared
281 @xref{Control Operations}.
284 The child doesn't inherit alarms set by the parent process.
285 @xref{Setting an Alarm}.
288 The set of pending signals (@pxref{Delivery of Signal}) for the child
289 process is cleared. (The child process inherits its mask of blocked
290 signals and signal actions from the parent process.)
296 @deftypefun pid_t vfork (void)
297 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuplugin{}}@acunsafe{@aculock{}}}
298 @c The vfork implementation proper is a safe syscall, but it may fall
299 @c back to fork if the vfork syscall is not available.
300 The @code{vfork} function is similar to @code{fork} but on some systems
301 it is more efficient; however, there are restrictions you must follow to
304 While @code{fork} makes a complete copy of the calling process's address
305 space and allows both the parent and child to execute independently,
306 @code{vfork} does not make this copy. Instead, the child process
307 created with @code{vfork} shares its parent's address space until it
308 calls @code{_exit} or one of the @code{exec} functions. In the
309 meantime, the parent process suspends execution.
311 You must be very careful not to allow the child process created with
312 @code{vfork} to modify any global data or even local variables shared
313 with the parent. Furthermore, the child process cannot return from (or
314 do a long jump out of) the function that called @code{vfork}! This
315 would leave the parent process's control information very confused. If
316 in doubt, use @code{fork} instead.
318 Some operating systems don't really implement @code{vfork}. @Theglibc{}
319 permits you to use @code{vfork} on all systems, but actually
320 executes @code{fork} if @code{vfork} isn't available. If you follow
321 the proper precautions for using @code{vfork}, your program will still
322 work even if the system uses @code{fork} instead.
325 @node Executing a File
326 @section Executing a File
327 @cindex executing a file
328 @cindex @code{exec} functions
330 This section describes the @code{exec} family of functions, for executing
331 a file as a process image. You can use these functions to make a child
332 process execute a new program after it has been forked.
334 To see the effects of @code{exec} from the point of view of the called
335 program, see @ref{Program Basics}.
338 The functions in this family differ in how you specify the arguments,
339 but otherwise they all do the same thing. They are declared in the
340 header file @file{unistd.h}.
344 @deftypefun int execv (const char *@var{filename}, char *const @var{argv}@t{[]})
345 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
346 The @code{execv} function executes the file named by @var{filename} as a
349 The @var{argv} argument is an array of null-terminated strings that is
350 used to provide a value for the @code{argv} argument to the @code{main}
351 function of the program to be executed. The last element of this array
352 must be a null pointer. By convention, the first element of this array
353 is the file name of the program sans directory names. @xref{Program
354 Arguments}, for full details on how programs can access these arguments.
356 The environment for the new process image is taken from the
357 @code{environ} variable of the current process image; see
358 @ref{Environment Variables}, for information about environments.
363 @deftypefun int execl (const char *@var{filename}, const char *@var{arg0}, @dots{})
364 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
365 This is similar to @code{execv}, but the @var{argv} strings are
366 specified individually instead of as an array. A null pointer must be
367 passed as the last such argument.
372 @deftypefun int execve (const char *@var{filename}, char *const @var{argv}@t{[]}, char *const @var{env}@t{[]})
373 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
374 This is similar to @code{execv}, but permits you to specify the environment
375 for the new program explicitly as the @var{env} argument. This should
376 be an array of strings in the same format as for the @code{environ}
377 variable; see @ref{Environment Access}.
382 @deftypefun int execle (const char *@var{filename}, const char *@var{arg0}, @dots{}, char *const @var{env}@t{[]})
383 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
384 This is similar to @code{execl}, but permits you to specify the
385 environment for the new program explicitly. The environment argument is
386 passed following the null pointer that marks the last @var{argv}
387 argument, and should be an array of strings in the same format as for
388 the @code{environ} variable.
393 @deftypefun int execvp (const char *@var{filename}, char *const @var{argv}@t{[]})
394 @safety{@prelim{}@mtsafe{@mtsenv{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
395 The @code{execvp} function is similar to @code{execv}, except that it
396 searches the directories listed in the @code{PATH} environment variable
397 (@pxref{Standard Environment}) to find the full file name of a
398 file from @var{filename} if @var{filename} does not contain a slash.
400 This function is useful for executing system utility programs, because
401 it looks for them in the places that the user has chosen. Shells use it
402 to run the commands that users type.
407 @deftypefun int execlp (const char *@var{filename}, const char *@var{arg0}, @dots{})
408 @safety{@prelim{}@mtsafe{@mtsenv{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
409 This function is like @code{execl}, except that it performs the same
410 file name searching as the @code{execvp} function.
413 The size of the argument list and environment list taken together must
414 not be greater than @code{ARG_MAX} bytes. @xref{General Limits}. On
415 @gnuhurdsystems{}, the size (which compares against @code{ARG_MAX})
416 includes, for each string, the number of characters in the string, plus
417 the size of a @code{char *}, plus one, rounded up to a multiple of the
418 size of a @code{char *}. Other systems may have somewhat different
421 These functions normally don't return, since execution of a new program
422 causes the currently executing program to go away completely. A value
423 of @code{-1} is returned in the event of a failure. In addition to the
424 usual file name errors (@pxref{File Name Errors}), the following
425 @code{errno} error conditions are defined for these functions:
429 The combined size of the new program's argument list and environment
430 list is larger than @code{ARG_MAX} bytes. @gnuhurdsystems{} have no
431 specific limit on the argument list size, so this error code cannot
432 result, but you may get @code{ENOMEM} instead if the arguments are too
433 big for available memory.
436 The specified file can't be executed because it isn't in the right format.
439 Executing the specified file requires more storage than is available.
442 If execution of the new file succeeds, it updates the access time field
443 of the file as if the file had been read. @xref{File Times}, for more
444 details about access times of files.
446 The point at which the file is closed again is not specified, but
447 is at some point before the process exits or before another process
450 Executing a new process image completely changes the contents of memory,
451 copying only the argument and environment strings to new locations. But
452 many other attributes of the process are unchanged:
456 The process ID and the parent process ID. @xref{Process Creation Concepts}.
459 Session and process group membership. @xref{Concepts of Job Control}.
462 Real user ID and group ID, and supplementary group IDs. @xref{Process
466 Pending alarms. @xref{Setting an Alarm}.
469 Current working directory and root directory. @xref{Working
470 Directory}. On @gnuhurdsystems{}, the root directory is not copied when
471 executing a setuid program; instead the system default root directory
472 is used for the new program.
475 File mode creation mask. @xref{Setting Permissions}.
478 Process signal mask; see @ref{Process Signal Mask}.
481 Pending signals; see @ref{Blocking Signals}.
484 Elapsed processor time associated with the process; see @ref{Processor Time}.
487 If the set-user-ID and set-group-ID mode bits of the process image file
488 are set, this affects the effective user ID and effective group ID
489 (respectively) of the process. These concepts are discussed in detail
490 in @ref{Process Persona}.
492 Signals that are set to be ignored in the existing process image are
493 also set to be ignored in the new process image. All other signals are
494 set to the default action in the new process image. For more
495 information about signals, see @ref{Signal Handling}.
497 File descriptors open in the existing process image remain open in the
498 new process image, unless they have the @code{FD_CLOEXEC}
499 (close-on-exec) flag set. The files that remain open inherit all
500 attributes of the open file description from the existing process image,
501 including file locks. File descriptors are discussed in @ref{Low-Level I/O}.
503 Streams, by contrast, cannot survive through @code{exec} functions,
504 because they are located in the memory of the process itself. The new
505 process image has no streams except those it creates afresh. Each of
506 the streams in the pre-@code{exec} process image has a descriptor inside
507 it, and these descriptors do survive through @code{exec} (provided that
508 they do not have @code{FD_CLOEXEC} set). The new process image can
509 reconnect these to new streams using @code{fdopen} (@pxref{Descriptors
512 @node Process Completion
513 @section Process Completion
514 @cindex process completion
515 @cindex waiting for completion of child process
516 @cindex testing exit status of child process
518 The functions described in this section are used to wait for a child
519 process to terminate or stop, and determine its status. These functions
520 are declared in the header file @file{sys/wait.h}.
525 @deftypefun pid_t waitpid (pid_t @var{pid}, int *@var{status-ptr}, int @var{options})
526 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
527 The @code{waitpid} function is used to request status information from a
528 child process whose process ID is @var{pid}. Normally, the calling
529 process is suspended until the child process makes status information
530 available by terminating.
532 Other values for the @var{pid} argument have special interpretations. A
533 value of @code{-1} or @code{WAIT_ANY} requests status information for
534 any child process; a value of @code{0} or @code{WAIT_MYPGRP} requests
535 information for any child process in the same process group as the
536 calling process; and any other negative value @minus{} @var{pgid}
537 requests information for any child process whose process group ID is
540 If status information for a child process is available immediately, this
541 function returns immediately without waiting. If more than one eligible
542 child process has status information available, one of them is chosen
543 randomly, and its status is returned immediately. To get the status
544 from the other eligible child processes, you need to call @code{waitpid}
547 The @var{options} argument is a bit mask. Its value should be the
548 bitwise OR (that is, the @samp{|} operator) of zero or more of the
549 @code{WNOHANG} and @code{WUNTRACED} flags. You can use the
550 @code{WNOHANG} flag to indicate that the parent process shouldn't wait;
551 and the @code{WUNTRACED} flag to request status information from stopped
552 processes as well as processes that have terminated.
554 The status information from the child process is stored in the object
555 that @var{status-ptr} points to, unless @var{status-ptr} is a null pointer.
557 This function is a cancellation point in multi-threaded programs. This
558 is a problem if the thread allocates some resources (like memory, file
559 descriptors, semaphores or whatever) at the time @code{waitpid} is
560 called. If the thread gets canceled these resources stay allocated
561 until the program ends. To avoid this calls to @code{waitpid} should be
562 protected using cancellation handlers.
563 @c ref pthread_cleanup_push / pthread_cleanup_pop
565 The return value is normally the process ID of the child process whose
566 status is reported. If there are child processes but none of them is
567 waiting to be noticed, @code{waitpid} will block until one is. However,
568 if the @code{WNOHANG} option was specified, @code{waitpid} will return
569 zero instead of blocking.
571 If a specific PID to wait for was given to @code{waitpid}, it will
572 ignore all other children (if any). Therefore if there are children
573 waiting to be noticed but the child whose PID was specified is not one
574 of them, @code{waitpid} will block or return zero as described above.
576 A value of @code{-1} is returned in case of error. The following
577 @code{errno} error conditions are defined for this function:
581 The function was interrupted by delivery of a signal to the calling
582 process. @xref{Interrupted Primitives}.
585 There are no child processes to wait for, or the specified @var{pid}
586 is not a child of the calling process.
589 An invalid value was provided for the @var{options} argument.
593 These symbolic constants are defined as values for the @var{pid} argument
594 to the @code{waitpid} function.
596 @comment Extra blank lines make it look better.
600 This constant macro (whose value is @code{-1}) specifies that
601 @code{waitpid} should return status information about any child process.
605 This constant (with value @code{0}) specifies that @code{waitpid} should
606 return status information about any child process in the same process
607 group as the calling process.
610 These symbolic constants are defined as flags for the @var{options}
611 argument to the @code{waitpid} function. You can bitwise-OR the flags
612 together to obtain a value to use as the argument.
617 This flag specifies that @code{waitpid} should return immediately
618 instead of waiting, if there is no child process ready to be noticed.
622 This flag specifies that @code{waitpid} should report the status of any
623 child processes that have been stopped as well as those that have
629 @deftypefun pid_t wait (int *@var{status-ptr})
630 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
631 This is a simplified version of @code{waitpid}, and is used to wait
632 until any one child process terminates. The call:
639 is exactly equivalent to:
642 waitpid (-1, &status, 0)
645 This function is a cancellation point in multi-threaded programs. This
646 is a problem if the thread allocates some resources (like memory, file
647 descriptors, semaphores or whatever) at the time @code{wait} is
648 called. If the thread gets canceled these resources stay allocated
649 until the program ends. To avoid this calls to @code{wait} should be
650 protected using cancellation handlers.
651 @c ref pthread_cleanup_push / pthread_cleanup_pop
656 @deftypefun pid_t wait4 (pid_t @var{pid}, int *@var{status-ptr}, int @var{options}, struct rusage *@var{usage})
657 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
658 If @var{usage} is a null pointer, @code{wait4} is equivalent to
659 @code{waitpid (@var{pid}, @var{status-ptr}, @var{options})}.
661 If @var{usage} is not null, @code{wait4} stores usage figures for the
662 child process in @code{*@var{rusage}} (but only if the child has
663 terminated, not if it has stopped). @xref{Resource Usage}.
665 This function is a BSD extension.
668 Here's an example of how to use @code{waitpid} to get the status from
669 all child processes that have terminated, without ever waiting. This
670 function is designed to be a handler for @code{SIGCHLD}, the signal that
671 indicates that at least one child process has terminated.
676 sigchld_handler (int signum)
678 int pid, status, serrno;
682 pid = waitpid (WAIT_ANY, &status, WNOHANG);
690 notice_termination (pid, status);
697 @node Process Completion Status
698 @section Process Completion Status
700 If the exit status value (@pxref{Program Termination}) of the child
701 process is zero, then the status value reported by @code{waitpid} or
702 @code{wait} is also zero. You can test for other kinds of information
703 encoded in the returned status value using the following macros.
704 These macros are defined in the header file @file{sys/wait.h}.
709 @deftypefn Macro int WIFEXITED (int @var{status})
710 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
711 This macro returns a nonzero value if the child process terminated
712 normally with @code{exit} or @code{_exit}.
717 @deftypefn Macro int WEXITSTATUS (int @var{status})
718 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
719 If @code{WIFEXITED} is true of @var{status}, this macro returns the
720 low-order 8 bits of the exit status value from the child process.
726 @deftypefn Macro int WIFSIGNALED (int @var{status})
727 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
728 This macro returns a nonzero value if the child process terminated
729 because it received a signal that was not handled.
730 @xref{Signal Handling}.
735 @deftypefn Macro int WTERMSIG (int @var{status})
736 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
737 If @code{WIFSIGNALED} is true of @var{status}, this macro returns the
738 signal number of the signal that terminated the child process.
743 @deftypefn Macro int WCOREDUMP (int @var{status})
744 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
745 This macro returns a nonzero value if the child process terminated
746 and produced a core dump.
751 @deftypefn Macro int WIFSTOPPED (int @var{status})
752 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
753 This macro returns a nonzero value if the child process is stopped.
758 @deftypefn Macro int WSTOPSIG (int @var{status})
759 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
760 If @code{WIFSTOPPED} is true of @var{status}, this macro returns the
761 signal number of the signal that caused the child process to stop.
765 @node BSD Wait Functions
766 @section BSD Process Wait Functions
768 @Theglibc{} also provides these related facilities for compatibility
769 with BSD Unix. BSD uses the @code{union wait} data type to represent
770 status values rather than an @code{int}. The two representations are
771 actually interchangeable; they describe the same bit patterns. @Theglibc{}
772 defines macros such as @code{WEXITSTATUS} so that they will
773 work on either kind of object, and the @code{wait} function is defined
774 to accept either type of pointer as its @var{status-ptr} argument.
776 These functions are declared in @file{sys/wait.h}.
781 @deftp {Data Type} {union wait}
782 This data type represents program termination status values. It has
783 the following members:
787 The value of this member is the same as that of the
788 @code{WTERMSIG} macro.
791 The value of this member is the same as that of the
792 @code{WCOREDUMP} macro.
795 The value of this member is the same as that of the
796 @code{WEXITSTATUS} macro.
799 The value of this member is the same as that of the
800 @code{WSTOPSIG} macro.
803 Instead of accessing these members directly, you should use the
807 The @code{wait3} function is the predecessor to @code{wait4}, which is
808 more flexible. @code{wait3} is now obsolete.
812 @deftypefun pid_t wait3 (union wait *@var{status-ptr}, int @var{options}, struct rusage *@var{usage})
813 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
814 If @var{usage} is a null pointer, @code{wait3} is equivalent to
815 @code{waitpid (-1, @var{status-ptr}, @var{options})}.
817 If @var{usage} is not null, @code{wait3} stores usage figures for the
818 child process in @code{*@var{rusage}} (but only if the child has
819 terminated, not if it has stopped). @xref{Resource Usage}.
822 @node Process Creation Example
823 @section Process Creation Example
825 Here is an example program showing how you might write a function
826 similar to the built-in @code{system}. It executes its @var{command}
827 argument using the equivalent of @samp{sh -c @var{command}}.
833 #include <sys/types.h>
834 #include <sys/wait.h>
836 /* @r{Execute the command using this shell program.} */
837 #define SHELL "/bin/sh"
841 my_system (const char *command)
850 /* @r{This is the child process. Execute the shell command.} */
851 execl (SHELL, SHELL, "-c", command, NULL);
852 _exit (EXIT_FAILURE);
855 /* @r{The fork failed. Report failure.} */
858 /* @r{This is the parent process. Wait for the child to complete.} */
859 if (waitpid (pid, &status, 0) != pid)
865 @comment Yes, this example has been tested.
867 There are a couple of things you should pay attention to in this
870 Remember that the first @code{argv} argument supplied to the program
871 represents the name of the program being executed. That is why, in the
872 call to @code{execl}, @code{SHELL} is supplied once to name the program
873 to execute and a second time to supply a value for @code{argv[0]}.
875 The @code{execl} call in the child process doesn't return if it is
876 successful. If it fails, you must do something to make the child
877 process terminate. Just returning a bad status code with @code{return}
878 would leave two processes running the original program. Instead, the
879 right behavior is for the child process to report failure to its parent
882 Call @code{_exit} to accomplish this. The reason for using @code{_exit}
883 instead of @code{exit} is to avoid flushing fully buffered streams such
884 as @code{stdout}. The buffers of these streams probably contain data
885 that was copied from the parent process by the @code{fork}, data that
886 will be output eventually by the parent process. Calling @code{exit} in
887 the child would output the data twice. @xref{Termination Internals}.