📄 library_21.html
字号:
<!-- This HTML file has been created by texi2html 1.27 from library.texinfo on 3 March 1994 --><TITLE>The GNU C Library - Signal Handling</TITLE><P>Go to the <A HREF="library_20.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_20.html">previous</A>, <A HREF="library_22.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_22.html">next</A> section.<P><H1><A NAME="SEC330" HREF="library_toc.html#SEC330" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC330">Signal Handling</A></H1><A NAME="IDX1452"></A><P>A <DFN>signal</DFN> is a software interrupt delivered to a process. Theoperating system uses signals to report exceptional situations to anexecuting program. Some signals report errors such as references toinvalid memory addresses; others report asynchronous events, such asdisconnection of a phone line.<P>The GNU C library defines a variety of signal types, each for aparticular kind of event. Some kinds of events make it inadvisable orimpossible for the program to proceed as usual, and the correspondingsignals normally abort the program. Other kinds of signals that reportharmless events are ignored by default.<P>If you anticipate an event that causes signals, you can define a handlerfunction and tell the operating system to run it when that particulartype of signal arrives.<P>Finally, one process can send a signal to another process; this allows aparent process to abort a child, or two related processes to communicateand synchronize.<P><H2><A NAME="SEC331" HREF="library_toc.html#SEC331" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC331">Basic Concepts of Signals</A></H2><P>This section explains basic concepts of how signals are generated, whathappens after a signal is delivered, and how programs can handlesignals.<P><H3><A NAME="SEC332" HREF="library_toc.html#SEC332" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC332">Some Kinds of Signals</A></H3><P>A signal reports the occurrence of an exceptional event. These are someof the events that can cause (or <DFN>generate</DFN>, or <DFN>raise</DFN>) asignal:<P><UL><LI>A program error such as dividing by zero or issuing an address outsidethe valid range.<P><LI>A user request to interrupt or terminate the program. Most environmentsare set up to let a user suspend the program by typing <KBD>C-z</KBD>, orterminate it with <KBD>C-c</KBD>. Whatever key sequence is used, theoperating system sends the proper signal to interrupt the process.<P><LI>The termination of a child process.<P><LI>Expiration of a timer or alarm.<P><LI>A call to <CODE>kill</CODE> or <CODE>raise</CODE> by the same process.<P><LI>A call to <CODE>kill</CODE> from another process. Signals are a limited butuseful form of interprocess communication.</UL><P>Each of these kinds of events (excepting explicit calls to <CODE>kill</CODE>and <CODE>raise</CODE>) generates its own particular kind of signal. Thevarious kinds of signals are listed and described in detail insection <A HREF="library_21.html#SEC335" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC335">Standard Signals</A>.<P><A NAME="IDX1453"></A><H3><A NAME="SEC333" HREF="library_toc.html#SEC333" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC333">Concepts of Signal Generation</A></H3><P>In general, the events that generate signals fall into three majorcategories: errors, external events, and explicit requests.<P>An error means that a program has done something invalid and cannotcontinue execution. But not all kinds of errors generate signals--infact, most do not. For example, opening a nonexistant file is an error,but it does not raise a signal; instead, <CODE>open</CODE> returns <CODE>-1</CODE>.In general, errors that are necessarily associated with certain libraryfunctions are reported by returning a value that indicates an error.The errors which raise signals are those which can happen anywhere inthe program, not just in library calls. These include division by zeroand invalid memory addresses.<P>An external event generally has to do with I/O or other processes.These include the arrival of input, the expiration of a timer, and thetermination of a child process.<P>An explicit request means the use of a library function such as<CODE>kill</CODE> whose purpose is specifically to generate a signal.<P>Signals may be generated <DFN>synchronously</DFN> or <DFN>asynchronously</DFN>. Asynchronous signal pertains to a specific action in the program, and isdelivered (unless blocked) during that action. Errors generate signalssynchronously, and so do explicit requests by a process to generate asignal for that same process.<P>Asynchronous signals are generated by events outside the control of theprocess that receives them. These signals arrive at unpredictable timesduring execution. External events generate signals asynchronously, andso do explicit requests that apply to some other process.<P>A given type of signal is either typically synchrous or typicallyasynchronous. For example, signals for errors are typically synchronousbecause errors generate signals synchronously. But any type of signalcan be generated synchronously or asynchronously with an explicitrequest.<P><A NAME="IDX1454"></A><A NAME="IDX1455"></A><A NAME="IDX1456"></A><H3><A NAME="SEC334" HREF="library_toc.html#SEC334" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC334">How Signals Are Delivered</A></H3><P>When a signal is generated, it becomes <DFN>pending</DFN>. Normally itremains pending for just a short period of time and then is<DFN>delivered</DFN> to the process that was signaled. However, if that kindof signal is currently <DFN>blocked</DFN>, it may remain pendingindefinitely--until signals of that kind are <DFN>unblocked</DFN>. Onceunblocked, it will be delivered immediately. See section <A HREF="library_21.html#SEC368" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC368">Blocking Signals</A>.<A NAME="IDX1457"></A><A NAME="IDX1458"></A><A NAME="IDX1459"></A><A NAME="IDX1460"></A><P>When the signal is delivered, whether right away or after a long delay,the <DFN>specified action</DFN> for that signal is taken. For certainsignals, such as <CODE>SIGKILL</CODE> and <CODE>SIGSTOP</CODE>, the action is fixed,but for most signals, the program has a choice: ignore the signal,specify a <DFN>handler function</DFN>, or accept the <DFN>default action</DFN> forthat kind of signal. The program specifies its choice using functionssuch as <CODE>signal</CODE> or <CODE>sigaction</CODE> (see section <A HREF="library_21.html#SEC344" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC344">Specifying Signal Actions</A>). Wesometimes say that a handler <DFN>catches</DFN> the signal. While thehandler is running, that particular signal is normally blocked.<P>If the specified action for a kind of signal is to ignore it, then anysuch signal which is generated is discarded immediately. This happenseven if the signal is also blocked at the time. A signal discarded inthis way will never be delivered, not even if the program subsequentlyspecifies a different action for that kind of signal and then unblocksit.<P>If a signal arrives which the program has neither handled nor ignored,its <DFN>default action</DFN> takes place. Each kind of signal has its owndefault action, documented below (see section <A HREF="library_21.html#SEC335" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC335">Standard Signals</A>). For most kindsof signals, the default action is to terminate the process. For certainkinds of signals that represent "harmless" events, the default actionis to do nothing.<P>When a signal terminates a process, its parent process can determine thecause of termination by examining the termination status code reportedby the <CODE>wait</CODE> or <CODE>waitpid</CODE> functions. (This is discussed inmore detail in 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>.) The information it can getincludes the fact that termination was due to a signal, and the kind ofsignal involved. If a program you run from a shell is terminated by asignal, the shell typically prints some kind of error message.<P>The signals that normally represent program errors have a specialproperty: when one of these signals terminates the process, it alsowrites a <DFN>core dump file</DFN> which records the state of the process atthe time of termination. You can examine the core dump with a debuggerto investigate what caused the error.<P>If you raise a "program error" signal by explicit request, and thisterminates the process, it makes a core dump file just as if the signalhad been due directly to an error.<P><A NAME="IDX1461"></A><A NAME="IDX1462"></A><H2><A NAME="SEC335" HREF="library_toc.html#SEC335" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC335">Standard Signals</A></H2><A NAME="IDX1463"></A><A NAME="IDX1464"></A><P>This section lists the names for various standard kinds of signals anddescribes what kind of event they mean. Each signal name is a macrowhich stands for a positive integer--the <DFN>signal number</DFN> for thatkind of signal. Your programs should never make assumptions about thenumeric code for a particular kind of signal, but rather refer to themalways by the names defined here. This is because the number for agiven kind of signal can vary from system to system, but the meanings ofthe names are standardized and fairly uniform.<P>The signal names are defined in the header file <TT>`signal.h'</TT>.<P><A NAME="IDX1465"></A><U>Macro:</U> int <B>NSIG</B><P>The value of this symbolic constant is the total number of signalsdefined. Since the signal numbers are allocated consecutively,<CODE>NSIG</CODE> is also one greater than the largest defined signal number.<P><A NAME="IDX1466"></A><H3><A NAME="SEC336" HREF="library_toc.html#SEC336" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC336">Program Error Signals</A></H3><P>The following signals are generated when a serious program error isdetected by the operating system or the computer itself. In general,all of these signals are indications that your program is seriouslybroken in some way, and there's usually no way to continue thecomputation which encountered the error.<P>Some programs handle program error signals in order to tidy up beforeterminating; for example, programs that turn off echoing of terminalinput should handle program error signals in order to turn echoing backon. The handler should end by specifying the default action for thesignal that happened and then reraising it; this will cause the programto terminate with that signal, as if it had not had a handler.(See section <A HREF="library_21.html#SEC353" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC353">Handlers That Terminate the Process</A>.)<P>Termination is the sensible ultimate outcome from a program error inmost programs. However, programming systems such as Lisp that can loadcompiled user programs might need to keep executing even if a userprogram incurs an error. These programs have handlers which use<CODE>longjmp</CODE> to return control to the command level.<P>The default action for all of these signals is to cause the process toterminate. If you block or ignore these signals or establish handlersfor them that return normally, your program will probably break horriblywhen such signals happen, unless they are generated by <CODE>raise</CODE> or<CODE>kill</CODE> instead of a real error.<A NAME="IDX1467"></A><P>When one of these program error signals terminates a process, it alsowrites a <DFN>core dump file</DFN> which records the state of the process atthe time of termination. The core dump file is named <TT>`core'</TT> and iswritten in whichever directory is current in the process at the time.(On the GNU system, you can specify the file name for core dumps withthe environment variable <CODE>COREFILE</CODE>.) The purpose of core dumpfiles is so that you can examine them with a debugger to investigatewhat caused the error.<P><A NAME="IDX1468"></A><U>Macro:</U> int <B>SIGFPE</B><P>The <CODE>SIGFPE</CODE> signal reports a fatal arithmetic error. Although thename is derived from "floating-point exception", this signal actuallycovers all arithmetic errors, including division by zero and overflow.If a program stores integer data in a location which is then used in afloating-point operation, this often causes an "invalid operation"exception, because the processor cannot recognize the data as afloating-point number.<A NAME="IDX1470"></A><A NAME="IDX1469"></A><P>Actual floating-point exceptions are a complicated subject because thereare many types of exceptions with subtly different meanings, and the<CODE>SIGFPE</CODE> signal doesn't distinguish between them. The <CITE>IEEEStandard for Binary Floating-Point Arithmetic (ANSI/IEEE Std 754-1985)</CITE>defines various floating-point exceptions and requires conformingcomputer systems to report their occurrences. However, this standarddoes not specify how the exceptions are reported, or what kinds ofhandling and control the operating system can offer to the programmer.<P>BSD systems provide the <CODE>SIGFPE</CODE> handler with an extra argumentthat distinguishes various causes of the exception. In order to accessthis argument, you must define the handler to accept two arguments,which means you must cast it to a one-argument function type in order toestablish the handler. The GNU library does provide this extraargument, but the value is meaningful only on operating systems thatprovide the information (BSD systems and GNU systems).<P><DL COMPACT><A NAME="IDX1471"></A><DT><CODE>FPE_INTOVF_TRAP</CODE><DD>Integer overflow (impossible in a C program unless you enable overflowtrapping in a hardware-specific fashion).<A NAME="IDX1472"></A><DT><CODE>FPE_INTDIV_TRAP</CODE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -