i'm writing program sigint signal handled first time sent, set default after that. so, example, have this:
static volatile int stop_terminating = 1; void handler(int dummy) { stop_terminating = 0; } int main(){ signal(sigint, handler); char input[256]; while(1){ if(stop_terminating == 0){ // reset action of signal default signal(sigint, sig_dfl); printf("message sent.\n"); // increment counter doesn't enter condition again stop_terminating++; } printf("user input:\n"); fgets(input, sizeof(input), stdin); // in stage, wanna press ctrl+c , print message, stopping fgets // happens is: press ctrl+c, signal catched, fgets // still asking input, , after send something, message printed // because looped through while(1) again. } }
how can stop fgets asking input , print message , ask again input?
the linux manual page signals says that
interruption of system calls , library functions signal handlers
if signal handler invoked while system call or library function call blocked, either:
the call automatically restarted after signal handler returns; or
the call fails error
eintr
.which of these 2 behaviors occurs depends on interface , whether or not signal handler established using
sa_restart
flag (seesigaction
(2)).
you may use either siginterrupt()
function signal()
, or use sigaction()
instead of signal()
registering signal handler, in order disable restarting read()
system call after signal.
note fgets()
c library might call read()
multiple times until newline character found, therefore may need switch using lower-level functions instead of stdio.h
apis.
Comments
Post a Comment