【问题标题】:How to bind program termination with end-of-stream in Boost.Process 0.5?如何在 Boost.Process 0.5 中将程序终止与流结束绑定?
【发布时间】:2012-09-08 07:57:58
【问题描述】:

在这个 Boost.Process 0.5 (http://www.highscore.de/boost/process0.5/index.html) 的简单示例中,程序 (ls) 的输出正在提供一个流。流工作正常,但与预期相反,在程序完成后流不会变得无效(例如流结束)(类似于以前版本的 Boost.Process,例如http://www.highscore.de/boost/process/index.html)

为了使流(示例中为is)在子程序退出后自动失效,我缺少什么?

也许这是我必须在 file_descriptor 的 Boost.Streams stream 中设置的一个选项?

#include <boost/process.hpp> // version 0.5 from http://www.highscore.de/boost/process0.5/process.zip
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <string>
using namespace boost::process;
using namespace boost::process::initializers;
using namespace boost::iostreams;
int main(){
    boost::process::pipe p = create_pipe();
    file_descriptor_sink sink(p.sink, close_handle);
    child c = execute(run_exe("/usr/bin/ls"), bind_stdout(sink));
    file_descriptor_source source(p.source,  close_handle);
    stream<file_descriptor_source> is(source);
    std::string s;
    while(std::getline(is, s)){
        std::cout << "read: " << s << std::endl;
    }
    std::clog << "end" << std::endl; // never reach
}

【问题讨论】:

  • 只是一个不相关的提示:循环条件中不需要std::getline(is, s) and is,只需std::getline(is, s) 就足够了。
  • @Joachim,谢谢,我尝试了不同的技巧后把它留在那里,我编辑了问题,所以它不那么混乱。

标签: c++ boost process boost-iostreams


【解决方案1】:

2020 年更新: Boost.Process 现在是 Boost https://www.boost.org/doc/libs/1_74_0/doc/html/process.html 的一部分,这个答案可能已经完全过时了。它仅适用于实验版本“0.5”http://www.highscore.de/boost/process0.5/index.html。


我与图书馆的作者 Boris Schaeling 进行了私人(实际上是通过 Nabble)通信。在放弃了几种可能性之后,比如 posix/boost.iostreams 中的错误,他给了我对有效代码的轻微修改。基本上,我可以推断出file_descriptor sink 必须超出范围(已销毁)才能使流返回 EOF。工作代码只是为sink 添加了一个特定的范围(在末尾列出)。我认为这很容易将所有内容封装在 pistream 类中。 (我列表中的下一步将是允许输出到进程。)

适用于 Boost 1.48 (Fedora 17)。

#include <boost/process.hpp> // version 0.5
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <string>

using namespace boost::process;
using namespace boost::process::initializers;
using namespace boost::iostreams;

int main() {
    pipe p = create_pipe();
    {
        // note the scope for sink
        file_descriptor_sink sink(p.sink, close_handle);
        /*  child c = */ // not necessary to hold a child object, it seems.
        execute(run_exe("/usr/bin/ls"), bind_stdout(sink));
    }   // note the scope for sink

    file_descriptor_source source(p.source,  close_handle);
    stream<file_descriptor_source> is(source);
    std::string s;
    while(std::getline(is, s)) {
        std::cout << "read: " << s << std::endl;
    }
    std::clog << "end" << std::endl; // never reach
}

用c(lang)++ -lboost_system -lboost_iostreams编译

编辑:这似乎也有效,避免了人为范围,但可能会令人困惑,因为接收器必须是临时的:

    ...
    pipe p = create_pipe();
    execute(run_exe("/usr/bin/ls"), bind_stdout(        
        file_descriptor_sink(p.sink, close_handle)
    ));
    file_descriptor_source source(p.source,  close_handle);
    ...

【讨论】:

  • 这是真正微妙的行为。感谢您分享这些知识。
【解决方案2】:

这适用于类似 POSIX 的系统:

#include <boost/process.hpp> // version 0.5 from http://www.highscore.de/boost/process0.5/process.zip
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/asio.hpp>
#include <string>

using namespace boost::process;
using namespace boost::process::initializers;
using namespace boost::iostreams;

int main()
{
    boost::asio::io_service io_service;
    int status = 1;
    boost::asio::signal_set set(io_service, SIGCHLD);
    set.async_wait(
        [&status](const boost::system::error_code&, int) { ::wait(&status); }
    );

    boost::process::pipe p = create_pipe();
    file_descriptor_sink sink(p.sink, close_handle);
    child c = execute(run_exe("/bin/ls"), bind_stdout(sink));
    file_descriptor_source source(p.source,  close_handle);
    stream<file_descriptor_source> is(source);
    std::string s;
    while(status && std::getline(is, s))
    {
        std::cout << "read: " << s << std::endl;
    }
    std::clog << "end" << std::endl; // never reach
}

请注意,它将 SIGCHLD 处理为异步设置status。它取自http://www.highscore.de/boost/process0.5/boost_process/tutorial.html#boost_process.tutorial.starting_a_program。此页面还显示了 Windows 的相同做法:

#if defined(BOOST_WINDOWS_API)
    DWORD exit_code;
    boost::asio::windows::object_handle handle(io_service, c.process_handle());
    handle.async_wait(
        [&handle, &exit_code](const boost::system::error_code&)
            { ::GetExitCodeProcess(handle.native(), &exit_code); }
    );
#endif

    io_service.run();

【讨论】:

  • 这个 Boost.Process 库变得过于复杂,所以我必须异步运行该进程并从流中单独检查程序的状态。这很疯狂!它还使循环的语法与其他流不兼容。不仅如此,现在还需要与 boost_streams 和 boost_asio 进行链接。它在 Boost.Process 0.3 中要简单得多,更不用说旧的 pstreams.sourceforge.net。
  • 呃。我真的无能为力,我害怕。此外,如果需要,您可以隐藏额外的复杂性。此外,Boot Asio 仅是标头,因此 not 会引入额外的依赖项。 (将它包装在一个类中?)。对不起,如果我的回答让你生气了。当然,您可以随意使用其他东西:)
  • 至于包装它会做很多工作,因为我必须创建一个行为几乎像包装的流的类,这将是一个令人头疼的问题,因为我不熟悉流背后的科学我可能会做得很差。目前我会坚持使用 Boos.Process 0.3(或 GSOC2010),也许我要求的这个功能是库的最后一个版本中的无意遗漏。历史行为是退出程序返回 EOF 状态(想想任何 posix 管道)。
  • 您使用什么版本的 Boost/Boost.Iostream 来编译您的代码?
  • @alfC 我刚用 1_51 试过,不过可能更早 1.46-1.51
猜你喜欢
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-05-27
相关资源
最近更新 更多