1 @node Process Startup, Child Processes, Signal Handling, Top
2 @chapter Process Startup and Termination
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.
12 This chapter explains what your program should do to handle the startup
13 of a process, to terminate its process, and to receive information
14 (arguments and the environment) from the parent process.
17 * Program Arguments:: Parsing your program's command-line arguments.
18 * Environment Variables:: How to access parameters inherited from
20 * Program Termination:: How to cause a process to terminate and
21 return status information to its parent.
24 @node Program Arguments
25 @section Program Arguments
26 @cindex program arguments
27 @cindex command line arguments
29 @cindex @code{main} function
30 The system starts a C program by calling the function @code{main}. It
31 is up to you to write a function named @code{main}---otherwise, you
32 won't even be able to link your program without errors.
34 You can define @code{main} either to take no arguments, or to take two
35 arguments that represent the command line arguments to the program, like
39 int main (int @var{argc}, char *@var{argv}[])
42 @cindex argc (program argument count)
43 @cindex argv (program argument vector)
44 The command line arguments are the whitespace-separated tokens given in
45 the shell command used to invoke the program; thus, in @samp{cat foo
46 bar}, the arguments are @samp{foo} and @samp{bar}. The only way a
47 program can look at its command line arguments is via the arguments of
48 @code{main}. If @code{main} doesn't take arguments, then you cannot get
51 The value of the @var{argc} argument is the number of command line
52 arguments. The @var{argv} argument is a vector of C strings; its
53 elements are the individual command line argument strings. The file
54 name of the program being run is also included in the vector as the
55 first element; the value of @var{argc} counts this element. A null
56 pointer always follows the last element: @code{@var{argv}[@var{argc}]}
59 For the command @samp{cat foo bar}, @var{argc} is 3 and @var{argv} has
60 three elements, @code{"cat"}, @code{"foo"} and @code{"bar"}.
62 If the syntax for the command line arguments to your program is simple
63 enough, you can simply pick the arguments off from @var{argv} by hand.
64 But unless your program takes a fixed number of arguments, or all of the
65 arguments are interpreted in the same way (as file names, for example),
66 you are usually better off using @code{getopt} to do the parsing.
69 * Argument Syntax:: By convention, options start with a hyphen.
70 * Parsing Options:: The @code{getopt} function.
71 * Example of Getopt:: An example of parsing options with @code{getopt}.
72 * Long Options:: GNU suggests utilities accept long-named options.
73 Here is how to do that.
74 * Long Option Example:: An example of using @code{getopt_long}.
78 @subsection Program Argument Syntax Conventions
79 @cindex program argument syntax
80 @cindex syntax, for program arguments
81 @cindex command argument syntax
83 POSIX recommends these conventions for command line arguments.
84 @code{getopt} (@pxref{Parsing Options}) makes it easy to implement them.
88 Arguments are options if they begin with a hyphen delimiter (@samp{-}).
91 Multiple options may follow a hyphen delimiter in a single token if
92 the options do not take arguments. Thus, @samp{-abc} is equivalent to
96 Option names are single alphanumeric characters (as for @code{isalnum};
97 see @ref{Classification of Characters}).
100 Certain options require an argument. For example, the @samp{-o} command
101 of the @code{ld} command requires an argument---an output file name.
104 An option and its argument may or may not appear as separate tokens. (In
105 other words, the whitespace separating them is optional.) Thus,
106 @samp{-o foo} and @samp{-ofoo} are equivalent.
109 Options typically precede other non-option arguments.
111 The implementation of @code{getopt} in the GNU C library normally makes
112 it appear as if all the option arguments were specified before all the
113 non-option arguments for the purposes of parsing, even if the user of
114 your program intermixed option and non-option arguments. It does this
115 by reordering the elements of the @var{argv} array. This behavior is
116 nonstandard; if you want to suppress it, define the
117 @code{_POSIX_OPTION_ORDER} environment variable. @xref{Standard
121 The argument @samp{--} terminates all options; any following arguments
122 are treated as non-option arguments, even if they begin with a hyphen.
125 A token consisting of a single hyphen character is interpreted as an
126 ordinary non-option argument. By convention, it is used to specify
127 input from or output to the standard input and output streams.
130 Options may be supplied in any order, or appear multiple times. The
131 interpretation is left up to the particular application program.
134 @cindex long-named options
135 GNU adds @dfn{long options} to these conventions. Long options consist
136 of @samp{--} followed by a name made of alphanumeric characters and
137 dashes. Option names are typically one to three words long, with
138 hyphens to separate words. Users can abbreviate the option names as
139 long as the abbreviations are unique.
141 To specify an argument for a long option, write
142 @samp{--@var{name}=@var{value}}. This syntax enables a long option to
143 accept an argument that is itself optional.
145 Eventually, the GNU system will provide completion for long option names
148 @node Parsing Options
149 @subsection Parsing Program Options
150 @cindex program arguments, parsing
151 @cindex command arguments, parsing
152 @cindex parsing program arguments
154 Here are the details about how to call the @code{getopt} function. To
155 use this facility, your program must include the header file
161 @deftypevar int opterr
162 If the value of this variable is nonzero, then @code{getopt} prints an
163 error message to the standard error stream if it encounters an unknown
164 option character or an option with a missing required argument. This is
165 the default behavior. If you set this variable to zero, @code{getopt}
166 does not print any messages, but it still returns the character @code{?}
167 to indicate an error.
172 @deftypevar int optopt
173 When @code{getopt} encounters an unknown option character or an option
174 with a missing required argument, it stores that option character in
175 this variable. You can use this for providing your own diagnostic
181 @deftypevar int optind
182 This variable is set by @code{getopt} to the index of the next element
183 of the @var{argv} array to be processed. Once @code{getopt} has found
184 all of the option arguments, you can use this variable to determine
185 where the remaining non-option arguments begin. The initial value of
186 this variable is @code{1}.
191 @deftypevar {char *} optarg
192 This variable is set by @code{getopt} to point at the value of the
193 option argument, for those options that accept arguments.
198 @deftypefun int getopt (int @var{argc}, char **@var{argv}, const char *@var{options})
199 The @code{getopt} function gets the next option argument from the
200 argument list specified by the @var{argv} and @var{argc} arguments.
201 Normally these values come directly from the arguments received by
204 The @var{options} argument is a string that specifies the option
205 characters that are valid for this program. An option character in this
206 string can be followed by a colon (@samp{:}) to indicate that it takes a
209 If the @var{options} argument string begins with a hyphen (@samp{-}), this
210 is treated specially. It permits arguments that are not options to be
211 returned as if they were associated with option character @samp{\0}.
213 The @code{getopt} function returns the option character for the next
214 command line option. When no more option arguments are available, it
215 returns @code{-1}. There may still be more non-option arguments; you
216 must compare the external variable @code{optind} against the @var{argv}
217 parameter to check this.
219 If the option has an argument, @code{getopt} returns the argument by
220 storing it in the varables @var{optarg}. You don't ordinarily need to
221 copy the @code{optarg} string, since it is a pointer into the original
222 @var{argv} array, not into a static area that might be overwritten.
224 If @code{getopt} finds an option character in @var{argv} that was not
225 included in @var{options}, or a missing option argument, it returns
226 @samp{?} and sets the external variable @code{optopt} to the actual
227 option character. In addition, if the external variable @code{opterr}
228 is nonzero (which is the default), @code{getopt} prints an error message.
231 @node Example of Getopt
232 @subsection Example of Parsing Arguments with @code{getopt}
234 Here is an example showing how @code{getopt} is typically used. The
235 key points to notice are:
239 Normally, @code{getopt} is called in a loop. When @code{getopt} returns
240 @code{-1}, indicating no more options are present, the loop terminates.
243 A @code{switch} statement is used to dispatch on the return value from
244 @code{getopt}. In typical use, each case just sets a variable that
245 is used later in the program.
248 A second loop is used to process the remaining non-option arguments.
252 @include testopt.c.texi
255 Here are some examples showing what this program prints with different
256 combinations of arguments:
260 aflag = 0, bflag = 0, cvalue = (null)
263 aflag = 1, bflag = 1, cvalue = (null)
266 aflag = 1, bflag = 1, cvalue = (null)
269 aflag = 0, bflag = 0, cvalue = foo
272 aflag = 0, bflag = 0, cvalue = foo
275 aflag = 0, bflag = 0, cvalue = (null)
276 Non-option argument arg1
279 aflag = 1, bflag = 0, cvalue = (null)
280 Non-option argument arg1
282 % testopt -c foo arg1
283 aflag = 0, bflag = 0, cvalue = foo
284 Non-option argument arg1
287 aflag = 1, bflag = 0, cvalue = (null)
288 Non-option argument -b
291 aflag = 1, bflag = 0, cvalue = (null)
292 Non-option argument -
296 @subsection Parsing Long Options
298 To accept GNU-style long options as well as single-character options,
299 use @code{getopt_long} instead of @code{getopt}. You should make every
300 program accept long options if it uses any options, for this takes
301 little extra work and helps beginners remember how to use the program.
305 @deftp {Data Type} {struct option}
306 This structure describes a single long option name for the sake of
307 @code{getopt_long}. The argument @var{longopts} must be an array of
308 these structures, one for each long option. Terminate the array with an
309 element containing all zeros.
311 The @code{struct option} structure has these fields:
314 @item const char *name
315 This field is the name of the option. It is a string.
318 This field says whether the option takes an argument. It is an integer,
319 and there are three legitimate values: @code{no_argument},
320 @code{required_argument} and @code{optional_argument}.
324 These fields control how to report or act on the option when it occurs.
326 If @code{flag} is a null pointer, then the @code{val} is a value which
327 identifies this option. Often these values are chosen to uniquely
328 identify particular long options.
330 If @code{flag} is not a null pointer, it should be the address of an
331 @code{int} variable which is the flag for this option. The value in
332 @code{val} is the value to store in the flag to indicate that the option
339 @deftypefun int getopt_long (int @var{argc}, char **@var{argv}, const char *@var{shortopts}, struct option *@var{longopts}, int *@var{indexptr})
340 Decode options from the vector @var{argv} (whose length is @var{argc}).
341 The argument @var{shortopts} describes the short options to accept, just as
342 it does in @code{getopt}. The argument @var{longopts} describes the long
343 options to accept (see above).
345 When @code{getopt_long} encounters a short option, it does the same
346 thing that @code{getopt} would do: it returns the character code for the
347 option, and stores the options argument (if it has one) in @code{optarg}.
349 When @code{getopt_long} encounters a long option, it takes actions based
350 on the @code{flag} and @code{val} fields of the definition of that
353 If @code{flag} is a null pointer, then @code{getopt_long} returns the
354 contents of @code{val} to indicate which option it found. You should
355 arrange distinct values in the @code{val} field for options with
356 different meanings, so you can decode these values after
357 @code{getopt_long} returns. If the long option is equivalent to a short
358 option, you can use the short option's character code in @code{val}.
360 If @code{flag} is not a null pointer, that means this option should just
361 set a flag in the program. The flag is a variable of type @code{int}
362 that you define. Put the address of the flag in the @code{flag} field.
363 Put in the @code{val} field the value you would like this option to
364 store in the flag. In this case, @code{getopt_long} returns @code{0}.
366 For any long option, @code{getopt_long} tells you the index in the array
367 @var{longopts} of the options definition, by storing it into
368 @code{*@var{indexptr}}. You can get the name of the option with
369 @code{@var{longopts}[*@var{indexptr}].name}. So you can distinguish among
370 long options either by the values in their @code{val} fields or by their
371 indices. You can also distinguish in this way among long options that
374 When a long option has an argument, @code{getopt_long} puts the argument
375 value in the variable @code{optarg} before returning. When the option
376 has no argument, the value in @code{optarg} is a null pointer. This is
377 how you can tell whether an optional argument was supplied.
379 When @code{getopt_long} has no more options to handle, it returns
380 @code{-1}, and leaves in the variable @code{optind} the index in
381 @var{argv} of the next remaining argument.
384 @node Long Option Example
385 @subsection Example of Parsing Long Options
388 @include longopt.c.texi
391 @node Environment Variables
392 @section Environment Variables
394 @cindex environment variable
395 When a program is executed, it receives information about the context in
396 which it was invoked in two ways. The first mechanism uses the
397 @var{argv} and @var{argc} arguments to its @code{main} function, and is
398 discussed in @ref{Program Arguments}. The second mechanism uses
399 @dfn{environment variables} and is discussed in this section.
401 The @var{argv} mechanism is typically used to pass command-line
402 arguments specific to the particular program being invoked. The
403 environment, on the other hand, keeps track of information that is
404 shared by many programs, changes infrequently, and that is less
407 The environment variables discussed in this section are the same
408 environment variables that you set using assignments and the
409 @code{export} command in the shell. Programs executed from the shell
410 inherit all of the environment variables from the shell.
411 @c !!! xref to right part of bash manual when it exists
414 Standard environment variables are used for information about the user's
415 home directory, terminal type, current locale, and so on; you can define
416 additional variables for other purposes. The set of all environment
417 variables that have values is collectively known as the
420 Names of environment variables are case-sensitive and must not contain
421 the character @samp{=}. System-defined environment variables are
422 invariably uppercase.
424 The values of environment variables can be anything that can be
425 represented as a string. A value must not contain an embedded null
426 character, since this is assumed to terminate the string.
430 * Environment Access:: How to get and set the values of
431 environment variables.
432 * Standard Environment:: These environment variables have
433 standard interpretations.
436 @node Environment Access
437 @subsection Environment Access
438 @cindex environment access
439 @cindex environment representation
441 The value of an environment variable can be accessed with the
442 @code{getenv} function. This is declared in the header file
448 @deftypefun {char *} getenv (const char *@var{name})
449 This function returns a string that is the value of the environment
450 variable @var{name}. You must not modify this string. In some systems
451 not using the GNU library, it might be overwritten by subsequent calls
452 to @code{getenv} (but not by any other library function).
453 @c !!! never overwritten on any unix system
454 If the environment variable @var{name} is not defined, the value is a
461 @deftypefun int putenv (const char *@var{string})
462 The @code{putenv} function adds or removes definitions from the environment.
463 If the @var{string} is of the form @samp{@var{name}=@var{value}}, the
464 definition is added to the environment. Otherwise, the @var{string} is
465 interpreted as the name of an environment variable, and any definition
466 for this variable in the environment is removed.
468 The GNU library provides this function for compatibility with SVID; it
469 may not be available in other systems.
472 @c !!! BSD function setenv
474 You can deal directly with the underlying representation of environment
475 objects to add more variables to the environment (for example, to
476 communicate with another program you are about to execute; see
477 @ref{Executing a File}).
481 @deftypevar {char **} environ
482 The environment is represented as an array of strings. Each string is
483 of the format @samp{@var{name}=@var{value}}. The order in which
484 strings appear in the environment is not significant, but the same
485 @var{name} must not appear more than once. The last element of the
486 array is a null pointer.
488 This variable is declared in the header file @file{unistd.h}.
490 If you just want to get the value of an environment variable, use
494 @node Standard Environment
495 @subsection Standard Environment Variables
496 @cindex standard environment variables
498 These environment variables have standard meanings. This doesn't mean
499 that they are always present in the environment; but if these variables
500 @emph{are} present, they have these meanings, and that you shouldn't try
501 to use these environment variable names for some other purpose.
503 @comment Extra blank lines make it look better.
506 @cindex HOME environment variable
507 @cindex home directory
509 This is a string representing the user's @dfn{home directory}, or
510 initial default working directory.
512 The user can set @code{HOME} to any value.
513 If you need to make sure to obtain the proper home directory
514 for a particular user, you should not use @code{HOME}; instead,
515 look up the user's name in the user database (@pxref{User Database}).
517 For most purposes, it is better to use @code{HOME}, precisely because
518 this lets the user specify the value.
522 @cindex LOGNAME environment variable
524 This is the name that the user used to log in. Since the value in the
525 environment can be tweaked arbitrarily, this is not a reliable way to
526 identify the user who is running a process; a function like
527 @code{getlogin} (@pxref{Who Logged In}) is better for that purpose.
529 For most purposes, it is better to use @code{LOGNAME}, precisely because
530 this lets the user specify the value.
533 @cindex PATH environment variable
535 A @dfn{path} is a sequence of directory names which is used for
536 searching for a file. The variable @code{PATH} holds a path used
537 for searching for programs to be run.
539 The @code{execlp} and @code{execvp} functions (@pxref{Executing a File})
540 use this environment variable, as do many shells and other utilities
541 which are implemented in terms of those functions.
543 The syntax of a path is a sequence of directory names separated by
544 colons. An empty string instead of a directory name stands for the
545 current directory (@pxref{Working Directory}).
547 A typical value for this environment variable might be a string like:
550 :/bin:/etc:/usr/bin:/usr/new/X11:/usr/new:/usr/local:/usr/local/bin
553 This means that if the user tries to execute a program named @code{foo},
554 the system will look for files named @file{foo}, @file{/bin/foo},
555 @file{/etc/foo}, and so on. The first of these files that exists is
556 the one that is executed.
560 @cindex TERM environment variable
562 This specifies the kind of terminal that is receiving program output.
563 Some programs can make use of this information to take advantage of
564 special escape sequences or terminal modes supported by particular kinds
565 of terminals. Many programs which use the termcap library
566 (@pxref{Finding a Terminal Description,Find,,termcap,The Termcap Library
567 Manual}) use the @code{TERM} environment variable, for example.
570 @cindex TZ environment variable
572 This specifies the time zone. @xref{TZ Variable}, for information about
573 the format of this string and how it is used.
576 @cindex LANG environment variable
578 This specifies the default locale to use for attribute categories where
579 neither @code{LC_ALL} nor the specific environment variable for that
580 category is set. @xref{Locales}, for more information about
584 @c I doubt this really exists
586 @cindex LC_ALL environment variable
588 This is similar to the @code{LANG} environment variable. However, its
589 value takes precedence over any values provided for the individual
590 attribute category environment variables, or for the @code{LANG}
591 environment variable.
595 @cindex LC_COLLATE environment variable
597 This specifies what locale to use for string sorting.
600 @cindex LC_CTYPE environment variable
602 This specifies what locale to use for character sets and character
606 @cindex LC_MONETARY environment variable
608 This specifies what locale to use for formatting monetary values.
611 @cindex LC_NUMERIC environment variable
613 This specifies what locale to use for formatting numbers.
616 @cindex LC_TIME environment variable
618 This specifies what locale to use for formatting date/time values.
620 @item _POSIX_OPTION_ORDER
621 @cindex _POSIX_OPTION_ORDER environment variable.
623 If this environment variable is defined, it suppresses the usual
624 reordering of command line arguments by @code{getopt}. @xref{Argument Syntax}.
626 @c !!! GNU also has COREFILE, CORESERVER, EXECSERVERS
629 @node Program Termination
630 @section Program Termination
631 @cindex program termination
632 @cindex process termination
634 @cindex exit status value
635 The usual way for a program to terminate is simply for its @code{main}
636 function to return. The @dfn{exit status value} returned from the
637 @code{main} function is used to report information back to the process's
638 parent process or shell.
640 A program can also terminate normally by calling the @code{exit}
643 In addition, programs can be terminated by signals; this is discussed in
644 more detail in @ref{Signal Handling}. The @code{abort} function causes
645 a signal that kills the program.
648 * Normal Termination:: If a program calls @code{exit}, a
649 process terminates normally.
650 * Exit Status:: The @code{exit status} provides information
651 about why the process terminated.
652 * Cleanups on Exit:: A process can run its own cleanup
653 functions upon normal termination.
654 * Aborting a Program:: The @code{abort} function causes
655 abnormal program termination.
656 * Termination Internals:: What happens when a process terminates.
659 @node Normal Termination
660 @subsection Normal Termination
662 A process terminates normally when the program calls @code{exit}.
663 Returning from @code{main} is equivalent to calling @code{exit}, and
664 the value that @code{main} returns is used as the argument to @code{exit}.
668 @deftypefun void exit (int @var{status})
669 The @code{exit} function terminates the process with status
670 @var{status}. This function does not return.
673 Normal termination causes the following actions:
677 Functions that were registered with the @code{atexit} or @code{on_exit}
678 functions are called in the reverse order of their registration. This
679 mechanism allows your application to specify its own ``cleanup'' actions
680 to be performed at program termination. Typically, this is used to do
681 things like saving program state information in a file, or unlocking
682 locks in shared data bases.
685 All open streams are closed, writing out any buffered output data. See
686 @ref{Closing Streams}. In addition, temporary files opened
687 with the @code{tmpfile} function are removed; see @ref{Temporary Files}.
690 @code{_exit} is called, terminating the program. @xref{Termination Internals}.
694 @subsection Exit Status
697 When a program exits, it can return to the parent process a small
698 amount of information about the cause of termination, using the
699 @dfn{exit status}. This is a value between 0 and 255 that the exiting
700 process passes as an argument to @code{exit}.
702 Normally you should use the exit status to report very broad information
703 about success or failure. You can't provide a lot of detail about the
704 reasons for the failure, and most parent processes would not want much
707 There are conventions for what sorts of status values certain programs
708 should return. The most common convention is simply 0 for success and 1
709 for failure. Programs that perform comparison use a different
710 convention: they use status 1 to indicate a mismatch, and status 2 to
711 indicate an inability to compare. Your program should follow an
712 existing convention if an existing convention makes sense for it.
714 A general convention reserves status values 128 and up for special
715 purposes. In particular, the value 128 is used to indicate failure to
716 execute another program in a subprocess. This convention is not
717 universally obeyed, but it is a good idea to follow it in your programs.
719 @strong{Warning:} Don't try to use the number of errors as the exit
720 status. This is actually not very useful; a parent process would
721 generally not care how many errors occurred. Worse than that, it does
722 not work, because the status value is truncated to eight bits.
723 Thus, if the program tried to report 256 errors, the parent would
724 receive a report of 0 errors---that is, success.
726 For the same reason, it does not work to use the value of @code{errno}
727 as the exit status---these can exceed 255.
729 @strong{Portability note:} Some non-POSIX systems use different
730 conventions for exit status values. For greater portability, you can
731 use the macros @code{EXIT_SUCCESS} and @code{EXIT_FAILURE} for the
732 conventional status value for success and failure, respectively. They
733 are declared in the file @file{stdlib.h}.
738 @deftypevr Macro int EXIT_SUCCESS
739 This macro can be used with the @code{exit} function to indicate
740 successful program completion.
742 On POSIX systems, the value of this macro is @code{0}. On other
743 systems, the value might be some other (possibly non-constant) integer
749 @deftypevr Macro int EXIT_FAILURE
750 This macro can be used with the @code{exit} function to indicate
751 unsuccessful program completion in a general sense.
753 On POSIX systems, the value of this macro is @code{1}. On other
754 systems, the value might be some other (possibly non-constant) integer
755 expression. Other nonzero status values also indicate future. Certain
756 programs use different nonzero status values to indicate particular
757 kinds of "non-success". For example, @code{diff} uses status value
758 @code{1} to mean that the files are different, and @code{2} or more to
759 mean that there was difficulty in opening the files.
762 @node Cleanups on Exit
763 @subsection Cleanups on Exit
765 Your program can arrange to run its own cleanup functions if normal
766 termination happens. If you are writing a library for use in various
767 application programs, then it is unreliable to insist that all
768 applications call the library's cleanup functions explicitly before
769 exiting. It is much more robust to make the cleanup invisible to the
770 application, by setting up a cleanup function in the library itself
771 using @code{atexit} or @code{on_exit}.
775 @deftypefun int atexit (void (*@var{function}) (void))
776 The @code{atexit} function registers the function @var{function} to be
777 called at normal program termination. The @var{function} is called with
780 The return value from @code{atexit} is zero on success and nonzero if
781 the function cannot be registered.
786 @deftypefun int on_exit (void (*@var{function})(int @var{status}, void *@var{arg}), void *@var{arg})
787 This function is a somewhat more powerful variant of @code{atexit}. It
788 accepts two arguments, a function @var{function} and an arbitrary
789 pointer @var{arg}. At normal program termination, the @var{function} is
790 called with two arguments: the @var{status} value passed to @code{exit},
793 This function is included in the GNU C library only for compatibility
794 for SunOS, and may not be supported by other implementations.
797 Here's a trivial program that illustrates the use of @code{exit} and
801 @include atexit.c.texi
805 When this program is executed, it just prints the message and exits.
807 @node Aborting a Program
808 @subsection Aborting a Program
809 @cindex aborting a program
811 You can abort your program using the @code{abort} function. The prototype
812 for this function is in @file{stdlib.h}.
817 @deftypefun void abort (void)
818 The @code{abort} function causes abnormal program termination. This
819 does not execute cleanup functions registered with @code{atexit} or
822 This function actually terminates the process by raising a
823 @code{SIGABRT} signal, and your program can include a handler to
824 intercept this signal; see @ref{Signal Handling}.
827 @c Put in by rms. Don't remove.
829 @strong{Future Change Warning:} Proposed Federal censorship regulations
830 may prohibit us from giving you information about the possibility of
831 calling this function. We would be required to say that this is not an
832 acceptable way of terminating a program.
835 @node Termination Internals
836 @subsection Termination Internals
838 The @code{_exit} function is the primitive used for process termination
839 by @code{exit}. It is declared in the header file @file{unistd.h}.
844 @deftypefun void _exit (int @var{status})
845 The @code{_exit} function is the primitive for causing a process to
846 terminate with status @var{status}. Calling this function does not
847 execute cleanup functions registered with @code{atexit} or
851 When a process terminates for any reason---either by an explicit
852 termination call, or termination as a result of a signal---the
853 following things happen:
857 All open file descriptors in the process are closed. @xref{Low-Level I/O}.
860 The low-order 8 bits of the return status code are saved to be reported
861 back to the parent process via @code{wait} or @code{waitpid}; see
862 @ref{Process Completion}.
865 Any child processes of the process being terminated are assigned a new
866 parent process. (This is the @code{init} process, with process ID 1.)
869 A @code{SIGCHLD} signal is sent to the parent process.
872 If the process is a session leader that has a controlling terminal, then
873 a @code{SIGHUP} signal is sent to each process in the foreground job,
874 and the controlling terminal is disassociated from that session.
878 If termination of a process causes a process group to become orphaned,
879 and any member of that process group is stopped, then a @code{SIGHUP}
880 signal and a @code{SIGCONT} signal are sent to each process in the
881 group. @xref{Job Control}.