为什么我不能捕捉SIGINT信号?

时间:2013-06-05 07:05:40

标签: linux signals

美好的一天,我有下一个代码:

server s;
namespace signals_handler
{
    //sig_atomic_t is_stop=0;
    void signal_handler(int sig)
    {
        if(sig==SIGHUP)
        {
            printf("recived :SIGHUP\n");
            s.restart();
        }
        else if(sig==SIGINT)
        {
            printf("recived :SIGINT\n");
            //is_stop = 1;
            s.interupt();
        }
    }
}
int main(int argc, char* argv[])
{
    signal(SIGHUP, signals_handler::signal_handler);
    signal(SIGINT, signals_handler::signal_handler);
    s.start();
    s.listen();
    return 0;
}

当我开始执行此代码时,我可以捕获SIGHUP,SIGINT不能为我的应用程序提供,但调试器停止在“listen”函数中,但不会移动到signalhandler函数,为什么会发生这种情况,我做错了什么?

1 个答案:

答案 0 :(得分:1)

这是正常的。 gdb抓住了信号。从手册:

  

通常,gdb设置为允许像SIGALRM这样的非错误信号   默默地传递给你的程序(以免干扰他们的   在计划中起作用)但是要停止你的计划   发生错误信号时立即发生。你可以改变这些   使用handle命令设置。

要更改行为,请使用:

handle SIGINT nostop pass
  

处理信号[关键字...]       改变gdb处理信号信号的方式。 signal可以是信号的编号或其名称(开头有或没有'SIG');   “低 - 高”形式的信号编号列表;或者“全部”这个词,   意思是所有已知的信号。描述了可选参数关键字   在下面,说出要做出的改变。

     

handle命令允许的关键字可以缩写。其   全名是:

nostop
    gdb should not stop your program when this signal happens. It may still print a message telling you that the signal has come in.
stop
    gdb should stop your program when this signal happens. This implies the print keyword as well.
print
    gdb should print a message when this signal happens.
noprint
    gdb should not mention the occurrence of the signal at all. This implies the nostop keyword as well.
pass
noignore
    gdb should allow your program to see this signal; your program can handle the signal, or else it may terminate if the signal is fatal and not handled. pass and noignore are synonyms.
nopass
ignore
    gdb should not allow your program to see this signal. nopass and ignore are synonyms.