【问题标题】:Why i can't catching SIGINT signal?为什么我无法捕捉到 SIGINT 信号?
【发布时间】:2024-01-08 17:09:01
【问题描述】:

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

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 不为我的应用程序传递,但调试器在“侦听”函数中停止但没有移动到信号处理函数,为什么会发生这种情况以及我做错了什么?

【问题讨论】:

    标签: linux signals


    【解决方案1】:

    这很正常。 gdb 捕捉到信号。来自手册:

    通常,gdb 设置为让非错误信号(如 SIGALRM) 默默地传递给你的程序(以免干扰他们的 在程序运行中的作用)但要停止您的程序 每当发生错误信号时立即。你可以改变这些 使用句柄命令进行设置。

    要更改行为,请使用:

    handle SIGINT nostop pass
    

    句柄信号[关键字...] 改变 gdb 处理信号信号的方式。信号可以是信号的编号或其名称(开头有或没有“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. 
    

    【讨论】: