I'm learning C and system programming. I need to create three signal handlers and once they are created, the program should send the signals to itself in the following order: SIGUSR1, SIGUSR2, and SIGINT. Below is my code. It is not working (for example, when I compile in the terminal - I type ./prog1 and click control-C, the program should print "Caught SIGINT, Exiting" and exit. I did not get any errors, but it just returns to the next line with nothing. Can someone take a look and point me to the right direction? Thank you so much for your help!
#include <signal.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
static void sigHandler_sigusr1(int sig)
{
//sig contains the signal number that was received
printf("Caught SIGUSR1\n", getpid());
kill(getpid(), SIGUSR1);
}
static void sigHandler_sigusr2(int sig)
{
//sig contains the signal number that was received
printf("Caught SIGSR2\n", getpid());
kill(getpid(), SIGUSR2);
}
static void sigHandler_sigint(int sig)
{
//sig contains the signal number that was received
printf("Caught SIGINT, Exiting\n", getpid());
kill(getpid(), SIGINT);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
if (signal(SIGUSR1, sigHandler_sigusr1) == SIG_ERR)
printf("Unable to create handler for SIGUSR1\n");
if (signal(SIGUSR2, sigHandler_sigusr2) == SIG_ERR)
printf("Unable to create handler for SIGUSR2\n");
if (signal(SIGINT, sigHandler_sigint) == SIG_ERR)
printf("Unable to create handler for SIGUSR1\n");
return 0;
}