boost :: process写入标准输入

Zin*_*ish 5 c++ stdin boost process

我有一个启动boost :: process应用程序的线程,该线程运行的代码如下:

void Service::Run()
{
    printf("Starting thread for service ID %i\n", this->sid);

    // Set up a stream sink for stdout, stderr and stdin
    bprocess::pipe pstdIn    = create_pipe();
    bprocess::pipe pstdOut   = create_pipe();
    file_descriptor_sink      fdSink(pstdIn.sink, close_handle);
    file_descriptor_source    fdSrc(pstdOut.sink, close_handle);

    // Set up the read write streams
    this->stdIn  = new fdistream(fdSink);
    this->stdOut = new fdostream(fdSrc);

    // Execute the service
    try
    {
        child proc = execute(
            set_args(this->args),
            inherit_env(),
            bind_stdin(fdSrc),

            throw_on_error()
        );

        printf("PID: %i\n", proc.pid);

        // Wait for the application to end then call a cleanup function
        int exit_code = wait_for_exit(proc);

        printf("Service died, error code: %i\n", exit_code);
    }
    catch(boost::system::system_error err)
    {
        printf("Something went wrong: %s\n", err.what());
    }

    this->manager->ServiceDie(this->sid);

    return;
}
Run Code Online (Sandbox Code Playgroud)

由于此功能是一个阻塞功能,因此它实际上等待服务被终止(在外部或根据我的需要;通过stdin输入以优雅地停止应用程序)。

我不知道如何写子进程的标准输入。我已经试过了:

*(this->stdIn) << "stop\n";
Run Code Online (Sandbox Code Playgroud)

在该类的公共函数中Service被另一个线程(Manager类)调用。但是,这没有结果。

我如何写孩子的标准输入proc

seh*_*ehe 2

这是根据此处示例改编的演示:

您可以在 Coliru 上看到它的直播遗憾的是,这正在突破 Coliru 的极限。可能会稍后:

#include <boost/process.hpp> 
#include <boost/assign/list_of.hpp> 
#include <string> 
#include <vector> 
#include <iostream> 

using namespace boost::process; 

int main() 
{ 
    std::string exec = find_executable_in_path("rev"); 
    std::vector<std::string> args = boost::assign::list_of("rev"); 

    context ctx; 
    ctx.environment = self::get_environment(); 
    ctx.stdin_behavior = capture_stream(); 
    ctx.stdout_behavior = capture_stream(); 
    child c = launch(exec, args, ctx); 

    postream &os = c.get_stdin();
    pistream &is = c.get_stdout(); 

    os << "some\ncookies\nfor\ntasting";
    os.close();

    std::cout << is.rdbuf(); 
} 
Run Code Online (Sandbox Code Playgroud)

输出:

emos
seikooc
rof
gnitsat
Run Code Online (Sandbox Code Playgroud)

现在,如果您需要真正的异步,可以在该页面下方找到另一个使用 Boost Asio 的示例