我想提升进程来启动一个进程并写入/读取它的标准输入/标准输出。原则上,代码可以工作,但不会终止。在我看来,外部程序没有收到 EOF,尽管我关闭了流以写入进程的标准输入。任何想法如何使进程发送 EOF?如果我删除要写入 /bin/cat 标准输入的代码,则该代码有效。然后我可以输入文本并在 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;
}