I met one issue of signal handling under Linux, my target is to let process ignore SIGTERM signal. But sometimes, process still exited, the probability of this issue will be 1/60.
Fake code of my application:
static int g_count_sig_old = 0;
static volatile int g_count_sig = 0;
void _signalhandler(){
g_count_sig ++;
printf(...); // Maybe not safe, just for debugging
myTrace(...); // Write log to file1 is not signal safe, but just for debugging.
}
main(){
sigaction(...) // Register signal handler for SIGTERM
while(1){
sleep(1000); // wait one second
myNewTrace(...); // Output value of g_count_sig to file2
if( g_count_sig != g_count_sig_old ) {
g_count_sig_old = g_count_sig;
printf(...); // output value of g_count_sig
myNewTrace(...); // Output value of g_count_sig to file2
}
}
}
I suppose this application will not quit when receiving signal SIGTERM, but actual testing result didn't match my design. Some times, the process still exit after receiving signal SIGTERM. And I confirm the process received SIGTERM signal when issue occurred, I can observe console output and trace file.
So I feel puzzled, why does this application exit even if ignoring SIGTERM? I am not sure how to position the cause of this issue, or it is reasonable symptom under Linux.
Hope to get your help. Thanks!