我正在学习 C 和系统编程。我需要创建三个信号处理程序,一旦创建它们,程序应按以下顺序将信号发送给自身:SIGUSR1、SIGUSR2 和 SIGINT。下面是我的代码。它不起作用(例如,当我在终端中编译时 - 我键入 ./prog1 并单击 control-C,程序应打印“Caught SIGINT,Exiting”并退出。我没有收到任何错误,但它只是返回到下一行什么都没有。有人可以看看并指出正确的方向吗?非常感谢您的帮助!
#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;
}