Skip to content

Instantly share code, notes, and snippets.

@mika-fischer
Last active October 20, 2020 16:17
Show Gist options
  • Save mika-fischer/e308f93aa987a4d1d64b0024c7df316d to your computer and use it in GitHub Desktop.
Save mika-fischer/e308f93aa987a4d1d64b0024c7df316d to your computer and use it in GitHub Desktop.
#include <boost/process.hpp>
#include <iostream>
#include <thread>
namespace bp = boost::process;
using namespace std;
void parent()
{
cout << "[P] Started" << endl;
boost::asio::io_service ios;
bp::async_pipe pipe(ios);
int exit_code = 0;
bp::child child(bp::args = {"./vtest", "child"},
bp::std_in < pipe,
bp::std_out > stdout,
bp::std_err > stderr,
bp::on_exit([&](int exit, std::error_code) {exit_code = exit;}),
ios);
thread io_thread([&ios]{ios.run();});
this_thread::sleep_for(chrono::seconds(1));
cout << "[P] pipe is_open: " << pipe.is_open() << endl;
cout << "[P] Closing pipe..." << endl;
pipe.async_close();
this_thread::sleep_for(chrono::seconds(1));
cout << "[P] pipe is_open: " << pipe.is_open() << endl;
io_thread.join();
cout << "[P] Terminating" << endl;
}
void child()
{
cout << "[C] Started" << endl;
// Block until stdin is closed...
char c;
while (cin >> c);
cout << "[C] Terminating" << endl;
}
int main(int argc, char* argv[])
{
if (argc < 2 || string(argv[1]) != "child")
parent();
else
child();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment