summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/process/test/sub_launcher.cpp
blob: c364901329a3301704eab4338cfb5ae9fd21b89e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2015 Klemens D. Morgenstern
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/process.hpp>
#include <boost/program_options.hpp>

#include <vector>
#include <string>

#include <iostream>
#include <cstdint>

#include <fstream>

#include <chrono>


int main(int argc, char *argv[])
{
    using namespace std;
    using namespace boost::program_options;
    using namespace boost::process;

    bool launch_detached = false;
    bool launch_attached = false;

    options_description desc;
    desc.add_options()
         ("launch-detached", bool_switch(&launch_detached))
         ("launch-attached", bool_switch(&launch_attached))
         ;

    variables_map vm;
    command_line_parser parser(argc, argv);
    store(parser.options(desc).allow_unregistered().run(), vm);
    notify(vm);

    child c1;
    child c2;

    std::error_code ec;

    if (launch_attached)
    {
        c1 =     child(argv[0], ec,  std_out > null, std_err > null, std_in < null);
        if (ec)
        {
            cout << -1 << endl;
            cerr << ec.message() << endl;
            return 1;
        }

        cout << c1.id() << endl;
    }
    else
        cout << -1 << endl;

    if (launch_detached)
    {
        group g;

        c2 = child(argv[0], ec, g, std_out > null, std_err > null, std_in < null);
        if (ec)
        {
            cout << -1 << endl;
            cerr << ec.message() << endl;
            return 1;
        }
        else
            cout << c2.id() << endl;

        g.detach();
    }
    else
        cout << -1 << endl;


    this_thread::sleep_for(chrono::seconds(10));


    return 0;
}