sub_launcher.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2015 Klemens D. Morgenstern
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include <boost/process.hpp>
  5. #include <boost/program_options.hpp>
  6. #include <vector>
  7. #include <string>
  8. #include <iostream>
  9. #include <cstdint>
  10. #include <fstream>
  11. #include <chrono>
  12. int main(int argc, char *argv[])
  13. {
  14. using namespace std;
  15. using namespace boost::program_options;
  16. using namespace boost::process;
  17. bool launch_detached = false;
  18. bool launch_attached = false;
  19. options_description desc;
  20. desc.add_options()
  21. ("launch-detached", bool_switch(&launch_detached))
  22. ("launch-attached", bool_switch(&launch_attached))
  23. ;
  24. variables_map vm;
  25. command_line_parser parser(argc, argv);
  26. store(parser.options(desc).allow_unregistered().run(), vm);
  27. notify(vm);
  28. child c1;
  29. child c2;
  30. std::error_code ec;
  31. if (launch_attached)
  32. {
  33. c1 = child(argv[0], ec, std_out > null, std_err > null, std_in < null);
  34. if (ec)
  35. {
  36. cout << -1 << endl;
  37. cerr << ec.message() << endl;
  38. return 1;
  39. }
  40. cout << c1.id() << endl;
  41. }
  42. else
  43. cout << -1 << endl;
  44. if (launch_detached)
  45. {
  46. group g;
  47. c2 = child(argv[0], ec, g, std_out > null, std_err > null, std_in < null);
  48. if (ec)
  49. {
  50. cout << -1 << endl;
  51. cerr << ec.message() << endl;
  52. return 1;
  53. }
  54. else
  55. cout << c2.id() << endl;
  56. g.detach();
  57. }
  58. else
  59. cout << -1 << endl;
  60. this_thread::sleep_for(chrono::seconds(10));
  61. return 0;
  62. }