C Library Function - raise()
Description
C Library Function int raise(int sig) causes a signal to be generated sig.sig The argument is compatible with the SIG macros.
raise is a function in the C standard library used to send a signal to the current process. It is defined in header file, and allows a program to send a signal to itself to trigger the corresponding signal handler.
Declaration
The following is the declaration of the raise() function.
int raise(int sig)
Parameters
- sig -- The signal code to send. The following are some important standard signal constants:
| macro | Signal |
|---|---|
| SIGABRT | (Signal Abort) Abnormal program termination. |
| SIGFPE | (Signal Floating-Point Exception) Arithmetic error, such as division by zero or overflow (not necessarily a floating-point operation). |
| SIGILL | (Signal Illegal Instruction) Illegal function image, such as an illegal instruction, usually caused by some variation in the code or an attempt to execute data. |
| SIGINT | (Signal Interrupt) Interrupt signal, such as ctrl-C, typically generated by the user. |
| SIGSEGV | (Signal Segmentation Violation) Illegal memory access, such as accessing a nonexistent memory location. |
| SIGTERM | (Signal Terminate) A termination request signal sent to the program. |
Return Value
The function returns zero on success, or nonzero otherwise.
Example
The following example demonstrates the use of the raise() function.
Example
#include
#include
#include
void
signal_catchfunc
(
int
)
;
int
main
(
)
{
int
ret
;
signal
(
SIGINT
,
signal_catchfunc
)
;
printf
(
"start generating a signal\n"
)
;
ret
=
raise
(
SIGINT
)
;
if
(
ret
!=
0
)
{
printf
(
"error: cannot generate SIGINT signal\n"
)
;
exit
(
0
)
;
}
printf
(
"exit....\n"
)
;
return
0
;
}
void
signal_catchfunc
(
int
signal
)
{
printf
(
"signal caught\n"
)
;
}
Let's compile and run the above program, which will produce the following result:
开始生成一个信号
!! 信号捕获 !!
退出...
C Standard Library -