I was trying to write a program which involves sending signal to a process for notifying it to pause for some time and start work again after another signal is received. I wrote a signal handler like this:
void sig_handler(int alrm)
{
if(sig_rcv==0)
{
printf("Signal received..sig_rcv value: %d\n",sig_rcv);
sig_rcv = (sig_rcv+1)%2;
printf("After increment sig_rcv value: %d\n",sig_rcv);
signal(SIGUSR1,sig_handler);
if(pause()<0)
{
printf("Signal received again..\n");
}
}
else
{
sig_rcv = (sig_rcv+1)%2;
printf("Signal received..sig_rcv value: %d\n",sig_rcv);
signal(SIGUSR1,sig_handler);
}
printf("Exiting..\n");
}
Here I am maintaining a global variable sig_rcv which is initially 0 and if a signal is received when it is zero, it will go to the if condition and pause for another signal.On the other hand, if it gets a signal while sig_rcv is 1, it will just change the value of that variable. My purpose of writing the signal handler in this way was to use the same signal for two different purposes. I used this system call:
signal(SIGUSR1,sig_handler);
Now when I was sending SIGUSR1 to the process it is executing upto the pause()
statement. But after sending SIGUSR1 for the second time, it shows no effect.It just freezes at the pause statement. This problem was resolved after I used another signal for removing the pause state, something like:
void sig_handler2(int a)
{
sig_rcv = (sig_rcv+1)%2;
printf("Restarting reading...\n");
}
and
signal(SIGUSR2,sig_handler2);
It worked perfectly in this case. So, my question is, why is this phenomenon taking place? Can't we wait for a signal while executing the signal handler written for the same signal? If it is so, what is the reason? And is there any way of achieving the same result without using SIGUSR2?