#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
RETURN VALUE:
signal() returns the previous value of the signal handler, or SIG_ERR on error. In the event of an error, errno is set to indicate the cause.
返回 指向 前一个此信号的处理(回调)函数 的指针,或者返回SIG_ERR。
例:
#include <iostream>
#include <stdio.h>
#include <signal.h>
using namespace std;
void fun1(int){
cout<<"fun1"<<endl;
}
void fun2(int){
cout<<"fun2"<<endl;
}
int main()
{
void (*res)(int);
if( (res=signal(SIGINT,fun1)) == SIG_ERR ){
perror("error!");
return -1;
}
raise(SIGINT);// == ctrl+c
if( (res=signal(SIGINT,fun2)) == SIG_ERR ){
perror("error!");
return -1;
}
raise(SIGINT);// == ctrl+c
res(SIGINT);//signal返回 指向前一个处理SIGINT的函数 的指针,也就是指向fun1
return 0;
}
结果:
图片.png