c - Custom SIGINT signal handler - Program still terminates even after signal is caught -
i playing signal.h , unistd.h libraries, , having issues. in code below, when send sigint signal running program calling ctrl-c, signal caught. however, when pressing ctrl-c again, program terminates. understand it, print statement "received signal 2" should printed every time press ctrl-c.
is understanding of signal incorrect, or there bug in code?
thanks input!
#include "handle_signals.h" void sig_handler(int signum) { printf("\nreceived signal %d\n", signum); } int main() { signal(sigint, sig_handler); while(1) { sleep(1); } return 0; } terminal output:
xxx@ubuntu:~/dropbox/xxx/handle_signals$ ./handle_signals ^c received signal 2 ^c xxx@ubuntu:~/dropbox/xxx/handle_signals$ edit: here header i've included
#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> void sig_handler(int signum); thanks responses. reading through them now!
don't use signal, use sigaction:
the behavior of signal() varies across unix versions, , has varied historically across different versions of linux. avoid use: use sigaction(2) instead.
http://man7.org/linux/man-pages/man2/signal.2.html
in original unix systems, when handler established using signal() invoked delivery of signal, disposition of signal reset sig_dfl, , system did not block delivery of further instances of signal.
linux implements same semantics: handler reset when signal delivered.
Comments
Post a Comment