我想升级进程以启动进程并写入/读取其stdin / stdout。原则上代码可以工作但不会终止。在我看来,外部程序没有收到EOF,虽然我关闭流写入进程'stdin。任何想法如何使过程发送EOF?如果我删除要写入/ bin / cat stdin的代码,代码就可以工作。然后我可以输入文本,在Ctrl-D上它终止。有关如何通过流发送EOF的任何想法吗?
我的代码:
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;
}