我在 Linux 的主应用程序中使用 boost::process
v. 1.65.1 创建一些 boost::process::child
对象并管理交换的数据通过 boost::process::std_in
和 boost::process::std_out
即管道。
当我的主应用程序收到控制台发送的 CTRL-C 时,我看到子应用程序也收到了 CTRL-C 信号。
为了终止我的 child ,我更愿意通过管道发送一个明确的命令,但是当我这样做时,信号已经传播了。实际上,一些 child 看到了其他人没有看到的命令并看到了信号。
- 这种信号传播是正常行为吗?
- 我可以做些什么来防止这种情况发生,以便我可以通过管道发出我的命令而不受干扰?
最佳答案
Is this signal propagation the normal behavior?
这本身并不是传播,而是当您在 POSIX 终端中键入 Ctrl+C 时,SIGINT
信号被广播到 <终端的前台进程组的em>所有进程。进程组由 shell 管理,默认情况下 fork 处理保留在父组 ( source ) 中。
What I can do to prevent this to happen so that I can issue my command via pipe without interference?
拦截子进程中的 SIGINT
并执行必要的清理:
#include <boost/asio/signal_set.hpp>
#include <iostream>
void exit_handler(const boost::system::error_code&, int signal_number)
{
std::cerr << "Signal " << signal_number << "!\n";
exit(1);
}
int main()
{
boost::asio::io_service io_service;
boost::asio::signal_set signals(io_service, SIGINT);
signals.async_wait( exit_handler );
io_service.run();
}
考虑其他信号(HUP
、TERM
)可能是个好主意。
关于c++ - 从 boost::process 到 boost::child 的信号传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49773319/