I am a beginner in C and system programming. I wrote a program and it should display the following: Caught SIGUSR1 Caught SIGUSR2 Caught SIGINT
However, when I do "./test.c", the only thing I see is "Caught SIGINT" when I type Ctrl-C. How can I fix my code so my program displays the messages above? Sorry if my question is dumb. Your help is greatly appreciated. Thanks for reading.
EDITED:
#include <signal.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
static void sigHandler_sigusr1(int sig)
{
//sig contains the signal number that was received
printf("Caught SIGUSR1, %d\n", getpid());
//kill(getpid(), SIGUSR1);
}
static void sigHandler_sigusr2(int sig)
{
//sig contains the signal number that was received
printf("Caught SIGSR2, %d\n", getpid());
//kill(getpid(), SIGUSR2);
}
static void sigHandler_sigint(int sig)
{
//sig contains the signal number that was received
printf("Caught SIGINT, Existing, %d\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 SIGINT\n");
kill(getpid(), SIGUSR1);
kill(getpid(), SIGUSR2);
kill(getpid(), SIGINT);
while (1)
{
sleep(1);
}
return 0;
}