c++ - 从 boost::process 到 boost::child 的信号传播

标签 c++ linux signals boost-process

我在 Linux 的主应用程序中使用 boost::process v. 1.65.1 创建一些 boost::process::child 对象并管理交换的数据通过 boost::process::std_inboost::process::std_out 即管道。

当我的主应用程序收到控制台发送的 CTRL-C 时,我看到子应用程序也收到了 CTRL-C 信号。

为了终止我的 child ,我更愿意通过管道发送一个明确的命令,但是当我这样做时,信号已经传播了。实际上,一些 child 看到了其他人没有看到的命令并看到了信号。

  1. 这种信号传播是正常行为吗?
  2. 我可以做些什么来防止这种情况发生,以便我可以通过管道发出我的命令而不受干扰?

最佳答案

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();
}

考虑其他信号(HUPTERM)可能是个好主意。

关于c++ - 从 boost::process 到 boost::child 的信号传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49773319/

相关文章:

c++ - "extern"在函数内部?

c++ - OpenCV 廉价立体相机无法同时加载两个流

c# - 覆盖 dll 时如何阻止 .NET Core 应用程序关闭?

linux rdma mellanox 说明

c - 如何通过kill命令从子进程向父进程发送信号

c++ - 表达式 `new T` 的计算结果是右值还是左值?

linux - OpenDKIM 不签署电子邮件

c++ - Qt 根据参数类型触发不同的信号

c - 如何用GTK Webkit和WebkitWebView下载

c++ - 在最佳时间从 _variant_t 获取 char*