c++ - Why doesn't SIGINT handler work in my code? -


i need command line application wait ctrl-c in main thread , execute deinitialization code , exit.

i tested scenario following test program:

#include <unistd.h> #include <signal.h> #include <cstdlib> #include <cstdio> #include <cassert>  using namespace std;  void control_break_handler(int) { printf("\ncontrol_break_handler\n"); }  int main() {     printf("%s", "before signal\n");      int result = 0;      struct sigaction sigact = {};     result = sigemptyset(&sigact.sa_mask);     assert(result == 0);     sigact.sa_handler = &control_break_handler;     sigact.sa_flags = 0;     result = sigaction(sigint, &sigact, null);     assert(result == 0);      sigset_t set;     result = sigemptyset(&set);     assert(result == 0);     result = sigaddset(&set, sigint);     assert(result == 0);     int sig;     result = sigwait(&set, &sig);     assert(result == 0);     printf("\nsigwait returned %d, sig = %d\n", result, sig);      sleep(10);      return 0; } 

but prints

sigwait returned 0, sig = 2 

after first ctrl-c , "control_break_handler" called after second ctrl-c. how execute deinitialization code on first ctrl-c? use ubuntu 13.04 x64.

edit: ok, seems figured out. thrown away sigaction , added call sigprocmask before sigwait. seems work fine.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -