无法捕获 CLion 发送的 SIGINT
Unable to catch SIGINT sent by CLion
我正在使用 CLion 进行一些网络编程项目。由于我正在使用套接字,如果我没有彻底关闭我正在使用的套接字和其他网络对象,那么程序将阻塞该端口,我将不得不使用命令行自行关闭它。
这很烦人,因为我必须在每次测试之间执行此操作。所以我寻找当你点击 "stop" 时 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)
{
;
}
}
我得到的是:Process finished with exit code 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 支持,他们只是将我重定向到另一个 CLion 用户编写的类似 issue。所以我猜他们不会做任何事情。
经过一些研究,我去了这个issue。几乎是同一件事,除了它说 CLion 没有正确终止进程,我就是这种情况。
显然,这是由于他们的停止脚本导致的错误。我找到了一种让 CLion 真正结束进程的方法。
警告!
您应该考虑到更改注册表选项可能很危险!使用以下解决方案需要您自担风险!
这是我所做的:
- 单击“帮助”
- 单击“查找”操作
- 类型和select 注册表
- 取消选中
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.
我正在寻找相同的问题,运行 进入以下 answer,由 Employed Russian 提供。这并不理想,但是我能够将 SIGINT 传递给我的进程并优雅地退出(在我的情况下,这是我的意图)。
我不喜欢这个方法的地方是你需要做太多的步骤:
- 将注册表配置为发送 SIGINT 而不是 SIGSTOP(WARNING 类似于 souki 的回答)
- 每当您想停止时 -> 暂停该过程。
- 发送指令
handle SIGTRAP nostop noprint pass
- 继续该过程。
我正在使用 CLion 进行一些网络编程项目。由于我正在使用套接字,如果我没有彻底关闭我正在使用的套接字和其他网络对象,那么程序将阻塞该端口,我将不得不使用命令行自行关闭它。
这很烦人,因为我必须在每次测试之间执行此操作。所以我寻找当你点击 "stop" 时 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)
{
;
}
}
我得到的是:Process finished with exit code 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 支持,他们只是将我重定向到另一个 CLion 用户编写的类似 issue。所以我猜他们不会做任何事情。
经过一些研究,我去了这个issue。几乎是同一件事,除了它说 CLion 没有正确终止进程,我就是这种情况。
显然,这是由于他们的停止脚本导致的错误。我找到了一种让 CLion 真正结束进程的方法。
警告!
您应该考虑到更改注册表选项可能很危险!使用以下解决方案需要您自担风险!
这是我所做的:
- 单击“帮助”
- 单击“查找”操作
- 类型和select 注册表
- 取消选中
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.
我正在寻找相同的问题,运行 进入以下 answer,由 Employed Russian 提供。这并不理想,但是我能够将 SIGINT 传递给我的进程并优雅地退出(在我的情况下,这是我的意图)。
我不喜欢这个方法的地方是你需要做太多的步骤:
- 将注册表配置为发送 SIGINT 而不是 SIGSTOP(WARNING 类似于 souki 的回答)
- 每当您想停止时 -> 暂停该过程。
- 发送指令
handle SIGTRAP nostop noprint pass
- 继续该过程。