使用C中的sigaction启用信号处理程序 [英] Enable a signal handler using sigaction in C
问题描述
结构sigaction的PSA;
我已经启用的主要功能我的信号处理程序,如下所示:
memset的(安培; PSA,0,sizeof的(PSA));
psa.sa_handler = pSigHandler;
的sigaction(SIGALRM,&安培; PSA,NULL);
的sigaction(SIGVTALRM,&安培; PSA,NULL);
的sigaction(SIGPROF,&安培; PSA,NULL);
我的信号处理程序是这样的:
静态无效pSigHandler(INT预兆报){
的printf(Pareint正负号数:%d,SIGNO); //调试
开关(预兆报){
案例SIGALRM:
的printf(P SIGALRM处理程序); //调试
打破;
案例SIGVTALRM:
的printf(P SIGVTALRM处理程序); //调试
打破;
案例SIGPROF:
的printf(P SIGPROF处理程序); //调试
打破;
默认值:/ *应该不会产生这种情况下,* /
打破;
}
返回;
}
现在我的问题可能很明显的一些人,为什么我没有看到印有调试线,当我运行呢?事实上,什么也没有打印出来。非常感谢你帮助我理解这一点。我运行在Linux上,使用了Eclipse程序。
的#include<&stdio.h中GT;
#包括LT&;&signal.h中GT;静态无效pSigHandler(INT预兆报){
开关(预兆报){
案例SIGTSTP:
的printf(TSTP);
fflush(标准输出);
打破;
}
}INT主要(无效)
{
结构的sigaction PSA;
psa.sa_handler = pSigHandler;
的sigaction(SIGTSTP,&安培; PSA,NULL);
为(;;){}
返回0;
}
由于需要fflush(标准输出)
尝试用C-Z
我甚至不知道这是否是安全的信号处理程序使用标准输入输出,虽然。
更新:<一href=\"http://bytes.com/topic/c/answers/440109-signal-handler-sigsegv\">http://bytes.com/topic/c/answers/440109-signal-handler-sigsegv
据该链接,你不应该这样做。
struct sigaction psa;
I have enabled my signal handler in the main function as shown below:
memset (&psa, 0, sizeof (psa));
psa.sa_handler = pSigHandler;
sigaction (SIGALRM, &psa, NULL);
sigaction(SIGVTALRM, &psa, NULL);
sigaction(SIGPROF, &psa, NULL);
My signal handler is like this:
static void pSigHandler(int signo){
printf("Pareint signum: %d", signo);// debug
switch (signo) {
case SIGALRM:
printf("P SIGALRM handler");//debug
break;
case SIGVTALRM:
printf("P SIGVTALRM handler");//debug
break;
case SIGPROF:
printf("P SIGPROF handler");//debug
break;
default: /*Should never get this case*/
break;
}
return;
}
Now my question may be obvious to some people, why didn't I see the printed debug lines when I run this? In fact, nothing was printed. Thank you very much for helping me to understand this. I'm running it on Linux, used Eclipse to program.
#include <stdio.h>
#include <signal.h>
static void pSigHandler(int signo){
switch (signo) {
case SIGTSTP:
printf("TSTP");
fflush(stdout);
break;
}
}
int main(void)
{
struct sigaction psa;
psa.sa_handler = pSigHandler;
sigaction(SIGTSTP, &psa, NULL);
for(;;) {}
return 0;
}
Because you need to fflush(stdout)
try with C-z
I'm not even sure if it's safe to use stdio in a signal handler though.
Update: http://bytes.com/topic/c/answers/440109-signal-handler-sigsegv
According to that link, you should not do this.
这篇关于使用C中的sigaction启用信号处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!