返回值
如果成功,此函数将返回零,否则返回非零。
示例
以下示例显示signal函数的用法-
#include <signal.h>
#include <stdio.h>
void signal_catchfunc(int);
int main () {
int ret;
ret = signal(SIGINT, signal_catchfunc);
if( ret == SIG_ERR) {
printf("Error: unable to set signal handler.\n");
exit(0);
}
printf("Going to raise a signal\n");
ret = raise(SIGINT);
if( ret !=0 ) {
printf("Error: unable to raise SIGINT signal.\n");
exit(0);
}
printf("Exiting...\n");
return(0);
}
void signal_catchfunc(int signal) {
printf("!! signal caught !!\n");
}
尝试一下
让我们编译并运行以上程序以产生以下结果-
Going to raise a signal
!! signal caught !!
Exiting...