我正在尝试重定向子进程的标准输入和标准输出。想用来自缓冲区的二进制数据填充进程的标准输入并读取它,(但现在我只需要知道有多少写入标准输出)
namespace bp = boost::process;
bp::opstream in;
bp::ipstream out;
bp::child c(Cmd.c_str(), bp::std_out > out, bp::std_in < in);
in.write((char*)buffer,bufferSize);
integer_type totalRead = 0;
char a[10240];
while (out.read(a,10240)) totalRead += out.gcount();
c.terminate();
Run Code Online (Sandbox Code Playgroud)
write 看起来是成功的,但程序卡在了 read-while 循环中,进程(子进程和父进程)在此期间保持空闲
考虑以下代码 -
#include <variant>
#include <string>
int p(std::variant<bool, std::string> v) {
return v.index();
}
int main() {
return p("ad");
}
Run Code Online (Sandbox Code Playgroud)
而不是选择std::string, p 将使用包含bool(我想要std::string)的变体进行实例化,这可以使用显式指定来解决,std::string但这工作量太大,我尝试提供不同的重载,但似乎不起作用。
我正在尝试使用折叠表达式来简化一些代码。在下面的代码中,我尝试将元素插入到数组中,但折叠表达式无法编译
struct test {
std::string cmd[20];
test() {
int i = 0;
auto insert = [&](auto... c) {
assert(i < 20);
(cmd[i++] = c), ...;
};
insert("c");
insert("c", "c2");
}
};
Run Code Online (Sandbox Code Playgroud)
编译器抱怨缺少“;”
我正在尝试使用 boost.process 写入和读取子级的 stdio,如下所示:
boost::asio::io_service writeService, readService;
bp::async_pipe in{writeService};
bp::async_pipe out{readService};
bp::child process(CompressCmd.c_str(), bp::std_in < in, bp::std_out > out);
Buffer src;
src.reserve(4 * 1024 * 1024);
integer_type read = 0;
//std::atomic_int64_t totalWrite{0};
integer_type totalWrite = 0;
while (callback(CallbackActions::NeedMoreInput, src, read)) {
in.async_write_some(
boost::asio::buffer(src.data(), read),
[](const boost::system::error_code &e, std::size_t) { });
// written data is not important, that's why using same buffer
out.async_read_some(boost::asio::buffer(src.data(), src.capacity()),
[&](const boost::system::error_code &e,
std::size_t byte_transferred) { totalWrite += byte_transferred; });
}
writeService.run();
in.close();
readService.run();
Run Code Online (Sandbox Code Playgroud)
所有读写操作都被通知为成功,但totalWrite的值完全不正确,报告为29356032,实际值应该约为50000000
我注意到程序正在中途终止,
在readService.run()冻结子进程后使用process.wait(), …