函数名: signal表头文件#include<signal.h>
功 能:设置某一信号的对应动作
定义函数:void (*signal(int signum,void(* handler)(int)))(int);
函数说明:signal()会依参数signum 指定的信号编号来设置该信号的处理函数。当指定的信号到达时就会跳转到参数handler指定的函数执行。
如果参数handler不是函数指针,则必须是下列两个常数之一:
SIG_IGN 忽略参数signum指定的信号。
SIG_DFL 将参数signum 指定的信号重设为核心预设的信号处理方
式。
详细信号,查看unix信号编号
返回值 返回先前的信号处理函数指针,如果有错误则返回SIG_ERR(-1)。
附加说明 在信号发生跳转到自定的handler处理函数执行后,系统会自动将此处理函数换回原来系统预设的处理方式,如果要改变此操作请改用sigaction()。
程序例:
/* This example installs a signal handler routine for SIGFPE,
catches an integer overflow condition, makes an adjustment
to AX register, and returns. This example program MAY cause
your computer to crash, and will produce runtime errors
depending on which memory model is used.
*/
#pragma inline
#include <stdio.h>
#include <signal.h>
void Catcher(int sig, int type, int *reglist)
{
printf("Caught it!
");
*(reglist + 8) = 3; /* make return AX = 3 */
}
int main(void)
{
signal(SIGFPE, Catcher);
asm mov ax,07FFFH /* AX = 32767 */
asm inc ax /* cause overflow */
asm into /* activate handler */
/* The handler set AX to 3 on return. If that hadn't happened,
there would have been another exception when the next 'into'
was executed after the 'dec' instruction. */
asm dec ax /* no overflow now */
asm into /* doesn't activate */
return 0;
}
注意点:
1、不要使用低级的或者STDIO.H的IO函数
2、不要使用对操作
3、不要进行系统调用
4、不是浮点信号的时候不要用longjmp