Http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.9.html.
Author: Eric Huss
Chinese translator: Liu jinhong poechant
Copyright Disclaimer: the original text in this article is copyrighted by Eric Huss, and the Chinese translation is copyrighted by poechant. Reprinted please indicate from "LIU Da's csdn blog": http://blog.csdn.net/poechant
9. Signal. h
The header file signal is used to process the signal generated when the program is running.
MACRO:
Sig_dfl
Sig_err
Sig_ign
SIGABRT
Sigfpe
Sigill
SIGINT
SIGSEGV
Sigterm
Function:
Signal ();
Raise ();
Variable:
Typedef sig_atomic_t
9.1.
Variables and definitions
TypeSig_atomic_tYesInt,SignalThe variable in the handler of the function.SIG _MacroSignalFunction is used to define the signal function.
Sig_dfl |
Handler by default. |
Sig_err |
Indicates the error signal. |
Sig_ign |
Ignore the signal. |
SIGMacros represent the signal numbers in the following situations.
SIGABRT |
Terminate an exception (generated by the abort function ). |
Sigfpe |
Floating Point error (caused by division-by-zero operation or unreasonable operation ). |
Sigill |
Illegal operation (command ). |
SIGINT |
Interactive signal (for example, Ctrl-C ). |
SIGSEGV |
Illegal storage access (segment error, memory error ). |
Sigterm |
Terminate the request. |
9.2. Signal
Statement:
Void (* signal (intSIG, Void (*Func) (INT );
This function manages how a signal is manipulated.SIGRepresentatives andSIGMacro-compatible signal numbers.FuncThe function is called when the signal is generated. If the function isSig_dflThe default handler is called. IfFuncYesSig_ignThe signal is ignored. IfFuncPoint to a function. When a signal is detected and the default handler is executed, the function is called. The function must carryIntThe parameter indicates the signal number. The function may beReturn,Abort,Exit, OrLongjmpTermination. When the function is terminated, the program continues to run from the center (unless it is a signal with undefined results)Sigfpe).
If the signal is successfully called, the pointer to the handler of the previous specific signal type is returned. If the signal call fails, returnSig_errAnd setErrnoSet a reasonable value.
9.3. Raise
Statement:
Int raise (intSIG);
GenerateSIGSignal.SIGParameters must matchSIGMacro compatibility.
If the call is successful, 0 is returned. Otherwise, a non-zero value is returned.
Instance:
# Include <signal. h>
# Include <stdio. h>
Void catch_function (INT );
Int main (void)
{
If (signal (SIGINT, catch_function) = sig_err)
{
Printf ("an error occured while setting a signal handler. \ n ");
Exit (0 );
}
Printf ("Raising the interactive attention signal. \ n ");
If (raise (SIGINT )! = 0)
{
Printf ("error raising the signal. \ n ");
Exit (0 );
}
Printf ("exiting. \ n ");
Return 0;
}
Void catch_function (INT signal)
{
Printf ("Interactive attention signal caught. \ n ");
}
Program running output result (assuming there is no error ):
Raising the interactive attention signal.
Interactive attention signal caught.
Exiting.
This series of translations is being updated continuously
(1) assert. HC standard Library Reference Guide series (2) ctype. HC standard Library Reference Guide series (3) errno. HC standard Library Reference Guide series (4) float. HC standard Library Reference Guide series (5) limits. HC standard Library Reference Guide series (6) locale. HC standard Library Reference Guide series (7) math. HC standard Library Reference Guide series (8) setjmp. HC standard Library Reference Guide series translations (9) signal. HC standard Library Reference Guide series translations (10) stdarg. HC standard Library Reference Guide series (11) stddef. HC standard Library Reference Guide series translations (12) stdio. H ()
Copyright Disclaimer: the original text in this article is copyrighted by Eric Huss, and the Chinese translation is copyrighted by poechant. Reprinted please indicate from "LIU Da's csdn blog": http://blog.csdn.net/poechant
-