how to redirect stdin and stdout using boost.process
Asked Answered
W

1

7

i am trying to redirect both stdin and stdout of a child process. want to fill the stdin of the process with binary data from buffers and read that,(but for now i only need to know how much is written to stdout)

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 looks to be successful but program got stuck in reading-while loop, process(both child and parent) remains idle during this

Wrongly answered 7/2, 2018 at 6:34 Comment(1)
If your child process is something like echo then you probably need to close your write pipe before it will stop trying to read from it and exit (closing its write end of your read pipe as it does so).Messuage
W
6

Working Code, looks like i have to close the internal pipe to set the child's stdin eof(child reads stdin until eof (in my case)) :

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();
Wrongly answered 7/2, 2018 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.