如何使用 boost.process 重定向标准输入和标准输出

how to redirect stdin and stdout using boost.process

我正在尝试重定向子进程的标准输入和标准输出。 想用缓冲区中的二进制数据填充进程的标准输入并读取它,(但现在我只需要知道有多少写入标准输出)

namespace  bp = boost::process;
bp::opstream in;
bp::ipstream out;

bp::child c(Cmd.c_str(), bp::std_out > out, bp::std_in < in);

in.write((char*)buffer,bufferSize);
integer_type totalRead = 0;
char a[10240];
while (out.read(a,10240))  totalRead += out.gcount();
c.terminate();

write 看起来是成功的,但是程序卡在了 reading-while 循环中, 进程(子进程和父进程)在此

期间保持空闲

工作代码,看起来我必须关闭内部管道才能设置 child 的标准输入 eof(child 读取标准输入直到 eof(在我的例子中)):

namespace  bp = boost::process;
bp::opstream in;
bp::ipstream out;

bp::child c(Cmd.c_str(), bp::std_out > out, bp::std_in < in);    
in.write((char*)buffer,bufferSize);

in.pipe().close();

integer_type totalRead = 0;
char a[10240];
while (out.read(a,10240))  totalRead += out.gcount();
c.terminate();