1

我正在学习 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;
}
4

2 回答 2

1

您的主程序没有等待接受信号。发生的情况是主程序启动并检查条件并立即返回 0;

为了理解

附在while(1)主程序中,现在按 ctr+C。您将获得预期的输出。

#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");
    kill(getpid(), SIGUSR1);
}
static void sigHandler_sigusr2(int sig)
{
    //sig contains the signal number that was received
    printf("Caught SIGSR2\n");
    kill(getpid(), SIGUSR2);
}
static void sigHandler_sigint(int sig)
{
    //sig contains the signal number that was received
    printf("Caught SIGINT, Existing\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");
        while(1){
         /*some work*/
        }

    return 0;
}

不要在信号处理程序中使用 printf。有关详细信息,请参阅此帖子

于 2013-07-17T06:10:28.913 回答
1

修改后main()的将起作用。

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");

   while(1)
   {
         sleep(1);
   }

    return 0;
}

在您的代码中,注册信号处理程序后,执行完成。while(1)添加以保持您的代码运行,因此CTRL + C将调用何时按下处理程序。

还添加#include <unistd.h>getpid()功能。%d并添加

printf("Caught SIGSR2\n", getpid());
于 2013-07-17T06:11:10.367 回答