gpt4 book ai didi

c++ - Boost::进程管链

转载 作者:太空宇宙 更新时间:2023-11-04 12:48:46 27 4
gpt4 key购买 nike

使用这个问题的结果 simultaneous read and write to child's stdio using boost.process ,我正在尝试修改代码,以便读取文件,通过 gzip 传输,gzip 的输出通过 bzip2 传输,最后将 bzip2 的输出写入文件。

我的第一次尝试是

/*
* ProcessPipe.cpp
*
* Created on: Apr 17, 2018
* Author: dbetz
*/

//#define BOOST_ASIO_ENABLE_HANDLER_TRACKING 1

#include <boost/asio.hpp>
#include <boost/asio/high_resolution_timer.hpp>
#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <iostream>
#include <fstream>
#include <functional>

namespace bp = boost::process;
using boost::system::error_code;
using namespace std::chrono_literals;

using Loop = std::function<void()>;
using Buffer = std::array<char, 500>;

int main() {
boost::asio::io_service svc;
auto gzip_exit = [](int code, std::error_code ec) {
std::cout << "gzip exited " << code << " (" << ec.message() << ")\n";
};

auto bzip2_exit = [](int code, std::error_code ec) {
std::cout << "bzip2 exited " << code << " (" << ec.message() << ")\n";
};

bp::async_pipe file_to_gzip_pipe{svc}, gzip_to_bzip_pipe{svc}, bzip_to_file_pipe{svc};
bp::child process_gzip("/usr/bin/gzip", "-c", bp::std_in < file_to_gzip_pipe, bp::std_out > gzip_to_bzip_pipe, svc, bp::on_exit(gzip_exit));
bp::child process_bzip2("/usr/bin/bzip2", "-c", bp::std_in < gzip_to_bzip_pipe, bp::std_out > bzip_to_file_pipe, svc, bp::on_exit(bzip2_exit));

std::ifstream ifs("src/ProcessPipe2.cpp");

Buffer file_to_gzip_buffer;
Loop file_to_gzip_loop;
file_to_gzip_loop = [&] {
if (!ifs.good())
{
error_code ec;
file_to_gzip_pipe.close(ec);
std::cout << "Read file, write gzip: closed stdin (" << ec.message() << ")\n";
return;
}

ifs.read(file_to_gzip_buffer.data(), file_to_gzip_buffer.size());

boost::asio::async_write(file_to_gzip_pipe, boost::asio::buffer(file_to_gzip_buffer.data(), ifs.gcount()),
[&](error_code ec, size_t transferred) {
std::cout << "Read file, write gzip: " << ec.message() << " sent " << transferred << " bytes\n";
if (!ec) {
file_to_gzip_loop(); // continue writing
}
});
};

Buffer gzip_to_bzip_buffer;
Loop gzip_to_bzip_loop;

gzip_to_bzip_loop=[&] {
gzip_to_bzip_pipe.async_read_some(boost::asio::buffer(gzip_to_bzip_buffer),
[&](error_code ec, size_t transferred){
// duplicate buffer
std::cout << "Read gzip, write bzip: " << ec.message() << " got " << transferred << " bytes\n";
if (!ec)
gzip_to_bzip_loop();
else
gzip_to_bzip_pipe.close();
}
);
};


std::ofstream ofs("src/ProcessPipe2.gz");

Buffer bzip_to_file_buffer;
Loop bzip_to_file_loop;
bzip_to_file_loop = [&] {
bzip_to_file_pipe.async_read_some(boost::asio::buffer(bzip_to_file_buffer),
[&](error_code ec, size_t transferred) {
std::cout << "Read bzip, write file: " << ec.message() << " got " << transferred << " bytes\n";
ofs << std::string(bzip_to_file_buffer.data(),transferred);
if (!ec)
bzip_to_file_loop(); // continue reading
});
};

file_to_gzip_loop(); // async
gzip_to_bzip_loop();
bzip_to_file_loop(); // async

svc.run(); // Await all async operations
}

但这给出了一个错误:

Read gzip, write bzip: Bad file descriptor got 0 bytes

问题似乎是 gzip_to_bzip_pipe 被 gzip 写入和 bzip 读取打开。有什么想法吗?

最佳答案

我会像这样简单地编写代码:

#include <boost/process.hpp>
#include <iostream>

namespace bp = boost::process;

int main() {
bp::pipe intermediate;

bp::child process_gzip("/bin/gzip", "-c", bp::std_in<"src/ProcessPipe2.cpp", bp::std_out> intermediate);
bp::child process_bzip2("/bin/bzip2", "-c", bp::std_in<intermediate, bp::std_out> "src/ProcessPipe2.gz.bz2");

process_bzip2.wait();
process_bzip2.wait();
}

奖金

您可以完全不使用子进程,只使用 boost::iostreams::copy:

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/copy.hpp>
#include <iostream>
#include <fstream>

namespace io = boost::iostreams;

int main() {
std::ifstream ifs("src/ProcessPipe2.cpp");

io::filtering_stream<io::output> os;
os.push(io::gzip_compressor());
os.push(io::bzip2_compressor());

std::ofstream ofs("src/ProcessPipe2.gz.bz2");
os.push(ofs);

io::copy(ifs, os);
}

关于c++ - Boost::进程管链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50066371/

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