Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <boost/thread.hpp>
  2. #include <boost/process.hpp>
  3. #include <iostream>
  4.  
  5. namespace bp = boost::process;
  6.  
  7. using namespace std;
  8.  
  9. void parent()
  10. {
  11. cout << "[P] Started" << endl;
  12.  
  13. boost::asio::io_service ios;
  14. bp::async_pipe in(ios);
  15. int exit_code = 0;
  16.  
  17. bp::child child(bp::args = {"./vtest", "child"},
  18. bp::std_in < in,
  19. bp::std_out > stdout,
  20. bp::std_err > stderr,
  21. bp::on_exit([&](int exit, std::error_code) {exit_code = exit;}),
  22. ios);
  23.  
  24. boost::thread io_thread([&ios]{ios.run();});
  25. try
  26. {
  27. io_thread.join();
  28. }
  29. catch (boost::thread_interrupted&)
  30. {
  31. boost::this_thread::disable_interruption di;
  32. cout << "[P] Received interruption request, closing child's stdin" << endl;
  33.  
  34. // Close stdin of child
  35. in.async_close();
  36. io_thread.join();
  37. }
  38.  
  39. cout << "[P] Terminating" << endl;
  40. }
  41.  
  42. void child()
  43. {
  44. cout << "[C] Started" << endl;
  45.  
  46. // Block until stdin is closed...
  47. char c;
  48. while (cin >> c);
  49.  
  50. cout << "[C] Terminating" << endl;
  51. }
  52.  
  53. int main(int argc, char* argv[])
  54. {
  55. if (argc < 2 || string(argv[1]) != "child")
  56. {
  57. boost::thread thread(parent);
  58. boost::this_thread::sleep_for(boost::chrono::seconds(1));
  59. cout << "[P] Interrupting..." << endl;
  60. thread.interrupt();
  61. thread.join();
  62. }
  63. else
  64. {
  65. child();
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement