gpt4 book ai didi

c++ - 无法捕获 CLion 发送的 SIGINT

转载 作者:太空狗 更新时间:2023-10-29 21:11:14 24 4
gpt4 key购买 nike

我正在使用 CLion 进行一些网络编程项目。由于我正在使用套接字,如果我没有彻底关闭我正在使用的套接字和其他网络对象,那么程序将阻塞该端口,我将不得不使用命令行自行关闭它。

这很烦人,考虑到我必须在每次测试之间执行此操作。所以我寻找当您单击“停止”时 CLion 终止程序的方式,我发现它发送一个 SIGINT 信号,here :

Click this button to terminate the current process externally by means of the standard shutdown script. Clicking the button once invokes soft kill allowing the application to catch the SIGINT event and perform graceful termination (on Windows, the Ctrl+C event is emulated). After the button is clicked once, it is replaced with icon run tool window kill indicating that subsequent click will lead to force termination of the application, e.g. on Unix SIGKILL is sent.

然后,我一直想抓它,但由于某些原因,它并没有起作用。所以我尝试创建一个新项目,我只是想在其中捕获 SIGINT 信号。这是一个非常简单的非工作示例:

#include <iostream>

void signalHandling(int signal)
{
std::cout << "Signal : " << signal << std::endl;
}

int main() {
signal(SIGINT, signalHandling);
while (1)
{
;
}
}

我得到的是:进程已完成,退出代码为 1

所以,我的问题是:为什么我无法捕捉到 CLion 发送的这个信号,或者至少应该发送这个信号?

可能与我的环境有关,我使用的是 CLion 2018.1.5,我的工具链是 Cygwin,我使用的是 cmake 3.10.3。我在 Windows 10 上工作。

最佳答案

这是我从@VTT 的帮助中得出的结论。

提供的示例应该有效,并且在不使用 CLion 时确实有效。我尝试在一个终端上实际执行 CTRL-C 并且它有效。我的程序收到了 SIGINT 信号。

所以这个问题可能是 CLion 错误,因为停止按钮实际上应该模拟 CTRL-C 并发送 SIGINT 信号。

我已将错误报告发送给 JetBrain 的团队,并将等待他们的最终反馈,并让您知道。

再次感谢@VTT。


编辑:

我确实联系了 CLion 支持,他们只是将我重定向到另一个类似的 issue由另一个 CLion 用户编写。所以我猜他们不会做任何事情。

经过一些研究,我去了这个 issue .几乎是同一件事,除了它说 CLion 没有正确终止进程,这是我的情况。

显然,由于他们的停止脚本,这是一个错误。我找到了一种让 CLion 真正结束进程的方法。

警告!

您应该考虑到更改注册表选项可能很危险!使用以下解决方案需要您自担风险!

这是我做的:

  • 点击“帮助”
  • 点击查找操作
  • 键入并选择注册表
  • 取消选中 run.processes.with.pty

就是这样!

You must know that this will not allow you to catch SIGINT signal supposed to be sent by the "stop" button of CLion, but it does kill the process, unlike the stop button. Please also note that this is not the actual answer to the question, but as I was working on a programming project, I had to manually kill my server to run it again. So it solved my problem, and might also solve someone's else.

关于c++ - 无法捕获 CLion 发送的 SIGINT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51169357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com