Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/thread.hpp>
- #include <boost/process.hpp>
- #include <iostream>
- namespace bp = boost::process;
- using namespace std;
- void parent()
- {
- cout << "[P] Started" << endl;
- boost::asio::io_service ios;
- bp::async_pipe in(ios);
- int exit_code = 0;
- bp::child child(bp::args = {"./vtest", "child"},
- bp::std_in < in,
- bp::std_out > stdout,
- bp::std_err > stderr,
- bp::on_exit([&](int exit, std::error_code) {exit_code = exit;}),
- ios);
- boost::thread io_thread([&ios]{ios.run();});
- try
- {
- io_thread.join();
- }
- catch (boost::thread_interrupted&)
- {
- boost::this_thread::disable_interruption di;
- cout << "[P] Received interruption request, closing child's stdin" << endl;
- // Close stdin of child
- in.async_close();
- 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")
- {
- boost::thread thread(parent);
- boost::this_thread::sleep_for(boost::chrono::seconds(1));
- cout << "[P] Interrupting..." << endl;
- thread.interrupt();
- thread.join();
- }
- else
- {
- child();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement