⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 library_22.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 3 页
字号:
use this environment variable, as do many shells and other utilitieswhich are implemented in terms of those functions.<P>The syntax of a path is a sequence of directory names separated bycolons.  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 isthe 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 ofspecial escape sequences or terminal modes supported by particular kindsof 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 aboutthe 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 whereneither <CODE>LC_ALL</CODE> nor the specific environment variable for thatcategory 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 aboutlocales.<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 characterclassification.<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 usualreordering 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'sparent 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 inmore 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 causesa 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>, andthe 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.  Thismechanism allows your application to specify its own "cleanup" actionsto be performed at program termination.  Typically, this is used to dothings like saving program state information in a file, or unlockinglocks in shared data bases.<P><LI>All open streams are closed, writing out any buffered output data.  Seesection <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 openedwith 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 smallamount of information about the cause of termination, using the<DFN>exit status</DFN>.  This is a value between 0 and 255 that the exitingprocess passes as an argument to <CODE>exit</CODE>.<P>Normally you should use the exit status to report very broad informationabout success or failure.  You can't provide a lot of detail about thereasons for the failure, and most parent processes would not want muchdetail anyway.<P>There are conventions for what sorts of status values certain programsshould return.  The most common convention is simply 0 for success and 1for failure.  Programs that perform comparison use a differentconvention: they use status 1 to indicate a mismatch, and status 2 toindicate an inability to compare.  Your program should follow anexisting convention if an existing convention makes sense for it.<P>A general convention reserves status values 128 and up for specialpurposes.  In particular, the value 128 is used to indicate failure toexecute another program in a subprocess.  This convention is notuniversally 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 exitstatus.  This is actually not very useful; a parent process wouldgenerally not care how many errors occurred.  Worse than that, it doesnot work, because the status value is truncated to eight bits.Thus, if the program tried to report 256 errors, the parent wouldreceive 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 differentconventions for exit status values.  For greater portability, you canuse the macros <CODE>EXIT_SUCCESS</CODE> and <CODE>EXIT_FAILURE</CODE> for theconventional status value for success and failure, respectively.  Theyare 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 indicatesuccessful program completion.<P>On POSIX systems, the value of this macro is <CODE>0</CODE>.  On othersystems, the value might be some other (possibly non-constant) integerexpression.<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 indicateunsuccessful program completion in a general sense.<P>On POSIX systems, the value of this macro is <CODE>1</CODE>.  On othersystems, the value might be some other (possibly non-constant) integerexpression.  Other nonzero status values also indicate future.  Certainprograms use different nonzero status values to indicate particularkinds 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 tomean 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 normaltermination happens.  If you are writing a library for use in variousapplication programs, then it is unreliable to insist that allapplications call the library's cleanup functions explicitly beforeexiting.  It is much more robust to make the cleanup invisible to theapplication, by setting up a cleanup function in the library itselfusing <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 becalled at normal program termination.  The <VAR>function</VAR> is called withno arguments.<P>The return value from <CODE>atexit</CODE> is zero on success and nonzero ifthe 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>.  Itaccepts two arguments, a function <VAR>function</VAR> and an arbitrarypointer <VAR>arg</VAR>.  At normal program termination, the <VAR>function</VAR> iscalled 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 compatibilityfor 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 &#60;stdio.h&#62;#include &#60;stdlib.h&#62;void bye (void){  puts ("Goodbye, cruel world....");}intmain (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 prototypefor 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.  Thisdoes 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 tointercept 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 regulationsmay prohibit us from giving you information about the possibility ofcalling this function.  We would be required to say that this is not anacceptable 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 terminationby <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 toterminate with status <VAR>status</VAR>.  Calling this function does notexecute cleanup functions registered with <CODE>atexit</CODE> or<CODE>on_exit</CODE>.<P>When a process terminates for any reason--either by an explicittermination call, or termination as a result of a signal--thefollowing 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 reportedback to the parent process via <CODE>wait</CODE> or <CODE>waitpid</CODE>; seesection <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 newparent 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, thena <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 thegroup.  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 + -