简体   繁体   中英

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). 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() . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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