关闭boost :: process子进程的stdin

问题描述:

我试图用一个字符串调用一个进程到它的stdin,用boost-1.64.0。 当前的代码是:关闭boost :: process子进程的stdin

bp::opstream inStream ; 
    bp::ipstream outStream; 
    bp::ipstream errStream; 

    bp::child child(
    command, // the command line 
    bp::shell, 
    bp::std_out > outStream, 
    bp::std_err > errStream, 
    bp::std_in < inStream); 


    // read the outStream/errStream in threads 

    child.wait(); 

的问题是,孩子可执行文件正在等待它的标准输入EOF。这里child.wait()无限期悬挂...

我试图使用asio :: buffer,std_in.close(),但是没有运气。 我发现的唯一破解是删除()inStream ...并且这不是很可靠。

我应该如何“通知”子进程并关闭它的stdin和新的boost ::进程库?

谢谢!

+0

你是什么意思,_“delete()inStream”_?这对我来说没有多大意义。 – sehe

我试图使用ASIO ::缓冲区,std_in.close()

这工作。当然,只有将它传递给启动函数(bp :: child构造函数,bp :: system等)才有效。

如果您需要传递数据,然后关闭它,只需关闭相关的文件描述符即可。我做这样的事情:

boost::asio::async_write(input, bp::buffer(_stdin_data), [&input](auto ec, auto bytes_written){ 
    if (ec) { 
     logger.log(LOG_WARNING) << "Standard input rejected: " << ec.message() << " after " << bytes_written << " bytes written"; 
    } 
    may_fail([&] { input.close(); }); 
}); 

哪里input

bp::async_pipe input(ios); 

此外,检查过程中实际上并没有停留在发送输出!如果你没有使用输出,它会缓冲并等待缓冲区已满。

+0

你节省了我的一周。我想may_fail是异常的自定义处理程序,所以我直接调用input.close()。你也必须调用ios.run():) – Salamandar

+0

是!对不起,这次复制不完整的代码:(http://paste.ubuntu.com/24701992/ – sehe

完成写入后,通过调用inStream.close();来关闭管道。您也可以在启动​​时关闭它。

asio解决方案当然也有效,避免了死锁的危险。