如何程序终止绑定尾流中Boost.Process 0.5? [英] How to bind program termination with end-of-stream in Boost.Process 0.5?

查看:153
本文介绍了如何程序终止绑定尾流中Boost.Process 0.5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的Boost.Process 0.5这个简单的例子(<一个href=\"http://www.highscore.de/boost/process0.5/index.html\">http://www.highscore.de/boost/process0.5/index.html)一个程序( LS )的输出是喂养流。流工作正常,但出乎预期该流不失效(如尾流)程序结束(类似Boost.Process的,如:<一previous版本后, href=\"http://www.highscore.de/boost/process/index.html\">http://www.highscore.de/boost/process/index.html)

我是什么,以便使(示例中的)流子程序退出后自动失效失踪?

也许是,我在的Boost.Streams 来设置一个选项类file_descriptor

 的#include&LT;升压/ process.hpp&GT; // 0.5版本从http://www.highscore.de/boost/process0.5/process.zip
#包括LT&;提升/输入输出流/设备/ file_descriptor.hpp&GT;
#包括LT&;升压/输入输出流/ stream.hpp&GT;
#包括LT&;串GT;
使用空间boost ::进程;
使用空间boost ::过程::初始化;
使用空间boost ::输入输出流;
诠释主(){
    提高::进程::管P = create_pipe();
    file_descriptor_sink水槽(p.sink,close_handle);
    孩子C =执行(run_exe(在/ usr /斌/ LS),bind_stdout(汇));
    file_descriptor_source源(p.source,close_handle);
    流&LT; file_descriptor_source&GT;是(源);
    性病::字符串s;
    而(的std ::函数getline(是,S)){
        性病::法院LT&;&LT; 读:&LT;&LT;小号所述&;&下;的std :: ENDL;
    }
    的std ::堵塞&LT;&LT; 结束&LT;&LT;的std :: ENDL; //永远达不到
}


解决方案

我与鲍里斯Schaeling,图书馆的作者的私人(实际上是通过Nabble)通信。丢弃几种可能性,像POSIX /虫子了Boost.Iostreams后,他给我的code,它的工作原理略有修改。基本上,我可以推断的是,类file_descriptor汇必须是超出范围(破坏)为了使流返回EOF。工作code只是增加了对特定范围汇(末尾列出)。我想,这使得很容易封装了所有在 pistream 的类也。 (在我的名单下一步将是允许还输出到过程。)

与升压1.48(Fedora的17)的作品。

 的#include&LT;升压/ process.hpp&GT; // 0.5版
#包括LT&;提升/输入输出流/设备/ file_descriptor.hpp&GT;
#包括LT&;升压/输入输出流/ stream.hpp&GT;
#包括LT&;串GT;使用空间boost ::进程;
使用空间boost ::过程::初始化;
使用空间boost ::输入输出流;诠释主(){
    配管p = create_pipe();
    {
        //注意,水槽范围
        file_descriptor_sink水槽(p.sink,close_handle);
        / *子C = * / //没有必要举行一个子对象,它似乎。
        执行(run_exe(在/ usr /斌/ LS),bind_stdout(汇));
    } //注意,水槽范围    file_descriptor_source源(p.source,close_handle);
    流&LT; file_descriptor_source&GT;是(源);
    性病::字符串s;
    而(的std ::函数getline(是,S)){
        性病::法院LT&;&LT; 读:&LT;&LT;小号所述&;&下;的std :: ENDL;
    }
    的std ::堵塞&LT;&LT; 结束&LT;&LT;的std :: ENDL; //永远达不到
}

编译与 C(郎)++ -lboost_system -lboost_iostreams

编辑:这似乎是工作为好,这避免了人工范围,但由于片必须是可能会造成混淆临时

  ...
    配管p = create_pipe();
    执行(run_exe(在/ usr /斌/ LS),bind_stdout(
        file_descriptor_sink(p.sink,close_handle)
    ));
    file_descriptor_source源(p.source,close_handle);
    ...

In this simple example of Boost.Process 0.5 ( http://www.highscore.de/boost/process0.5/index.html) the output of a program (ls) is feeding a stream. The stream works fine but contrary to the expectation the stream doesn't become invalid (e.g. end-of-stream) after the program finishes (similar to previous version of Boost.Process, e.g. http://www.highscore.de/boost/process/index.html)

What am I missing in order to make the stream (is in the example) automatically invalid after child program exits?

Perhaps is it an option that I have to set in the Boost.Streams stream of file_descriptor?

#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
}

解决方案

I had a private (actually through Nabble) communication with Boris Schaeling, the author of the library. After discarding several possibilities, like bugs in posix/boost.iostreams, he gave me a slight modification of the code that works. Basically, what I can deduce is that the file_descriptor sink must be out of scope (destroyed) in order for the stream to return an EOF. The working code simply adds a specific scope for sink (listed at the end). I think this makes easy to encapsulate all in a pistream kind of class. (Next step in my list will be to allow also output to the process.)

Works with 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
}

Compiles with c(lang)++ -lboost_system -lboost_iostreams

EDIT: This seems to work as well, which avoid the artificial scope, but can be confusing because the sink has to be a temporary:

    ...
    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);
    ...

这篇关于如何程序终止绑定尾流中Boost.Process 0.5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆