gpt4 book ai didi

c++ - 如何与 Boost.Process 0.5 中的进程同步交互

转载 作者:搜寻专家 更新时间:2023-10-31 02:23:21 25 4
gpt4 key购买 nike

This is a question about Boost.Process 0.5, not any later or earlier version, Boost now contains a Boost.Process library with a different syntax and features.

假设我有一个简单的程序请求一个数字并返回另一个数字,即:

// 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 作为接收器和源,但它没有用。

如何让程序从执行的项目读取和写入?

我找不到输入和输出交错的例子。


编辑以显示早期版本库的工作示例

This how it used to be done in Boost.Process GSOC2010 (not 0.5 as in the question above), note that the actual interaction with the program is the same as above.

#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");
}

最佳答案

似乎是以下行中的错字:

iostreams::file_descriptor_sink fdsink(po.source,  iostreams::close_handle);

必须是:

iostreams::file_descriptor_sink fdsink(po.sink,  iostreams::close_handle);

关于c++ - 如何与 Boost.Process 0.5 中的进程同步交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29406929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com