Avoid "anonymous" code in pthread_spin_lock.
[glibc.git] / manual / startup.texi
blobed75e7bdc3dfec9f2373c2362700189eab0ce763
1 @node Program Basics, Processes, Signal Handling, Top
2 @c %MENU% Writing the beginning and end of your program
3 @chapter The Basic Program/System Interface
5 @cindex process
6 @cindex program
7 @cindex address space
8 @cindex thread of control
9 @dfn{Processes} are the primitive units for allocation of system
10 resources.  Each process has its own address space and (usually) one
11 thread of control.  A process executes a program; you can have multiple
12 processes executing the same program, but each process has its own copy
13 of the program within its own address space and executes it
14 independently of the other copies.  Though it may have multiple threads
15 of control within the same program and a program may be composed of
16 multiple logically separate modules, a process always executes exactly
17 one program.
19 Note that we are using a specific definition of ``program'' for the
20 purposes of this manual, which corresponds to a common definition in the
21 context of Unix system.  In popular usage, ``program'' enjoys a much
22 broader definition; it can refer for example to a system's kernel, an
23 editor macro, a complex package of software, or a discrete section of
24 code executing within a process.
26 Writing the program is what this manual is all about.  This chapter
27 explains the most basic interface between your program and the system
28 that runs, or calls, it.  This includes passing of parameters (arguments
29 and environment) from the system, requesting basic services from the
30 system, and telling the system the program is done.
32 A program starts another program with the @code{exec} family of system calls.
33 This chapter looks at program startup from the execee's point of view.  To
34 see the event from the execor's point of view, see @ref{Executing a File}.
36 @menu
37 * Program Arguments::           Parsing your program's command-line arguments.
38 * Environment Variables::       Less direct parameters affecting your program
39 * System Calls::                Requesting service from the system
40 * Program Termination::         Telling the system you're done; return status
41 @end menu
43 @node Program Arguments
44 @section Program Arguments
45 @cindex program arguments
46 @cindex command line arguments
47 @cindex arguments, to program
49 @cindex program startup
50 @cindex startup of program
51 @cindex invocation of program
52 @cindex @code{main} function
53 @findex main
54 The system starts a C program by calling the function @code{main}.  It
55 is up to you to write a function named @code{main}---otherwise, you
56 won't even be able to link your program without errors.
58 In @w{ISO C} you can define @code{main} either to take no arguments, or to
59 take two arguments that represent the command line arguments to the
60 program, like this:
62 @smallexample
63 int main (int @var{argc}, char *@var{argv}[])
64 @end smallexample
66 @cindex argc (program argument count)
67 @cindex argv (program argument vector)
68 The command line arguments are the whitespace-separated tokens given in
69 the shell command used to invoke the program; thus, in @samp{cat foo
70 bar}, the arguments are @samp{foo} and @samp{bar}.  The only way a
71 program can look at its command line arguments is via the arguments of
72 @code{main}.  If @code{main} doesn't take arguments, then you cannot get
73 at the command line.
75 The value of the @var{argc} argument is the number of command line
76 arguments.  The @var{argv} argument is a vector of C strings; its
77 elements are the individual command line argument strings.  The file
78 name of the program being run is also included in the vector as the
79 first element; the value of @var{argc} counts this element.  A null
80 pointer always follows the last element: @code{@var{argv}[@var{argc}]}
81 is this null pointer.
83 For the command @samp{cat foo bar}, @var{argc} is 3 and @var{argv} has
84 three elements, @code{"cat"}, @code{"foo"} and @code{"bar"}.
86 In Unix systems you can define @code{main} a third way, using three arguments:
88 @smallexample
89 int main (int @var{argc}, char *@var{argv}[], char *@var{envp}[])
90 @end smallexample
92 The first two arguments are just the same.  The third argument
93 @var{envp} gives the program's environment; it is the same as the value
94 of @code{environ}.  @xref{Environment Variables}.  POSIX.1 does not
95 allow this three-argument form, so to be portable it is best to write
96 @code{main} to take two arguments, and use the value of @code{environ}.
98 @menu
99 * Argument Syntax::             By convention, options start with a hyphen.
100 * Parsing Program Arguments::   Ways to parse program options and arguments.
101 @end menu
103 @node Argument Syntax, Parsing Program Arguments, , Program Arguments
104 @subsection Program Argument Syntax Conventions
105 @cindex program argument syntax
106 @cindex syntax, for program arguments
107 @cindex command argument syntax
109 POSIX recommends these conventions for command line arguments.
110 @code{getopt} (@pxref{Getopt}) and @code{argp_parse} (@pxref{Argp}) make
111 it easy to implement them.
113 @itemize @bullet
114 @item
115 Arguments are options if they begin with a hyphen delimiter (@samp{-}).
117 @item
118 Multiple options may follow a hyphen delimiter in a single token if
119 the options do not take arguments.  Thus, @samp{-abc} is equivalent to
120 @samp{-a -b -c}.
122 @item
123 Option names are single alphanumeric characters (as for @code{isalnum};
124 @pxref{Classification of Characters}).
126 @item
127 Certain options require an argument.  For example, the @samp{-o} command
128 of the @code{ld} command requires an argument---an output file name.
130 @item
131 An option and its argument may or may not appear as separate tokens.  (In
132 other words, the whitespace separating them is optional.)  Thus,
133 @w{@samp{-o foo}} and @samp{-ofoo} are equivalent.
135 @item
136 Options typically precede other non-option arguments.
138 The implementations of @code{getopt} and @code{argp_parse} in @theglibc{}
139 normally make it appear as if all the option arguments were
140 specified before all the non-option arguments for the purposes of
141 parsing, even if the user of your program intermixed option and
142 non-option arguments.  They do this by reordering the elements of the
143 @var{argv} array.  This behavior is nonstandard; if you want to suppress
144 it, define the @code{_POSIX_OPTION_ORDER} environment variable.
145 @xref{Standard Environment}.
147 @item
148 The argument @samp{--} terminates all options; any following arguments
149 are treated as non-option arguments, even if they begin with a hyphen.
151 @item
152 A token consisting of a single hyphen character is interpreted as an
153 ordinary non-option argument.  By convention, it is used to specify
154 input from or output to the standard input and output streams.
156 @item
157 Options may be supplied in any order, or appear multiple times.  The
158 interpretation is left up to the particular application program.
159 @end itemize
161 @cindex long-named options
162 GNU adds @dfn{long options} to these conventions.  Long options consist
163 of @samp{--} followed by a name made of alphanumeric characters and
164 dashes.  Option names are typically one to three words long, with
165 hyphens to separate words.  Users can abbreviate the option names as
166 long as the abbreviations are unique.
168 To specify an argument for a long option, write
169 @samp{--@var{name}=@var{value}}.  This syntax enables a long option to
170 accept an argument that is itself optional.
172 Eventually, @gnusystems{} will provide completion for long option names
173 in the shell.
175 @node Parsing Program Arguments, , Argument Syntax, Program Arguments
176 @subsection Parsing Program Arguments
178 @cindex program arguments, parsing
179 @cindex command arguments, parsing
180 @cindex parsing program arguments
181 If the syntax for the command line arguments to your program is simple
182 enough, you can simply pick the arguments off from @var{argv} by hand.
183 But unless your program takes a fixed number of arguments, or all of the
184 arguments are interpreted in the same way (as file names, for example),
185 you are usually better off using @code{getopt} (@pxref{Getopt}) or
186 @code{argp_parse} (@pxref{Argp}) to do the parsing.
188 @code{getopt} is more standard (the short-option only version of it is a
189 part of the POSIX standard), but using @code{argp_parse} is often
190 easier, both for very simple and very complex option structures, because
191 it does more of the dirty work for you.
193 @menu
194 * Getopt::                      Parsing program options using @code{getopt}.
195 * Argp::                        Parsing program options using @code{argp_parse}.
196 * Suboptions::                  Some programs need more detailed options.
197 * Suboptions Example::          This shows how it could be done for @code{mount}.
198 @end menu
200 @c Getopt and argp start at the @section level so that there's
201 @c enough room for their internal hierarchy (mostly a problem with
202 @c argp).         -Miles
204 @include getopt.texi
205 @include argp.texi
207 @node Suboptions, Suboptions Example, Argp, Parsing Program Arguments
208 @c This is a @section so that it's at the same level as getopt and argp
209 @subsubsection Parsing of Suboptions
211 Having a single level of options is sometimes not enough.  There might
212 be too many options which have to be available or a set of options is
213 closely related.
215 For this case some programs use suboptions.  One of the most prominent
216 programs is certainly @code{mount}(8).  The @code{-o} option take one
217 argument which itself is a comma separated list of options.  To ease the
218 programming of code like this the function @code{getsubopt} is
219 available.
221 @comment stdlib.h
222 @deftypefun int getsubopt (char **@var{optionp}, const char* const *@var{tokens}, char **@var{valuep})
224 The @var{optionp} parameter must be a pointer to a variable containing
225 the address of the string to process.  When the function returns the
226 reference is updated to point to the next suboption or to the
227 terminating @samp{\0} character if there is no more suboption available.
229 The @var{tokens} parameter references an array of strings containing the
230 known suboptions.  All strings must be @samp{\0} terminated and to mark
231 the end a null pointer must be stored.  When @code{getsubopt} finds a
232 possible legal suboption it compares it with all strings available in
233 the @var{tokens} array and returns the index in the string as the
234 indicator.
236 In case the suboption has an associated value introduced by a @samp{=}
237 character, a pointer to the value is returned in @var{valuep}.  The
238 string is @samp{\0} terminated.  If no argument is available
239 @var{valuep} is set to the null pointer.  By doing this the caller can
240 check whether a necessary value is given or whether no unexpected value
241 is present.
243 In case the next suboption in the string is not mentioned in the
244 @var{tokens} array the starting address of the suboption including a
245 possible value is returned in @var{valuep} and the return value of the
246 function is @samp{-1}.
247 @end deftypefun
249 @node Suboptions Example, , Suboptions, Parsing Program Arguments
250 @subsection Parsing of Suboptions Example
252 The code which might appear in the @code{mount}(8) program is a perfect
253 example of the use of @code{getsubopt}:
255 @smallexample
256 @include subopt.c.texi
257 @end smallexample
260 @node Environment Variables
261 @section Environment Variables
263 @cindex environment variable
264 When a program is executed, it receives information about the context in
265 which it was invoked in two ways.  The first mechanism uses the
266 @var{argv} and @var{argc} arguments to its @code{main} function, and is
267 discussed in @ref{Program Arguments}.  The second mechanism uses
268 @dfn{environment variables} and is discussed in this section.
270 The @var{argv} mechanism is typically used to pass command-line
271 arguments specific to the particular program being invoked.  The
272 environment, on the other hand, keeps track of information that is
273 shared by many programs, changes infrequently, and that is less
274 frequently used.
276 The environment variables discussed in this section are the same
277 environment variables that you set using assignments and the
278 @code{export} command in the shell.  Programs executed from the shell
279 inherit all of the environment variables from the shell.
280 @c !!! xref to right part of bash manual when it exists
282 @cindex environment
283 Standard environment variables are used for information about the user's
284 home directory, terminal type, current locale, and so on; you can define
285 additional variables for other purposes.  The set of all environment
286 variables that have values is collectively known as the
287 @dfn{environment}.
289 Names of environment variables are case-sensitive and must not contain
290 the character @samp{=}.  System-defined environment variables are
291 invariably uppercase.
293 The values of environment variables can be anything that can be
294 represented as a string.  A value must not contain an embedded null
295 character, since this is assumed to terminate the string.
298 @menu
299 * Environment Access::          How to get and set the values of
300                                  environment variables.
301 * Standard Environment::        These environment variables have
302                                  standard interpretations.
303 @end menu
305 @node Environment Access
306 @subsection Environment Access
307 @cindex environment access
308 @cindex environment representation
310 The value of an environment variable can be accessed with the
311 @code{getenv} function.  This is declared in the header file
312 @file{stdlib.h}.  Modifications of enviroment variables are not
313 allowed in Multi-threaded programs.  The @code{getenv} function
314 can be safely used in multi-threaded programs
315 @pindex stdlib.h
317 @comment stdlib.h
318 @comment ISO
319 @deftypefun {char *} getenv (const char *@var{name})
320 This function returns a string that is the value of the environment
321 variable @var{name}.  You must not modify this string.  In some non-Unix
322 systems not using @theglibc{}, it might be overwritten by subsequent
323 calls to @code{getenv} (but not by any other library function).  If the
324 environment variable @var{name} is not defined, the value is a null
325 pointer.
326 @end deftypefun
329 @comment stdlib.h
330 @comment SVID
331 @deftypefun int putenv (char *@var{string})
332 The @code{putenv} function adds or removes definitions from the environment.
333 If the @var{string} is of the form @samp{@var{name}=@var{value}}, the
334 definition is added to the environment.  Otherwise, the @var{string} is
335 interpreted as the name of an environment variable, and any definition
336 for this variable in the environment is removed.
338 If the function is successful it returns @code{0}.  Otherwise the return
339 value is nonzero and @code{errno} is set to indicate the error.
341 The difference to the @code{setenv} function is that the exact string
342 given as the parameter @var{string} is put into the environment.  If the
343 user should change the string after the @code{putenv} call this will
344 reflect in automatically in the environment.  This also requires that
345 @var{string} is no automatic variable which scope is left before the
346 variable is removed from the environment.  The same applies of course to
347 dynamically allocated variables which are freed later.
349 This function is part of the extended Unix interface.  Since it was also
350 available in old SVID libraries you should define either
351 @var{_XOPEN_SOURCE} or @var{_SVID_SOURCE} before including any header.
352 @end deftypefun
355 @comment stdlib.h
356 @comment BSD
357 @deftypefun int setenv (const char *@var{name}, const char *@var{value}, int @var{replace})
358 The @code{setenv} function can be used to add a new definition to the
359 environment.  The entry with the name @var{name} is replaced by the
360 value @samp{@var{name}=@var{value}}.  Please note that this is also true
361 if @var{value} is the empty string.  To do this a new string is created
362 and the strings @var{name} and @var{value} are copied.  A null pointer
363 for the @var{value} parameter is illegal.  If the environment already
364 contains an entry with key @var{name} the @var{replace} parameter
365 controls the action.  If replace is zero, nothing happens.  Otherwise
366 the old entry is replaced by the new one.
368 Please note that you cannot remove an entry completely using this function.
370 If the function is successful it returns @code{0}.  Otherwise the
371 environment is unchanged and the return value is @code{-1} and
372 @code{errno} is set.
374 This function was originally part of the BSD library but is now part of
375 the Unix standard.
376 @end deftypefun
378 @comment stdlib.h
379 @comment BSD
380 @deftypefun int unsetenv (const char *@var{name})
381 Using this function one can remove an entry completely from the
382 environment.  If the environment contains an entry with the key
383 @var{name} this whole entry is removed.  A call to this function is
384 equivalent to a call to @code{putenv} when the @var{value} part of the
385 string is empty.
387 The function return @code{-1} if @var{name} is a null pointer, points to
388 an empty string, or points to a string containing a @code{=} character.
389 It returns @code{0} if the call succeeded.
391 This function was originally part of the BSD library but is now part of
392 the Unix standard.  The BSD version had no return value, though.
393 @end deftypefun
395 There is one more function to modify the whole environment.  This
396 function is said to be used in the POSIX.9 (POSIX bindings for Fortran
397 77) and so one should expect it did made it into POSIX.1.  But this
398 never happened.  But we still provide this function as a GNU extension
399 to enable writing standard compliant Fortran environments.
401 @comment stdlib.h
402 @comment GNU
403 @deftypefun int clearenv (void)
404 The @code{clearenv} function removes all entries from the environment.
405 Using @code{putenv} and @code{setenv} new entries can be added again
406 later.
408 If the function is successful it returns @code{0}.  Otherwise the return
409 value is nonzero.
410 @end deftypefun
413 You can deal directly with the underlying representation of environment
414 objects to add more variables to the environment (for example, to
415 communicate with another program you are about to execute;
416 @pxref{Executing a File}).
418 @comment unistd.h
419 @comment POSIX.1
420 @deftypevar {char **} environ
421 The environment is represented as an array of strings.  Each string is
422 of the format @samp{@var{name}=@var{value}}.  The order in which
423 strings appear in the environment is not significant, but the same
424 @var{name} must not appear more than once.  The last element of the
425 array is a null pointer.
427 This variable is declared in the header file @file{unistd.h}.
429 If you just want to get the value of an environment variable, use
430 @code{getenv}.
431 @end deftypevar
433 Unix systems, and @gnusystems{}, pass the initial value of
434 @code{environ} as the third argument to @code{main}.
435 @xref{Program Arguments}.
437 @node Standard Environment
438 @subsection Standard Environment Variables
439 @cindex standard environment variables
441 These environment variables have standard meanings.  This doesn't mean
442 that they are always present in the environment; but if these variables
443 @emph{are} present, they have these meanings.  You shouldn't try to use
444 these environment variable names for some other purpose.
446 @comment Extra blank lines make it look better.
447 @table @code
448 @item HOME
449 @cindex @code{HOME} environment variable
450 @cindex home directory
452 This is a string representing the user's @dfn{home directory}, or
453 initial default working directory.
455 The user can set @code{HOME} to any value.
456 If you need to make sure to obtain the proper home directory
457 for a particular user, you should not use @code{HOME}; instead,
458 look up the user's name in the user database (@pxref{User Database}).
460 For most purposes, it is better to use @code{HOME}, precisely because
461 this lets the user specify the value.
463 @c !!! also USER
464 @item LOGNAME
465 @cindex @code{LOGNAME} environment variable
467 This is the name that the user used to log in.  Since the value in the
468 environment can be tweaked arbitrarily, this is not a reliable way to
469 identify the user who is running a program; a function like
470 @code{getlogin} (@pxref{Who Logged In}) is better for that purpose.
472 For most purposes, it is better to use @code{LOGNAME}, precisely because
473 this lets the user specify the value.
475 @item PATH
476 @cindex @code{PATH} environment variable
478 A @dfn{path} is a sequence of directory names which is used for
479 searching for a file.  The variable @code{PATH} holds a path used
480 for searching for programs to be run.
482 The @code{execlp} and @code{execvp} functions (@pxref{Executing a File})
483 use this environment variable, as do many shells and other utilities
484 which are implemented in terms of those functions.
486 The syntax of a path is a sequence of directory names separated by
487 colons.  An empty string instead of a directory name stands for the
488 current directory (@pxref{Working Directory}).
490 A typical value for this environment variable might be a string like:
492 @smallexample
493 :/bin:/etc:/usr/bin:/usr/new/X11:/usr/new:/usr/local/bin
494 @end smallexample
496 This means that if the user tries to execute a program named @code{foo},
497 the system will look for files named @file{foo}, @file{/bin/foo},
498 @file{/etc/foo}, and so on.  The first of these files that exists is
499 the one that is executed.
501 @c !!! also TERMCAP
502 @item TERM
503 @cindex @code{TERM} environment variable
505 This specifies the kind of terminal that is receiving program output.
506 Some programs can make use of this information to take advantage of
507 special escape sequences or terminal modes supported by particular kinds
508 of terminals.  Many programs which use the termcap library
509 (@pxref{Finding a Terminal Description,Find,,termcap,The Termcap Library
510 Manual}) use the @code{TERM} environment variable, for example.
512 @item TZ
513 @cindex @code{TZ} environment variable
515 This specifies the time zone.  @xref{TZ Variable}, for information about
516 the format of this string and how it is used.
518 @item LANG
519 @cindex @code{LANG} environment variable
521 This specifies the default locale to use for attribute categories where
522 neither @code{LC_ALL} nor the specific environment variable for that
523 category is set.  @xref{Locales}, for more information about
524 locales.
526 @ignore
527 @c I doubt this really exists
528 @item LC_ALL
529 @cindex @code{LC_ALL} environment variable
531 This is similar to the @code{LANG} environment variable.  However, its
532 value takes precedence over any values provided for the individual
533 attribute category environment variables, or for the @code{LANG}
534 environment variable.
535 @end ignore
537 @item LC_ALL
538 @cindex @code{LC_ALL} environment variable
540 If this environment variable is set it overrides the selection for all
541 the locales done using the other @code{LC_*} environment variables.  The
542 value of the other @code{LC_*} environment variables is simply ignored
543 in this case.
545 @item LC_COLLATE
546 @cindex @code{LC_COLLATE} environment variable
548 This specifies what locale to use for string sorting.
550 @item LC_CTYPE
551 @cindex @code{LC_CTYPE} environment variable
553 This specifies what locale to use for character sets and character
554 classification.
556 @item LC_MESSAGES
557 @cindex @code{LC_MESSAGES} environment variable
559 This specifies what locale to use for printing messages and to parse
560 responses.
562 @item LC_MONETARY
563 @cindex @code{LC_MONETARY} environment variable
565 This specifies what locale to use for formatting monetary values.
567 @item LC_NUMERIC
568 @cindex @code{LC_NUMERIC} environment variable
570 This specifies what locale to use for formatting numbers.
572 @item LC_TIME
573 @cindex @code{LC_TIME} environment variable
575 This specifies what locale to use for formatting date/time values.
577 @item NLSPATH
578 @cindex @code{NLSPATH} environment variable
580 This specifies the directories in which the @code{catopen} function
581 looks for message translation catalogs.
583 @item _POSIX_OPTION_ORDER
584 @cindex @code{_POSIX_OPTION_ORDER} environment variable.
586 If this environment variable is defined, it suppresses the usual
587 reordering of command line arguments by @code{getopt} and
588 @code{argp_parse}.  @xref{Argument Syntax}.
590 @c !!! GNU also has COREFILE, CORESERVER, EXECSERVERS
591 @end table
593 @node System Calls
594 @section System Calls
596 @cindex system call
597 A system call is a request for service that a program makes of the
598 kernel.  The service is generally something that only the kernel has
599 the privilege to do, such as doing I/O.  Programmers don't normally
600 need to be concerned with system calls because there are functions in
601 @theglibc{} to do virtually everything that system calls do.
602 These functions work by making system calls themselves.  For example,
603 there is a system call that changes the permissions of a file, but
604 you don't need to know about it because you can just use @theglibc{}'s
605 @code{chmod} function.
607 @cindex kernel call
608 System calls are sometimes called kernel calls.
610 However, there are times when you want to make a system call explicitly,
611 and for that, @theglibc{} provides the @code{syscall} function.
612 @code{syscall} is harder to use and less portable than functions like
613 @code{chmod}, but easier and more portable than coding the system call
614 in assembler instructions.
616 @code{syscall} is most useful when you are working with a system call
617 which is special to your system or is newer than @theglibc{} you
618 are using.  @code{syscall} is implemented in an entirely generic way;
619 the function does not know anything about what a particular system
620 call does or even if it is valid.
622 The description of @code{syscall} in this section assumes a certain
623 protocol for system calls on the various platforms on which @theglibc{}
624 runs.  That protocol is not defined by any strong authority, but
625 we won't describe it here either because anyone who is coding
626 @code{syscall} probably won't accept anything less than kernel and C
627 library source code as a specification of the interface between them
628 anyway.
631 @code{syscall} is declared in @file{unistd.h}.
633 @comment unistd.h
634 @comment ???
635 @deftypefun {long int} syscall (long int @var{sysno}, @dots{})
637 @code{syscall} performs a generic system call.
639 @cindex system call number
640 @var{sysno} is the system call number.  Each kind of system call is
641 identified by a number.  Macros for all the possible system call numbers
642 are defined in @file{sys/syscall.h}
644 The remaining arguments are the arguments for the system call, in
645 order, and their meanings depend on the kind of system call.  Each kind
646 of system call has a definite number of arguments, from zero to five.
647 If you code more arguments than the system call takes, the extra ones to
648 the right are ignored.
650 The return value is the return value from the system call, unless the
651 system call failed.  In that case, @code{syscall} returns @code{-1} and
652 sets @code{errno} to an error code that the system call returned.  Note
653 that system calls do not return @code{-1} when they succeed.
654 @cindex errno
656 If you specify an invalid @var{sysno}, @code{syscall} returns @code{-1}
657 with @code{errno} = @code{ENOSYS}.
659 Example:
661 @smallexample
663 #include <unistd.h>
664 #include <sys/syscall.h>
665 #include <errno.h>
667 @dots{}
669 int rc;
671 rc = syscall(SYS_chmod, "/etc/passwd", 0444);
673 if (rc == -1)
674    fprintf(stderr, "chmod failed, errno = %d\n", errno);
676 @end smallexample
678 This, if all the compatibility stars are aligned, is equivalent to the
679 following preferable code:
681 @smallexample
683 #include <sys/types.h>
684 #include <sys/stat.h>
685 #include <errno.h>
687 @dots{}
689 int rc;
691 rc = chmod("/etc/passwd", 0444);
692 if (rc == -1)
693    fprintf(stderr, "chmod failed, errno = %d\n", errno);
695 @end smallexample
697 @end deftypefun
700 @node Program Termination
701 @section Program Termination
702 @cindex program termination
703 @cindex process termination
705 @cindex exit status value
706 The usual way for a program to terminate is simply for its @code{main}
707 function to return.  The @dfn{exit status value} returned from the
708 @code{main} function is used to report information back to the process's
709 parent process or shell.
711 A program can also terminate normally by calling the @code{exit}
712 function.
714 In addition, programs can be terminated by signals; this is discussed in
715 more detail in @ref{Signal Handling}.  The @code{abort} function causes
716 a signal that kills the program.
718 @menu
719 * Normal Termination::          If a program calls @code{exit}, a
720                                  process terminates normally.
721 * Exit Status::                 The @code{exit status} provides information
722                                  about why the process terminated.
723 * Cleanups on Exit::            A process can run its own cleanup
724                                  functions upon normal termination.
725 * Aborting a Program::          The @code{abort} function causes
726                                  abnormal program termination.
727 * Termination Internals::       What happens when a process terminates.
728 @end menu
730 @node Normal Termination
731 @subsection Normal Termination
733 A process terminates normally when its program signals it is done by
734 calling @code{exit}.  Returning from @code{main} is equivalent to
735 calling @code{exit}, and the value that @code{main} returns is used as
736 the argument to @code{exit}.
738 @comment stdlib.h
739 @comment ISO
740 @deftypefun void exit (int @var{status})
741 The @code{exit} function tells the system that the program is done, which
742 causes it to terminate the process.
744 @var{status} is the program's exit status, which becomes part of the
745 process' termination status.  This function does not return.
746 @end deftypefun
748 Normal termination causes the following actions:
750 @enumerate
751 @item
752 Functions that were registered with the @code{atexit} or @code{on_exit}
753 functions are called in the reverse order of their registration.  This
754 mechanism allows your application to specify its own ``cleanup'' actions
755 to be performed at program termination.  Typically, this is used to do
756 things like saving program state information in a file, or unlocking
757 locks in shared data bases.
759 @item
760 All open streams are closed, writing out any buffered output data.  See
761 @ref{Closing Streams}.  In addition, temporary files opened
762 with the @code{tmpfile} function are removed; see @ref{Temporary Files}.
764 @item
765 @code{_exit} is called, terminating the program.  @xref{Termination Internals}.
766 @end enumerate
768 @node Exit Status
769 @subsection Exit Status
770 @cindex exit status
772 When a program exits, it can return to the parent process a small
773 amount of information about the cause of termination, using the
774 @dfn{exit status}.  This is a value between 0 and 255 that the exiting
775 process passes as an argument to @code{exit}.
777 Normally you should use the exit status to report very broad information
778 about success or failure.  You can't provide a lot of detail about the
779 reasons for the failure, and most parent processes would not want much
780 detail anyway.
782 There are conventions for what sorts of status values certain programs
783 should return.  The most common convention is simply 0 for success and 1
784 for failure.  Programs that perform comparison use a different
785 convention: they use status 1 to indicate a mismatch, and status 2 to
786 indicate an inability to compare.  Your program should follow an
787 existing convention if an existing convention makes sense for it.
789 A general convention reserves status values 128 and up for special
790 purposes.  In particular, the value 128 is used to indicate failure to
791 execute another program in a subprocess.  This convention is not
792 universally obeyed, but it is a good idea to follow it in your programs.
794 @strong{Warning:} Don't try to use the number of errors as the exit
795 status.  This is actually not very useful; a parent process would
796 generally not care how many errors occurred.  Worse than that, it does
797 not work, because the status value is truncated to eight bits.
798 Thus, if the program tried to report 256 errors, the parent would
799 receive a report of 0 errors---that is, success.
801 For the same reason, it does not work to use the value of @code{errno}
802 as the exit status---these can exceed 255.
804 @strong{Portability note:} Some non-POSIX systems use different
805 conventions for exit status values.  For greater portability, you can
806 use the macros @code{EXIT_SUCCESS} and @code{EXIT_FAILURE} for the
807 conventional status value for success and failure, respectively.  They
808 are declared in the file @file{stdlib.h}.
809 @pindex stdlib.h
811 @comment stdlib.h
812 @comment ISO
813 @deftypevr Macro int EXIT_SUCCESS
814 This macro can be used with the @code{exit} function to indicate
815 successful program completion.
817 On POSIX systems, the value of this macro is @code{0}.  On other
818 systems, the value might be some other (possibly non-constant) integer
819 expression.
820 @end deftypevr
822 @comment stdlib.h
823 @comment ISO
824 @deftypevr Macro int EXIT_FAILURE
825 This macro can be used with the @code{exit} function to indicate
826 unsuccessful program completion in a general sense.
828 On POSIX systems, the value of this macro is @code{1}.  On other
829 systems, the value might be some other (possibly non-constant) integer
830 expression.  Other nonzero status values also indicate failures.  Certain
831 programs use different nonzero status values to indicate particular
832 kinds of "non-success".  For example, @code{diff} uses status value
833 @code{1} to mean that the files are different, and @code{2} or more to
834 mean that there was difficulty in opening the files.
835 @end deftypevr
837 Don't confuse a program's exit status with a process' termination status.
838 There are lots of ways a process can terminate besides having its program
839 finish.  In the event that the process termination @emph{is} caused by program
840 termination (i.e., @code{exit}), though, the program's exit status becomes
841 part of the process' termination status.
843 @node Cleanups on Exit
844 @subsection Cleanups on Exit
846 Your program can arrange to run its own cleanup functions if normal
847 termination happens.  If you are writing a library for use in various
848 application programs, then it is unreliable to insist that all
849 applications call the library's cleanup functions explicitly before
850 exiting.  It is much more robust to make the cleanup invisible to the
851 application, by setting up a cleanup function in the library itself
852 using @code{atexit} or @code{on_exit}.
854 @comment stdlib.h
855 @comment ISO
856 @deftypefun int atexit (void (*@var{function}) (void))
857 The @code{atexit} function registers the function @var{function} to be
858 called at normal program termination.  The @var{function} is called with
859 no arguments.
861 The return value from @code{atexit} is zero on success and nonzero if
862 the function cannot be registered.
863 @end deftypefun
865 @comment stdlib.h
866 @comment SunOS
867 @deftypefun int on_exit (void (*@var{function})(int @var{status}, void *@var{arg}), void *@var{arg})
868 This function is a somewhat more powerful variant of @code{atexit}.  It
869 accepts two arguments, a function @var{function} and an arbitrary
870 pointer @var{arg}.  At normal program termination, the @var{function} is
871 called with two arguments:  the @var{status} value passed to @code{exit},
872 and the @var{arg}.
874 This function is included in @theglibc{} only for compatibility
875 for SunOS, and may not be supported by other implementations.
876 @end deftypefun
878 Here's a trivial program that illustrates the use of @code{exit} and
879 @code{atexit}:
881 @smallexample
882 @include atexit.c.texi
883 @end smallexample
885 @noindent
886 When this program is executed, it just prints the message and exits.
888 @node Aborting a Program
889 @subsection Aborting a Program
890 @cindex aborting a program
892 You can abort your program using the @code{abort} function.  The prototype
893 for this function is in @file{stdlib.h}.
894 @pindex stdlib.h
896 @comment stdlib.h
897 @comment ISO
898 @deftypefun void abort (void)
899 The @code{abort} function causes abnormal program termination.  This
900 does not execute cleanup functions registered with @code{atexit} or
901 @code{on_exit}.
903 This function actually terminates the process by raising a
904 @code{SIGABRT} signal, and your program can include a handler to
905 intercept this signal; see @ref{Signal Handling}.
906 @end deftypefun
908 @c Put in by rms.  Don't remove.
909 @cartouche
910 @strong{Future Change Warning:} Proposed Federal censorship regulations
911 may prohibit us from giving you information about the possibility of
912 calling this function.  We would be required to say that this is not an
913 acceptable way of terminating a program.
914 @end cartouche
916 @node Termination Internals
917 @subsection Termination Internals
919 The @code{_exit} function is the primitive used for process termination
920 by @code{exit}.  It is declared in the header file @file{unistd.h}.
921 @pindex unistd.h
923 @comment unistd.h
924 @comment POSIX.1
925 @deftypefun void _exit (int @var{status})
926 The @code{_exit} function is the primitive for causing a process to
927 terminate with status @var{status}.  Calling this function does not
928 execute cleanup functions registered with @code{atexit} or
929 @code{on_exit}.
930 @end deftypefun
932 @comment stdlib.h
933 @comment ISO
934 @deftypefun void _Exit (int @var{status})
935 The @code{_Exit} function is the @w{ISO C} equivalent to @code{_exit}.
936 The @w{ISO C} committee members were not sure whether the definitions of
937 @code{_exit} and @code{_Exit} were compatible so they have not used the
938 POSIX name.
940 This function was introduced in @w{ISO C99} and is declared in
941 @file{stdlib.h}.
942 @end deftypefun
944 When a process terminates for any reason---either because the program
945 terminates, or as a result of a signal---the
946 following things happen:
948 @itemize @bullet
949 @item
950 All open file descriptors in the process are closed.  @xref{Low-Level I/O}.
951 Note that streams are not flushed automatically when the process
952 terminates; see @ref{I/O on Streams}.
954 @item
955 A process exit status is saved to be reported back to the parent process
956 via @code{wait} or @code{waitpid}; see @ref{Process Completion}.  If the
957 program exited, this status includes as its low-order 8 bits the program
958 exit status.
961 @item
962 Any child processes of the process being terminated are assigned a new
963 parent process.  (On most systems, including GNU, this is the @code{init}
964 process, with process ID 1.)
966 @item
967 A @code{SIGCHLD} signal is sent to the parent process.
969 @item
970 If the process is a session leader that has a controlling terminal, then
971 a @code{SIGHUP} signal is sent to each process in the foreground job,
972 and the controlling terminal is disassociated from that session.
973 @xref{Job Control}.
975 @item
976 If termination of a process causes a process group to become orphaned,
977 and any member of that process group is stopped, then a @code{SIGHUP}
978 signal and a @code{SIGCONT} signal are sent to each process in the
979 group.  @xref{Job Control}.
980 @end itemize