?? library_22.html
字號:
use this environment variable, as do many shells and other utilities
which are implemented in terms of those functions.
<P>
The syntax of a path is a sequence of directory names separated by
colons. An empty string instead of a directory name stands for the
current directory (see section <A HREF="library_13.html#SEC188" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_13.html#SEC188">Working Directory</A>).
<P>
A typical value for this environment variable might be a string like:
<P>
<PRE>
:/bin:/etc:/usr/bin:/usr/new/X11:/usr/new:/usr/local:/usr/local/bin
</PRE>
<P>
This means that if the user tries to execute a program named <CODE>foo</CODE>,
the system will look for files named <TT>`foo'</TT>, <TT>`/bin/foo'</TT>,
<TT>`/etc/foo'</TT>, and so on. The first of these files that exists is
the one that is executed.
<P>
<A NAME="IDX1656"></A>
<DT><CODE>TERM</CODE>
<DD><P>
This specifies the kind of terminal that is receiving program output.
Some programs can make use of this information to take advantage of
special escape sequences or terminal modes supported by particular kinds
of terminals. Many programs which use the termcap library
(see section 'Finding a Terminal Description' in <CITE>The Termcap Library Manual</CITE>) use the <CODE>TERM</CODE> environment variable, for example.
<P>
<A NAME="IDX1657"></A>
<DT><CODE>TZ</CODE>
<DD><P>
This specifies the time zone. See section <A HREF="library_19.html#SEC318" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_19.html#SEC318">Specifying the Time Zone with <CODE>TZ</CODE></A>, for information about
the format of this string and how it is used.
<P>
<A NAME="IDX1658"></A>
<DT><CODE>LANG</CODE>
<DD><P>
This specifies the default locale to use for attribute categories where
neither <CODE>LC_ALL</CODE> nor the specific environment variable for that
category is set. See section <A HREF="library_7.html#SEC76" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_7.html#SEC76">Locales and Internationalization</A>, for more information about
locales.
<P>
<A NAME="IDX1659"></A>
<DT><CODE>LC_COLLATE</CODE>
<DD><P>
This specifies what locale to use for string sorting.
<P>
<A NAME="IDX1660"></A>
<DT><CODE>LC_CTYPE</CODE>
<DD><P>
This specifies what locale to use for character sets and character
classification.
<P>
<A NAME="IDX1661"></A>
<DT><CODE>LC_MONETARY</CODE>
<DD><P>
This specifies what locale to use for formatting monetary values.
<P>
<A NAME="IDX1662"></A>
<DT><CODE>LC_NUMERIC</CODE>
<DD><P>
This specifies what locale to use for formatting numbers.
<P>
<A NAME="IDX1663"></A>
<DT><CODE>LC_TIME</CODE>
<DD><P>
This specifies what locale to use for formatting date/time values.
<P>
<A NAME="IDX1664"></A>
<DT><CODE>_POSIX_OPTION_ORDER</CODE>
<DD><P>
If this environment variable is defined, it suppresses the usual
reordering of command line arguments by <CODE>getopt</CODE>. See section <A HREF="library_22.html#SEC387" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_22.html#SEC387">Program Argument Syntax Conventions</A>.
<P>
</DL>
<P>
<A NAME="IDX1665"></A>
<A NAME="IDX1666"></A>
<H2><A NAME="SEC395" HREF="library_toc.html#SEC395" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC395">Program Termination</A></H2>
<A NAME="IDX1667"></A>
<P>
The usual way for a program to terminate is simply for its <CODE>main</CODE>
function to return. The <DFN>exit status value</DFN> returned from the
<CODE>main</CODE> function is used to report information back to the process's
parent process or shell.
<P>
A program can also terminate normally by calling the <CODE>exit</CODE>
function.
<P>
In addition, programs can be terminated by signals; this is discussed in
more detail in section <A HREF="library_21.html#SEC330" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC330">Signal Handling</A>. The <CODE>abort</CODE> function causes
a signal that kills the program.
<P>
<H3><A NAME="SEC396" HREF="library_toc.html#SEC396" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC396">Normal Termination</A></H3>
<P>
A process terminates normally when the program calls <CODE>exit</CODE>.
Returning from <CODE>main</CODE> is equivalent to calling <CODE>exit</CODE>, and
the value that <CODE>main</CODE> returns is used as the argument to <CODE>exit</CODE>.
<P>
<A NAME="IDX1668"></A>
<U>Function:</U> void <B>exit</B> <I>(int <VAR>status</VAR>)</I><P>
The <CODE>exit</CODE> function terminates the process with status
<VAR>status</VAR>. This function does not return.
<P>
Normal termination causes the following actions:
<P>
<OL>
<LI>
Functions that were registered with the <CODE>atexit</CODE> or <CODE>on_exit</CODE>
functions are called in the reverse order of their registration. This
mechanism allows your application to specify its own "cleanup" actions
to be performed at program termination. Typically, this is used to do
things like saving program state information in a file, or unlocking
locks in shared data bases.
<P>
<LI>
All open streams are closed, writing out any buffered output data. See
section <A HREF="library_11.html#SEC121" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC121">Closing Streams</A>. In addition, temporary files opened
with the <CODE>tmpfile</CODE> function are removed; see section <A HREF="library_11.html#SEC164" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC164">Temporary Files</A>.
<P>
<LI>
<CODE>_exit</CODE> is called, terminating the program. See section <A HREF="library_22.html#SEC400" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_22.html#SEC400">Termination Internals</A>.
</OL>
<P>
<A NAME="IDX1669"></A>
<H3><A NAME="SEC397" HREF="library_toc.html#SEC397" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC397">Exit Status</A></H3>
<P>
When a program exits, it can return to the parent process a small
amount of information about the cause of termination, using the
<DFN>exit status</DFN>. This is a value between 0 and 255 that the exiting
process passes as an argument to <CODE>exit</CODE>.
<P>
Normally you should use the exit status to report very broad information
about success or failure. You can't provide a lot of detail about the
reasons for the failure, and most parent processes would not want much
detail anyway.
<P>
There are conventions for what sorts of status values certain programs
should return. The most common convention is simply 0 for success and 1
for failure. Programs that perform comparison use a different
convention: they use status 1 to indicate a mismatch, and status 2 to
indicate an inability to compare. Your program should follow an
existing convention if an existing convention makes sense for it.
<P>
A general convention reserves status values 128 and up for special
purposes. In particular, the value 128 is used to indicate failure to
execute another program in a subprocess. This convention is not
universally obeyed, but it is a good idea to follow it in your programs.
<P>
<STRONG>Warning:</STRONG> Don't try to use the number of errors as the exit
status. This is actually not very useful; a parent process would
generally not care how many errors occurred. Worse than that, it does
not work, because the status value is truncated to eight bits.
Thus, if the program tried to report 256 errors, the parent would
receive a report of 0 errors--that is, success.
<P>
For the same reason, it does not work to use the value of <CODE>errno</CODE>
as the exit status--these can exceed 255.
<P>
<STRONG>Portability note:</STRONG> Some non-POSIX systems use different
conventions for exit status values. For greater portability, you can
use the macros <CODE>EXIT_SUCCESS</CODE> and <CODE>EXIT_FAILURE</CODE> for the
conventional status value for success and failure, respectively. They
are declared in the file <TT>`stdlib.h'</TT>.
<A NAME="IDX1670"></A>
<P>
<A NAME="IDX1671"></A>
<U>Macro:</U> int <B>EXIT_SUCCESS</B><P>
This macro can be used with the <CODE>exit</CODE> function to indicate
successful program completion.
<P>
On POSIX systems, the value of this macro is <CODE>0</CODE>. On other
systems, the value might be some other (possibly non-constant) integer
expression.
<P>
<A NAME="IDX1672"></A>
<U>Macro:</U> int <B>EXIT_FAILURE</B><P>
This macro can be used with the <CODE>exit</CODE> function to indicate
unsuccessful program completion in a general sense.
<P>
On POSIX systems, the value of this macro is <CODE>1</CODE>. On other
systems, the value might be some other (possibly non-constant) integer
expression. Other nonzero status values also indicate future. Certain
programs use different nonzero status values to indicate particular
kinds of "non-success". For example, <CODE>diff</CODE> uses status value
<CODE>1</CODE> to mean that the files are different, and <CODE>2</CODE> or more to
mean that there was difficulty in opening the files.
<P>
<H3><A NAME="SEC398" HREF="library_toc.html#SEC398" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC398">Cleanups on Exit</A></H3>
<P>
Your program can arrange to run its own cleanup functions if normal
termination happens. If you are writing a library for use in various
application programs, then it is unreliable to insist that all
applications call the library's cleanup functions explicitly before
exiting. It is much more robust to make the cleanup invisible to the
application, by setting up a cleanup function in the library itself
using <CODE>atexit</CODE> or <CODE>on_exit</CODE>.
<P>
<A NAME="IDX1673"></A>
<U>Function:</U> int <B>atexit</B> <I>(void (*<VAR>function</VAR>) (void))</I><P>
The <CODE>atexit</CODE> function registers the function <VAR>function</VAR> to be
called at normal program termination. The <VAR>function</VAR> is called with
no arguments.
<P>
The return value from <CODE>atexit</CODE> is zero on success and nonzero if
the function cannot be registered.
<P>
<A NAME="IDX1674"></A>
<U>Function:</U> int <B>on_exit</B> <I>(void (*<VAR>function</VAR>)(int <VAR>status</VAR>, void *<VAR>arg</VAR>), void *<VAR>arg</VAR>)</I><P>
This function is a somewhat more powerful variant of <CODE>atexit</CODE>. It
accepts two arguments, a function <VAR>function</VAR> and an arbitrary
pointer <VAR>arg</VAR>. At normal program termination, the <VAR>function</VAR> is
called with two arguments: the <VAR>status</VAR> value passed to <CODE>exit</CODE>,
and the <VAR>arg</VAR>.
<P>
This function is included in the GNU C library only for compatibility
for SunOS, and may not be supported by other implementations.
<P>
Here's a trivial program that illustrates the use of <CODE>exit</CODE> and
<CODE>atexit</CODE>:
<P>
<PRE>
#include <stdio.h>
#include <stdlib.h>
void
bye (void)
{
puts ("Goodbye, cruel world....");
}
int
main (void)
{
atexit (bye);
exit (EXIT_SUCCESS);
}
</PRE>
<P>
When this program is executed, it just prints the message and exits.
<P>
<A NAME="IDX1675"></A>
<H3><A NAME="SEC399" HREF="library_toc.html#SEC399" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC399">Aborting a Program</A></H3>
<P>
You can abort your program using the <CODE>abort</CODE> function. The prototype
for this function is in <TT>`stdlib.h'</TT>.
<A NAME="IDX1676"></A>
<P>
<A NAME="IDX1677"></A>
<U>Function:</U> void <B>abort</B> <I>(void)</I><P>
The <CODE>abort</CODE> function causes abnormal program termination. This
does not execute cleanup functions registered with <CODE>atexit</CODE> or
<CODE>on_exit</CODE>.
<P>
This function actually terminates the process by raising a
<CODE>SIGABRT</CODE> signal, and your program can include a handler to
intercept this signal; see section <A HREF="library_21.html#SEC330" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC330">Signal Handling</A>.
<P>
<STRONG>Future Change Warning:</STRONG> Proposed Federal censorship regulations
may prohibit us from giving you information about the possibility of
calling this function. We would be required to say that this is not an
acceptable way of terminating a program.
<P>
<H3><A NAME="SEC400" HREF="library_toc.html#SEC400" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC400">Termination Internals</A></H3>
<P>
The <CODE>_exit</CODE> function is the primitive used for process termination
by <CODE>exit</CODE>. It is declared in the header file <TT>`unistd.h'</TT>.
<A NAME="IDX1678"></A>
<P>
<A NAME="IDX1679"></A>
<U>Function:</U> void <B>_exit</B> <I>(int <VAR>status</VAR>)</I><P>
The <CODE>_exit</CODE> function is the primitive for causing a process to
terminate with status <VAR>status</VAR>. Calling this function does not
execute cleanup functions registered with <CODE>atexit</CODE> or
<CODE>on_exit</CODE>.
<P>
When a process terminates for any reason--either by an explicit
termination call, or termination as a result of a signal--the
following things happen:
<P>
<UL>
<LI>
All open file descriptors in the process are closed. See section <A HREF="library_12.html#SEC171" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_12.html#SEC171">Low-Level Input/Output</A>.
<P>
<LI>
The low-order 8 bits of the return status code are saved to be reported
back to the parent process via <CODE>wait</CODE> or <CODE>waitpid</CODE>; see
section <A HREF="library_23.html#SEC407" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC407">Process Completion</A>.
<P>
<LI>
Any child processes of the process being terminated are assigned a new
parent process. (This is the <CODE>init</CODE> process, with process ID 1.)
<P>
<LI>
A <CODE>SIGCHLD</CODE> signal is sent to the parent process.
<P>
<LI>
If the process is a session leader that has a controlling terminal, then
a <CODE>SIGHUP</CODE> signal is sent to each process in the foreground job,
and the controlling terminal is disassociated from that session.
See section <A HREF="library_24.html#SEC411" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html#SEC411">Job Control</A>.
<P>
<LI>
If termination of a process causes a process group to become orphaned,
and any member of that process group is stopped, then a <CODE>SIGHUP</CODE>
signal and a <CODE>SIGCONT</CODE> signal are sent to each process in the
group. See section <A HREF="library_24.html#SEC411" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html#SEC411">Job Control</A>.
</UL>
<P>Go to the <A HREF="library_21.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html">previous</A>, <A HREF="library_23.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html">next</A> section.<P>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -