I have this function (in a long program with semaphores, I'll attach any code if necessary):
void signalHandler(int signumber){
/* for SIGINT */
if(signumber == SIGINT){
printf(" Pressed - Stop the timer & start cleaning process...\n");
flag = 0; //relevant for the rest of the program
}
}
when I'm running the program and press CTRL+C
to stop it, instead of getting this output:
^C Pressed - Stop the timer & start cleaning process...
*****Timer finished / stopped - clean the queue*****
....
I get this output with double print:
^C Pressed - Stop the timer & start cleaning process...
Pressed - Stop the timer & start cleaning process...
*****Timer finished / stopped - clean the queue*****
....
This little thing very annoying, how can I fix it? Thanks.
In ISO C, a signal handler may only set a volatile atomic flag, or abort the program . In POSIX it may also call some signal-safe functions, you will see that printf
is not in the list.
So, basically, don't call printf from the signal handler.
Perhaps you could write
to stdout, if going for POSIX conformance without ISO conformance. Or for the latter, set a flag in the signal handler and check that flag periodically from the main loop.