简体   繁体   English

C++ 中的 SIGINT 信号()/sigaction

[英]SIGINT signal()/sigaction in C++

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.现在是我的信号()所接收的信号情报命令(CTRL + C正确?)当我的功能sigHandle应与2(SIGINT数)的整数值被称为的理解,该方法应该运行和程序应该出口。

All I would like to do is just print the signal number and move on, however after printing out "Signal: 2" it exits.我想要做的只是打印信号编号并继续前进,但是在打印出“信号:2”后它退出了。

(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.) (最终我应该处理前 32 个中断,但我认为Ctrl + C将是最困难的,所以我从这里开始。)

In main if I do signal(SIGINT, SIG_IGN);在主要,如果我做信号(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.它正确地忽略了信号并且不退出,但我现在无法知道我是否收到了 SIGINT 中断。

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.早些时候我在玩 sigaction 结构,但我找不到任何真正全面的文档,所以我决定只使用“原始”信号处理。

This was my sigaction code (same problem as above):这是我的 sigaction 代码(与上面的问题相同):

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

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!谢谢!

It is not.它不是。 You just replacing SIGINT's handles with same function.您只需用相同的功能替换 SIGINT 的句柄。 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.
}

Also note you should not call stdio (or other non-reentrant functions) in signal handlers.另请注意,您不应在信号处理程序中调用 stdio(或其他不可重入函数)。 (your signal handler might be invoked in the middle of a malloc or it's C++ equivalent) (您的信号处理程序可能会在 malloc 或 C++ 等价物的中间调用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM