我是 C 和系统编程的初学者。我写了一个程序,它应该显示以下内容: Caught SIGUSR1 Caught SIGUSR2 Caught SIGINT
但是,当我执行“./test.c”时,当我键入 Ctrl-C 时,我看到的唯一内容是“Caught SIGINT”。如何修复我的代码,以便我的程序显示上面的消息?对不起,如果我的问题很愚蠢。非常感谢您的帮助。谢谢阅读。
编辑:
#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;
}