c - I am generating signal and facing the strange behaviour -
i have started getting hands of signals in linux here strange behavior happening in code. have started , searched have not found anything, sorry if question lame, here code-
void handler(int sig ){ printf("inside handler\n"); } int main(int argc, char * argv[], char * envp[]){ if( signal(sigint, handler) ==sig_err ) exit(exit_failure); for(size_t = 0; ; i++){ printf("%d\n", i); sleep(2); } }
i know printf
, signal
calls not idea use have not studied sigaction
till now. according book , others tutorial if press ctrl+c
has call handler
every time here strange thing: when pressing ctrl+c
once, it's calling handler
next time it's terminating program. why strange thing happening?
the manual page signal
syscall says:
if signal signum delivered process, 1 of following happens:
if disposition set sig_ign, signal ignored.
if disposition set sig_dfl, default action associated signal (see signal(7)) occurs.
if disposition set function, then first either disposition reset sig_dfl, or signal blocked (see portability below), , handler called argument signum. if invocation of handler caused signal blocked, signal unblocked upon return handler.
since default behavior of sigint terminate once signal reset default behavior, subsequent behavior different.
Comments
Post a Comment