Can someone tell me, as to why program1’s signal handler is not working?
Program1: signal1.c
#include <stdio.h>
#include <signal.h>
void handler(int sig)
{
printf("Caught signal: %d",sig);
signal(sig,handler);
}
int main()
{
struct sigaction sa;
sa.sa_handler=handler;
sa.sa_sigaction=NULL;
sigaction(SIGRTMIN,&sa,NULL);
kill(0,SIGRTMIN);
}
Actual Output:
# ./a.out
Real-time signal 0
Expected Output:
Caught signal: 34
Please help me to resolve Program1
However, the program2 works, if i use a simple signal handler like as usual:
enter code here
#include <stdio.h>
#include <signal.h>
void handler(int sig)
{
printf("Caught signal: %d\n",sig);
signal(sig,handler);
}
int main()
{
signal(SIGRTMIN, handler);
kill(0,SIGRTMIN);
}
Output:
Caught signal: 34
This is because
sa_handlerandsa_sigactionare members of the same union:So that when you set
sa_sigactionit overwritessa_handlervalue.