C++ 中的 SIGINT signal()/sigaction

发布于 2024-12-07 19:56:56 字数 1280 浏览 0 评论 0原文

所以这是我的代码:

void sigHandle(int sig)
{
    signal(SIGINT, sigHandle);    //Is this line necessairy?
    cout<<"Signal: "<<sig<<endl;    
}

int main(){

    signal(SIGINT, sigHandle);

    while(true){ //Supposed to loop until user exits.

    //rest of my code
    
    }
}

现在我对 signal() 的理解是,当收到 SIGINT 命令(Ctrl+C 对吗?)时,我的函数 sigHandle 应该被调用整数值 2(SIGINT 数字),该方法应该运行,并且程序应该退出。

我想做的只是打印信号编号并继续,但是在打印出“Signal: 2”后它退出。

(最终我应该处理前 32 个中断,但我认为 Ctrl+C 将是最困难的,所以我从这里开始。)

在 main 中,如果我这样做signal(SIGINT, SIG_IGN); 它会正确忽略信号并且不会退出,但我现在无法知道是否收到了 SIGINT 中断。

早些时候,我正在研究 sigaction 结构,但我找不到任何真正全面的文档,所以我决定只使用“原始”信号处理。

这是我的 sigaction 代码(与上面相同的问题):

struct sigaction action;
action.sa_handler = sigHandle;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
sigaction(SIGINT, &action, 0);

感谢您的帮助!

编辑

好所以经过很多很多个小时的浏览手册页和互联网后,我遇到了一个(非常)贫民窟的解决方案,涉及保存堆栈预无限循环,然后当中断来临时,做我需要做的事情,然后重新-将堆栈设置回原来的位置并调用 sigrelse() 命令来重新设置可能已更改且未重新加载的任何状态。

我知道这不是解决这个问题的最优雅/最有效/甚至不是社会上可接受的解决方案,但它有效,而且据我所知,我没有在任何地方泄漏任何内存,所以这一切都很好......

我仍在寻找一个这个问题的解决方案,我认为我的堆栈重新设置恶作剧只是一个临时修复......

谢谢!

So here is my code:

void sigHandle(int sig)
{
    signal(SIGINT, sigHandle);    //Is this line necessairy?
    cout<<"Signal: "<<sig<<endl;    
}

int main(){

    signal(SIGINT, sigHandle);

    while(true){ //Supposed to loop until user exits.

    //rest of my code
    
    }
}

Now it is my understanding of signal() that when the SIGINT command (Ctrl+C right?) is received my function sigHandle should be called with an integer value of 2 (the SIGINT number), the method should run and the program should NOT exit.

All I would like to do is just print the signal number and move on, however after printing out "Signal: 2" it exits.

(Eventually I'm supposed to handle the first 32 interrupts but I figured Ctrl+C would be the most difficult so I'm starting here.)

In main if I do signal(SIGINT, SIG_IGN); it ignores the signal correctly and doesn't exit but I now have no way of knowing if I recieved the SIGINT interrupt.

Earlier I was playing around with the sigaction struct but I could not find any real comprehensive documentation on it so I decided to go with just "raw" signal handling.

This was my sigaction code (same problem as above):

struct sigaction action;
action.sa_handler = sigHandle;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
sigaction(SIGINT, &action, 0);

Thanks for your help!

EDIT

OK SO After many many many hours of scowering through man pages and the internet I have happened across a (very) ghetto solution involving saving the stack pre-infinite loop then when the interrupt comes, doing what I need to do, then re-setting the stack back to where it was and calling the sigrelse() command to re-set any states that might have been changed and not re-loaded.

I understand that this is not the most elegant/efficient/or even socially acceptable solution to this problem but it works and as far as I can tell I am not leaking any memory anywhere so it's all good...

I am still looking for a solution to this problem and I view my stack re-setting shenanigins as only a temporary fix...

Thanks!

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

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

发布评论

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

评论(2

執念 2024-12-14 19:56:56

另请注意,您不应在信号处理程序中调用 stdio(或其他不可重入函数)。
(您的信号处理程序可能会在 malloc 中间调用,或者它是 C++ 的等效项)

Also note you should not call stdio (or other non-reentrant functions) in signal handlers.
(your signal handler might be invoked in the middle of a malloc or it's C++ equivalent)

浮云落日 2024-12-14 19:56:56

它不是。您只需用相同的功能替换 SIGINT 的句柄即可。你如何编程执行等待?

如果你有类似的东西:

int main
{ 
 // ...

  int r = read(fd, &buff, read_size); // your program hangs here, waiting for the data.
                                      // but if signal occurred during this period of time
                                      // read will return immediately, and r may != read_size

  return 0;  // then it will go straight to return.
}

It is not. You just replacing SIGINT's handles with same function. How does you program perform wait?

If you have something like:

int main
{ 
 // ...

  int r = read(fd, &buff, read_size); // your program hangs here, waiting for the data.
                                      // but if signal occurred during this period of time
                                      // read will return immediately, and r may != read_size

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