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