If we use sigaction to define a signal handler, then why don’t we need to reset the handler? If we do the use signal(sig_no,handler_func) then we must reset it. Why is this?
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
void func(int sig)
{
printf("caught signal:%d\n",sig);
// Not needed to reset handler. Why?
}
int main()
{
struct sigaction sa;
sa.sa_handler=(void*)func;
sigaction(SIGRTMIN,&sa,NULL);
kill(0,SIGRTMIN);
kill(0,SIGRTMIN);
kill(0,SIGRTMIN);
}
Output:
[root@dhcppc0 signals]# ./a.out
caught signal:34
caught signal:34
caught signal:34 (3 times signal caught by same handler without resetting handler)
Unless you specify
SA_RESETHANDin the flags, the disposition isn’t changed (and therefore doesn’t need to be reset)If you were to set
sa.sa_flags = SA_RESETHANDYou’d need to reset it because the disposition would be reset toSIG_DFL(which is what happens withsignal())Basically, the answer to your question is “Because that’s how sigaction works. Its behavior is different than signal”.