Within one course, I wrote an application that, when sending a SIGTERM signal to it, outputs to the output the number of sent SIGUSR1 and SIGUSR2 signals. I wrote a program that sends the signals from the main program to the pid

#include <stdio.h> #include <signal.h> #include <stdlib.h> int signal_cntr1 = 0; int signal_cntr2 = 0; void sig_handler1(int unused){ signal_cntr1++; } void sig_handler2(int unused){ signal_cntr2++; } void sig_handler3(int unused){ printf("%d %d\n",signal_cntr1, signal_cntr2); exit(0); } int main(){ signal(SIGUSR1, sig_handler1); signal(SIGUSR2, sig_handler2); signal(SIGTERM, sig_handler3); while(1); return 0; } 

test program code

 #include <stdio.h> #include <sys/types.h> #include <signal.h> #include <stdlib.h> int main(int v, char **c){ int i; if(v != 2){ exit(EXIT_FAILURE); } for(i = 0; i < 253; i++){ kill(atoi(c[1]), SIGUSR1); usleep(5000); } for(i = 0; i < 124; i++){ kill(atoi(c[1]), SIGUSR2); usleep(5000); } kill(atoi(c[1]), SIGTERM); usleep(5000); return 0; } 

but the work is such

 brainiac@brainiac-Latitude-7480:~/workspace/linux_fund$ ./solution & [3] 4542 [2] Done ./solution brainiac@brainiac-Latitude-7480:~/workspace/linux_fund$ ./a.out 4542 78 40 [3]- Done ./solution brainiac@brainiac-Latitude-7480:~/workspace/linux_fund$ 

although in the test application, 253 sigusr1 and 124 sigusr2 signals are sent, and the application shows 78 and 40, respectively. Where am I wrong?

    2 answers 2

    Signals are NOT buffered in a regular luna. This means that if the process did not manage to process one signal, but arrived with a second one of the same type, then it will be lost. It seems that this is what happens to you.

    The signal is sent with a period of only 5 ms, and taking into account the fact that you have the processor loaded 100% by the while (1) operator ; I fully admit the loss of signals.

    To prevent this from happening, you can use the RT Linux extensions.

      Signals of the same type in Linux do not queue up. If the process is processing SIGUSR1 and another SIGUSR1 arrives, it is simply ignored. But if SIGUSR1 processed and SIGUSR2 arrives, then both will be processed.

      I suppose if you raise the delay between sending signals, then the problem on a particular system at a particular point in time may become less noticeable or disappear (if each signal has time to process before the next signal). And if you omit the delay or remove completely - on the contrary, it will become more pronounced.