3

I want to boost-process to start a process and write/read to its stdin/stdout. In principle the code works but it doesn't terminate. It seems to me that the external program does not receive a EOF, although I close the stream to write to the process' stdin. Any ideas how to make the process send a EOF? The code works if I remove the code to write to /bin/cat stdin. Then I can input the text and on Ctrl-D it terminates. Any ideas how to send a EOF over the stream?

My code:

std::vector<std::string>  args;
args.push_back("/bin/cat");

boost::process::pipe procout = boost::process::create_pipe();
boost::process::pipe procin = boost::process::create_pipe();
{
    boost::iostreams::file_descriptor_sink fdsink(procout.sink,
                                  boost::iostreams::close_handle);
    boost::iostreams::file_descriptor_source fdsource(procin.source,
                            boost::iostreams::close_handle);
    boost::process::execute(
        boost::process::initializers::set_args(args),
        boost::process::initializers::bind_stdin(fdsource),
        boost::process::initializers::bind_stdout(fdsink)
        );
}
boost::iostreams::file_descriptor_source source(procout.source,
                        boost::iostreams::close_handle);
boost::iostreams::file_descriptor_sink sink(procin.sink,
                        boost::iostreams::close_handle);
boost::iostreams::stream<boost::iostreams::file_descriptor_source> is(source);
boost::iostreams::stream<boost::iostreams::file_descriptor_sink> os(sink);
os << "Some text " << std::endl;
os.close();
std::string s;
for (;;) {
    std::getline(is, s);
    if (is.eof()) {
        break;
    }
    std::cout << s << std::endl;
}
pyfex
  • 1,195
  • 1
  • 8
  • 13
  • 1
    It's often better to do e.g. `if (!std::getline(...)) { /* error or eof */ }` Try that. – Some programmer dude Jul 24 '13 at 18:36
  • 1
    possible duplicate of [How to bind program termination with end-of-stream in Boost.Process 0.5?](http://stackoverflow.com/questions/12329065/how-to-bind-program-termination-with-end-of-stream-in-boost-process-0-5) – n. m. could be an AI Jul 24 '13 at 18:59
  • If I reduce the program as in the mentioned article it works (as I meant with "The code works if I remove the code to write to /bin/cat stdin"). The problem only occurs if also write to the stream, which is something that is not discussed or asked in this question. – pyfex Jul 25 '13 at 07:07
  • 1
    using if (!std::getline(...)) instead of eof() doesn't change anything in the behavior. – pyfex Jul 25 '13 at 17:41

0 Answers0