简体   繁体   English

SIGINT(ctrl + c)不会中断接受呼叫

[英]SIGINT(ctrl+c) doesn't interrupt accept call

In infinite loop, i am accepting new connections, but i need choice to stop that server, so i want use ctrl+c (SIGINT). 在无限循环中,我接受新的连接,但是我需要选择停止该服务器,因此我想使用ctrl + c(SIGINT)。 When i press it, it calls my signal handler, but it doesn't interrupt accept call, so the check if interrupt is true isn't evaluated (when no client's connecting, so accept is blocking for long while). 当我按下它时,它会调用我的信号处理程序,但不会中断接受调用,因此不会评估是否为真的中断(当没有客户端连接时,接受会长时间阻塞)。

sig_atomic_t interrupt;
.
.
.
signal(SIGINT, sigintHandler);
.
.
.
while(!interrupt) {
  server.accept();
}
.
.
.
void sigintHandler(int param) {
    interrupt = 1;
}

Your OS is using SA_RESTART semantics by default when using signal() . 使用signal()时,默认情况下,您的操作系统使用SA_RESTART语义。 You should be able to confirm this in the man page. 您应该能够在手册页中对此进行确认。 Since you don't want this to happen, use sigaction() to set the handler and omit the SA_RESTART flag. 由于您不希望发生这种情况,因此请使用sigaction()设置处理程序,并忽略SA_RESTART标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM