处理多个信号

发布于 2024-12-19 12:20:09 字数 1154 浏览 1 评论 0原文

我有一个关于处理信号的问题。假设如果我们收到 SIGINT 信号,我们应该打印“Received Signal”。如果在十秒内处理程序收到另一个信号,它应该打印“关闭”,然后以状态 1 退出。

我的代码如下:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void handler(int);
void secondhandler(int);
void alrmhandler(int);

void alrmhandler(int alrmsig) {
    alarm(0);
}

void secondhandler(int sig) {
    /* after recieving second signal prints shutting down and exit */
    printf("Shutting Down\n");
    exit(1);
}

void handler(int sig) {
    /* receive first SIGINT signal */
    printf("Received Signal\n");
    /* handle for the alarm function */
    signal(SIGALRM, alrmhandler);
    /* start 10s alarm */
    alarm(10);
    /* catch second SIGINT signal within 10s*/
    signal(SIGINT, secondhandler);
}

int main(void) {
    signal(SIGINT, handler);
    printf("Hello World!\n");
    for (;;) {
        /* infinite loop */
    }

    return 0;
}

我尝试使用 Dev-C++ 编译它,但失败并显示 SIGALRM 未声明(在此函数中首次使用)

无论如何,我想知道的是这段代码是否正确。我实际上对 alrmhandler() 有点不确定。我应该忽略 SIGALRM 吗?

I have a question about handling a signal. Assume that if we receive SIGINT signal we should print "Received Signal". If within ten seconds the handler receives another signal it should print "Shutting Down" then exit with status 1.

I made my code like this:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void handler(int);
void secondhandler(int);
void alrmhandler(int);

void alrmhandler(int alrmsig) {
    alarm(0);
}

void secondhandler(int sig) {
    /* after recieving second signal prints shutting down and exit */
    printf("Shutting Down\n");
    exit(1);
}

void handler(int sig) {
    /* receive first SIGINT signal */
    printf("Received Signal\n");
    /* handle for the alarm function */
    signal(SIGALRM, alrmhandler);
    /* start 10s alarm */
    alarm(10);
    /* catch second SIGINT signal within 10s*/
    signal(SIGINT, secondhandler);
}

int main(void) {
    signal(SIGINT, handler);
    printf("Hello World!\n");
    for (;;) {
        /* infinite loop */
    }

    return 0;
}

I tried to compile it with Dev-C++, but it failed with SIGALRM was undeclared (first use in this function).

Anyway, what I want to know is if this code is right. I'm actually kinda not sure with the alrmhandler(). Should I ignore the SIGALRM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贵在坚持 2024-12-26 12:20:09

如果您使用的是 Windows 平台,则您能够发送的唯一信号是:SIGABRT、SIGFPE、SIGILL、SIGINT、SIGSEGV 或 SIGTERM。

If you are on a Windows platform, the only signals you will be able to send are : SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, or SIGTERM.

网名女生简单气质 2024-12-26 12:20:09

你写:

我想知道这段代码是否正确。

不完全正确。 printf() 不是 异步信号安全,因此不应从信号处理程序中调用,除非您非常确定这样做是安全的。在您提供的代码中执行此操作安全。

一般来说,alarm() 技术容易出现竞争。您的十秒警报可能会在 secondaryhandler() 函数执行过程中到期。为了防止这种情况,您可以屏蔽信号,以使用更复杂的信号处理来补偿。函数。

有更优雅/灵活的方法来实现您想要的超时,但这可能是一个更适合 codereview.stackexchange.com 的问题。

You write:

what I want to know is if this code is right.

Not entirely. printf() is not async-signal-safe, and so should not be called from within a signal handler unless you are very sure it is safe to do so. It is not safe to do so within the code you provide.

The alarm() technique is, generally, race-prone. Your ten second alarm might expire in the middle of your secondhandler() function. To guard against this, you might mask out signals to compensate with a more sophisticated signal manipulation function.

There are more elegant/flexible ways of implementing the timeout you desire, but that's perhaps a question better suited for codereview.stackexchange.com.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文