• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #include <boost/process.hpp>
6 #include <boost/program_options.hpp>
7 
8 #include <vector>
9 #include <string>
10 
11 #include <iostream>
12 #include <cstdint>
13 
14 #include <fstream>
15 
16 #include <chrono>
17 
18 
main(int argc,char * argv[])19 int main(int argc, char *argv[])
20 {
21     using namespace std;
22     using namespace boost::program_options;
23     using namespace boost::process;
24 
25     bool launch_detached = false;
26     bool launch_attached = false;
27 
28     options_description desc;
29     desc.add_options()
30          ("launch-detached", bool_switch(&launch_detached))
31          ("launch-attached", bool_switch(&launch_attached))
32          ;
33 
34     variables_map vm;
35     command_line_parser parser(argc, argv);
36     store(parser.options(desc).allow_unregistered().run(), vm);
37     notify(vm);
38 
39     child c1;
40     child c2;
41 
42     std::error_code ec;
43 
44     if (launch_attached)
45     {
46         c1 =     child(argv[0], ec,  std_out > null, std_err > null, std_in < null);
47         if (ec)
48         {
49             cout << -1 << endl;
50             cerr << ec.message() << endl;
51             return 1;
52         }
53 
54         cout << c1.id() << endl;
55     }
56     else
57         cout << -1 << endl;
58 
59     if (launch_detached)
60     {
61         group g;
62 
63         c2 = child(argv[0], ec, g, std_out > null, std_err > null, std_in < null);
64         if (ec)
65         {
66             cout << -1 << endl;
67             cerr << ec.message() << endl;
68             return 1;
69         }
70         else
71             cout << c2.id() << endl;
72 
73         g.detach();
74     }
75     else
76         cout << -1 << endl;
77 
78 
79     this_thread::sleep_for(chrono::seconds(10));
80 
81 
82     return 0;
83 }
84