这是关于Boost.Process 0.5的问题,而不是以后的问题或earlier version,Boost现在包含一个具有不同语法和功能的Boost.Process库。
假设我有一个简单的程序,要求输入一个数字并返回另一个数字,即:
// ask.x, simple program with IO
#include<iostream>
int main(){
double n;
std::cout << "number?" << std::endl;
std::cin >> n;
std::cout << n + 1 << std::endl;
}
现在,我希望通过Boost.Process 0.5(http://www.highscore.de/boost/process0.5/boost_process/tutorial.html)与该程序进行交互式编程。当我尝试使用该库时,我没有得到预期的行为,该号码永远不会发送到该程序。 (阅读第一行是好的)。我试着写一下http://www.highscore.de/boost/process0.5/boost_process/tutorial.html#boost_process.tutorial.synchronous_i_o中描述的例子的概括,但我失败了。
MWE,上半部分是很多必要的样板,也是我认为我犯错的地方。
#include <boost/process.hpp> // version 0.5
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <string>
#define ALSOSEND // for fully interactive case
using namespace boost;
int main() {
// Boilerplate code, see input only example here https://stackoverflow.com/questions/12329065/how-to-bind-program-termination-with-end-of-stream-in-boost-process-0-5
process::pipe pi = boost::process::create_pipe();
process::pipe po = boost::process::create_pipe();
{
iostreams::file_descriptor_sink sink(
pi.sink,
iostreams::close_handle
);
iostreams::file_descriptor_source source(
po.source,
boost::iostreams::close_handle
);
process::execute(
process::initializers::run_exe("./ask.x"),
process::initializers::bind_stdout(sink)
#ifdef ALSOSEND
, process::initializers::bind_stdin(source)
#endif
);
}
iostreams::file_descriptor_source fdsource(pi.source, iostreams::close_handle);
iostreams::stream<iostreams::file_descriptor_source> is(fdsource);
iostreams::file_descriptor_sink fdsink(po.source, iostreams::close_handle);
iostreams::stream<iostreams::file_descriptor_sink> os(fdsink);
// actual interaction with the process
std::string line;
std::getline(is, line);
assert(line == "number?");
std::cout << "sending: " << "5" << std::endl;
os << "5" << std::endl; // RUN GETS STUCK HERE
std::getline(is, line);
assert(line == "6");
}
显然,我不了解接收器和源的逻辑。我也尝试使用单个pipe
作为接收器和源,但它没有用。
如何让程序从已执行的项目读取和写入?
我找不到输入和输出都是交错的例子。
编辑以显示使用早期版本库的工作示例
这是如何在Boost.Process GSOC2010中完成的(不是0.5作为 在上面的问题中),注意与实际的互动 程序与上面相同。
#include <boost/filesystem.hpp> // quasibug in process GSOC2010 needs to include filesystem BEFORE
#include <boost/process.hpp> // version GSOC2010 (not 0.5)
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <string>
using namespace boost;
int main() {
// boiler plate code
std::vector<std::string> args;
process::context ctx;
ctx.process_name = "askprocess";
ctx.streams[process::stdout_id] = boost::process::behavior::pipe();
ctx.streams[process::stdin_id ] = boost::process::behavior::pipe();
process::child c = create_child("./ask", args, ctx);
process::pistream is(c.get_handle(process::stdout_id));
process::postream os(c.get_handle(process::stdin_id ));
// actual interaction with the process
std::string line;
std::getline(is, line);
assert(line == "number?");
std::cout << "sending: " << "5" << std::endl;
os << "5" << std::endl; // RUN GETS STUCK HERE
std::getline(is, line);
assert(line == "6");
}
答案 0 :(得分:2)
在以下行中似乎是一个拼写错误:
iostreams::file_descriptor_sink fdsink(po.source, iostreams::close_handle);
必须:
iostreams::file_descriptor_sink fdsink(po.sink, iostreams::close_handle);