I am trying to generate and catch signal SIGUSR1.
I am creating a child process using fork system call, using sigaction to associate the function handler with the signal and use kill to invoke the signal.
However either signal is not getting invoked or handler fuction is not being called.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
void childProcess(void);
void parentProcess(pid_t);
void segHandler(int);
int main(void)
{
pid_t pid;
pid = fork();
switch(pid)
{
case -1 :
printf("Could not fork\n");
exit(EXIT_FAILURE);
case 0 :
printf("Entering Child\n");
childProcess();
break;
default :
printf("Entering Parent\n");
printf("Immediate PID : %d\n", pid);
parentProcess(pid);
}
return EXIT_SUCCESS;
}
void childProcess(void)
{
int op;
struct sigaction act;
act.sa_handler = segHandler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
op = sigaction(SIGUSR1, &act, 0);
if(op == -1)
{
printf("Unable to sigaction\n");
exit(EXIT_FAILURE);
}
else
printf("Sigaction successfully set\n");
}
void parentProcess(pid_t pid)
{
printf("Parent Process Sleeping for 5 Seconds\n");
sleep(5);
printf("Parent Process Woke up after 5 Seconds\n");
printf("Calling kill on PID : %d\n", pid);
kill(pid, SIGUSR1);
}
void segHandler(int sig)
{
printf("Generated signal has been caugth\n");
printf("Recideved Signal : %s", strsignal(sig));
}