gpt4 book ai didi

linux - 为什么我无法捕获 SIGINT 信号?

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:50 33 4
gpt4 key购买 nike

你好,我有下一个代码:

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

最佳答案

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

Normally, gdb is set up to let the non-erroneous signals like SIGALRM be silently passed to your program (so as not to interfere with their role in the program's functioning) but to stop your program immediately whenever an error signal happens. You can change these settings with the handle command.

要改变行为,使用:

handle SIGINT nostop pass

handle signal [keywords...] Change the way gdb handles signal signal. signal can be the number of a signal or its name (with or without the ‘SIG’ at the beginning); a list of signal numbers of the form ‘low-high’; or the word ‘all’, meaning all the known signals. Optional arguments keywords, described below, say what change to make.

The keywords allowed by the handle command can be abbreviated. Their full names are:

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.

关于linux - 为什么我无法捕获 SIGINT 信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16933577/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com