在自己的处理程序中捕获信号

发布于 2024-08-24 05:35:45 字数 557 浏览 14 评论 0原文

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

void handler(int signo)
{
    printf("Into handler\n");
    while(1);
}
int main()
{
    struct sigaction act;
    act.sa_handler = handler;
    act.sa_flags = 0;
    sigemptyset(& act.sa_mask);
    sigaction(SIGINT, &act, NULL);
    while(1);
    return 0;
}

捕获一次 KeyboardInterrupt 后,当我再次按“Ctrl+C”时,SIGINT 未被处理...... 我打算每次按“Ctrl+C”时都应打印“Into handler”

我想在“SIGINT handler()”本身内部捕获 SIGINT..

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

void handler(int signo)
{
    printf("Into handler\n");
    while(1);
}
int main()
{
    struct sigaction act;
    act.sa_handler = handler;
    act.sa_flags = 0;
    sigemptyset(& act.sa_mask);
    sigaction(SIGINT, &act, NULL);
    while(1);
    return 0;
}

After catching the KeyboardInterrupt once, when I press "Ctrl+C" again, SIGINT is not handled...
I intend that "Into handler" should be printed each time I press "Ctrl+C".

I want to catch SIGINT inside the "SIGINT handler()" itself..

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

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

发布评论

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

评论(5

皇甫轩 2024-08-31 05:35:45

您需要在 sa_mask 上设置 SA_NODEFER 以捕获与当前正在处理的信号相同的信号:

SA_NODEFER:不阻止从其自己的信号处理程序中接收信号。 SA_NOMASK 是此标志的过时的非标准同义词。

You need to set SA_NODEFER on sa_mask to catch the same signal as the one you're currently handling:

SA_NODEFER: Do not prevent the signal from being received from within its own signal handler. SA_NOMASK is an obsolete, non-standard synonym for this flag.

玻璃人 2024-08-31 05:35:45

不过,您正在做的事情似乎是一个非常糟糕的主意,最好只是从处理程序中设置一个标志并从中返回,然后从 main 进行打印。

您需要设置 SA_NODEFER 或以其他方式在信号处理程序本身内重新启用信号,否则信号会在调用处理程序之前被阻止或切换回其默认行为。

从信号处理程序调用 printf 是未定义的行为。它可能会使你的程序崩溃。实际上可以从信号处理程序安全调用的函数列表非常有限。 我需要异步信号安全列表glibc 的函数

What you are doing seems like a very bad idea, though, and it might be better to simply set a flag from the handler and return from it, and then do the printing from main.

You need to set SA_NODEFER or otherwise re-enable the signal within the signal handler itself because otherwise the signal gets blocked or switched back to its default behavior right before the call to the handler.

Calling printf from a signal handler is undefined behavior. It may crash your program. The list of functions that you actually can safely call from a signal handler is very limited. I need a list of Async-Signal-Safe Functions from glibc

怪我入戏太深 2024-08-31 05:35:45

在信号处理程序中使用 printf 函数并不是一个好主意,因为它可能会导致未定义的行为!代码示例缺少信号处理程序工作的重要部分......请查看我的博客关于此 此处。如何捕获分段错误?'

另外,您需要用更强大的东西替换 while 循环,作为在测试信号处理程序时退出程序的方法......例如...... .你如何戒掉它?

Using the printf function within a signal handler is not exactly a good idea to use as it can cause behavior that is undefined! The code sample is missing a vital bit for the signal handler to work....Have a look at my blog about this here on 'Q6. How to trap a Segmentation fault?'

Also, you need to replace the while loop with something more robust as a way to quit the program while testing the signal handler...like...how do you quit it?

烟雨凡馨 2024-08-31 05:35:45

您可以使用类似这样的东西来代替 printf:

const char *str = "Into handler\n";
write(1, str, strlen(str));

函数 write(..) 可以安全地从信号处理程序中调用。
不要忘记包含 unistd.h 标头来使用它。

You can use something like this instead of printf:

const char *str = "Into handler\n";
write(1, str, strlen(str));

The function write(..) is safe to be called from signal hanlder.
Don't forget to include unistd.h header to use it.

放手` 2024-08-31 05:35:45

处理程序中的“while(1)”阻止第一个服务调用返回。删除该中断和后续中断应该会​​导致处理程序再次被调用。

中断服务例程不应阻止调用线程返回。

The "while(1)" in handler is preventing the first service call from ever returning. Remove that and subsequent interrupts should cause handler to be called again.

An interrupt service routine should not prevent the calling thread from returning.

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