• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Tutorial</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../process.html" title="Chapter 29. Boost.Process">
10<link rel="prev" href="concepts.html" title="Concepts">
11<link rel="next" href="design.html" title="Design Rationale">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="concepts.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../process.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="design.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="boost_process.tutorial"></a><a class="link" href="tutorial.html" title="Tutorial">Tutorial</a>
29</h2></div></div></div>
30<div class="toc"><dl class="toc">
31<dt><span class="section"><a href="tutorial.html#boost_process.tutorial.starting_a_process">Starting a
32      process</a></span></dt>
33<dt><span class="section"><a href="tutorial.html#boost_process.tutorial.launch_mode">Launch functions</a></span></dt>
34<dt><span class="section"><a href="tutorial.html#boost_process.tutorial.error_handling">Error</a></span></dt>
35<dt><span class="section"><a href="tutorial.html#boost_process.tutorial.io">Synchronous I/O</a></span></dt>
36<dt><span class="section"><a href="tutorial.html#boost_process.tutorial.async_io">Asynchronous I/O</a></span></dt>
37<dt><span class="section"><a href="tutorial.html#boost_process.tutorial.group">Groups</a></span></dt>
38<dt><span class="section"><a href="tutorial.html#boost_process.tutorial.env">Environment</a></span></dt>
39</dl></div>
40<p>
41      In this section we will go step by step through the different features of boost.process.
42      For a full description see the <a class="link" href="../process/reference.html" title="Reference">reference</a>
43      and the <a class="link" href="concepts.html" title="Concepts">concepts</a> sections.
44    </p>
45<div class="section">
46<div class="titlepage"><div><div><h3 class="title">
47<a name="boost_process.tutorial.starting_a_process"></a><a class="link" href="tutorial.html#boost_process.tutorial.starting_a_process" title="Starting a process">Starting a
48      process</a>
49</h3></div></div></div>
50<p>
51        We want to start a process, so let's start with a simple process. We will
52        invoke the gcc compiler to compile a simple program.
53      </p>
54<p>
55        With the standard library this looks like this.
56      </p>
57<p>
58</p>
59<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <a href="http://en.cppreference.com/w/cpp/utility/program/system" target="_top">std::system</a><span class="special">(</span><span class="string">"g++ main.cpp"</span><span class="special">);</span>
60</pre>
61<p>
62      </p>
63<p>
64        Which we can write exactly like this in boost.process.
65      </p>
66<p>
67</p>
68<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">bp</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">process</span><span class="special">;</span> <span class="comment">//we will assume this for all further examples</span>
69<span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">bp::system</a></code><span class="special">(</span><span class="string">"g++ main.cpp"</span><span class="special">);</span>
70</pre>
71<p>
72      </p>
73<p>
74        If a single string is given (or the explicit form <code class="computeroutput">bp::cmd</code>),
75        it will be interpreted as a command line. That will cause the execution function
76        to search the <code class="computeroutput"><span class="identifier">PATH</span></code> variable
77        to find the executable. The alternative is the <code class="computeroutput"><span class="identifier">exe</span><span class="special">-</span><span class="identifier">args</span></code> style,
78        where the first string will be interpreted as a filename (including the path),
79        and the rest as arguments passed to said function.
80      </p>
81<div class="note"><table border="0" summary="Note">
82<tr>
83<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
84<th align="left">Note</th>
85</tr>
86<tr><td align="left" valign="top"><p>
87          For more details on the <code class="computeroutput"><span class="identifier">cmd</span></code>/<code class="computeroutput"><span class="identifier">exe</span><span class="special">-</span><span class="identifier">args</span></code> style look <a class="link" href="design.html#boost_process.design.arg_cmd_style" title="Arguments/Command Style">here</a>.
88        </p></td></tr>
89</table></div>
90<p>
91        So as a first step, we'll use the <code class="computeroutput"><span class="identifier">exe</span><span class="special">-</span><span class="identifier">args</span></code> style.
92      </p>
93<p>
94</p>
95<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">bp::system</a></code><span class="special">(</span><span class="string">"/usr/bin/g++"</span><span class="special">,</span> <span class="string">"main.cpp"</span><span class="special">);</span>
96</pre>
97<p>
98      </p>
99<p>
100        With that syntax we still have "g++" hard-coded, so let's assume
101        we get the string from an external source as <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">path</span></code>,
102        we can do this too.
103      </p>
104<p>
105</p>
106<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">path</span> <span class="identifier">p</span> <span class="special">=</span> <span class="string">"/usr/bin/g++"</span><span class="special">;</span> <span class="comment">//or get it from somewhere else.</span>
107<span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">bp::system</a></code><span class="special">(</span><span class="identifier">p</span><span class="special">,</span> <span class="string">"main.cpp"</span><span class="special">);</span>
108</pre>
109<p>
110      </p>
111<p>
112        Now we might want to find the <code class="computeroutput"><span class="identifier">g</span><span class="special">++</span></code> executable in the <code class="computeroutput"><span class="identifier">PATH</span></code>-variable,
113        as the <code class="computeroutput"><span class="identifier">cmd</span></code> syntax would do.
114        <code class="computeroutput"><span class="identifier">Boost</span><span class="special">.</span><span class="identifier">process</span></code> provides a function to this end:
115        <code class="computeroutput"><a class="link" href="../boost/process/search_path.html" title="Function search_path">bp::search_path</a></code>.
116      </p>
117<p>
118</p>
119<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">path</span> <span class="identifier">p</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="../boost/process/search_path.html" title="Function search_path">bp::search_path</a></code><span class="special">(</span><span class="string">"g++"</span><span class="special">);</span> <span class="comment">//or get it from somewhere else.</span>
120<span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">bp::system</a></code><span class="special">(</span><span class="identifier">p</span><span class="special">,</span> <span class="string">"main.cpp"</span><span class="special">);</span>
121</pre>
122<p>
123      </p>
124<div class="note"><table border="0" summary="Note">
125<tr>
126<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
127<th align="left">Note</th>
128</tr>
129<tr><td align="left" valign="top"><p>
130          <code class="computeroutput"><a class="link" href="../boost/process/search_path.html" title="Function search_path">search_path</a></code>
131          will search for any executable with that name. This also includes to add
132          a file suffix on windows, such as <code class="computeroutput"><span class="special">.</span><span class="identifier">exe</span></code> or <code class="computeroutput"><span class="special">.</span><span class="identifier">bat</span></code>.
133        </p></td></tr>
134</table></div>
135</div>
136<div class="section">
137<div class="titlepage"><div><div><h3 class="title">
138<a name="boost_process.tutorial.launch_mode"></a><a class="link" href="tutorial.html#boost_process.tutorial.launch_mode" title="Launch functions">Launch functions</a>
139</h3></div></div></div>
140<p>
141        Given that our example used the <code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">system</a></code>
142        function, our program will wait until the child process is completed. This
143        may be unwanted, especially since compiling can take a while.
144      </p>
145<p>
146        In order to avoid that, boost.process provides several ways to launch a process.
147        Besides the already mentioned <code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">system</a></code>
148        function and its asynchronous version <code class="computeroutput"><a class="link" href="../boost/process/async_system.html" title="Function template async_system">async_system</a></code>,
149        we can also use the <code class="computeroutput"><a class="link" href="../boost/process/spawn.html" title="Function template spawn">spawn</a></code>
150        function or the <code class="computeroutput"><a class="link" href="../boost/process/child.html" title="Class child">child</a></code>
151        class.
152      </p>
153<p>
154        The <code class="computeroutput"><a class="link" href="../boost/process/spawn.html" title="Function template spawn">spawn</a></code> function
155        launches a process and immediately detaches it, so no handle will be returned
156        and the process will be ignored. This is not what we need for compiling,
157        but maybe we want to entertain the user, while compiling:
158      </p>
159<p>
160</p>
161<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/process/spawn.html" title="Function template spawn">bp::spawn</a></code><span class="special">(</span><code class="computeroutput"><a class="link" href="../boost/process/search_path.html" title="Function search_path">bp::search_path</a></code><span class="special">(</span><span class="string">"chrome"</span><span class="special">),</span> <a href="../www.boost.org" target="_top">"www.boost.org"</a><span class="special">);</span>
162</pre>
163<p>
164      </p>
165<p>
166        Now for the more sensible approach for compiling: a non-blocking execution.
167        To implement that, we directly call the constructor of <code class="computeroutput"><a class="link" href="../boost/process/child.html" title="Class child">child</a></code>.
168      </p>
169<p>
170</p>
171<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/process/child.html" title="Class child">bp::child</a></code> <span class="identifier">c</span><span class="special">(</span><code class="computeroutput"><a class="link" href="../boost/process/search_path.html" title="Function search_path">bp::search_path</a></code><span class="special">(</span><span class="string">"g++"</span><span class="special">),</span> <span class="string">"main.cpp"</span><span class="special">);</span>
172
173<span class="keyword">while</span> <span class="special">(</span><span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675143440-bb">running</a></code><span class="special">())</span>
174    <span class="identifier">do_some_stuff</span><span class="special">();</span>
175
176<span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675140144-bb">wait</a></code><span class="special">();</span> <span class="comment">//wait for the process to exit   </span>
177<span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675147856-bb">exit_code</a></code><span class="special">();</span>
178</pre>
179<p>
180      </p>
181<p>
182        So we launch the process, by calling the child constructor. Then we check
183        and do other things while the process is running and afterwards get the exit
184        code. The call to <code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675140144-bb">wait</a></code>
185        is necessary, to obtain it and tell the operating system, that no one is
186        waiting for the process anymore.
187      </p>
188<div class="note"><table border="0" summary="Note">
189<tr>
190<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
191<th align="left">Note</th>
192</tr>
193<tr><td align="left" valign="top"><p>
194          You can also wait for a time span or until a time point with <code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675136848-bb">wait_for</a></code> and <code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675130464-bb">wait_until</a></code>.
195        </p></td></tr>
196</table></div>
197<div class="warning"><table border="0" summary="Warning">
198<tr>
199<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../doc/src/images/warning.png"></td>
200<th align="left">Warning</th>
201</tr>
202<tr><td align="left" valign="top"><p>
203          If you don't call wait on a child object, it will be terminated on destruction.
204          This can be avoided by calling <code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675152832-bb">detach</a></code>
205          beforehand
206        </p></td></tr>
207</table></div>
208</div>
209<div class="section">
210<div class="titlepage"><div><div><h3 class="title">
211<a name="boost_process.tutorial.error_handling"></a><a class="link" href="tutorial.html#boost_process.tutorial.error_handling" title="Error">Error</a>
212</h3></div></div></div>
213<p>
214        Until now, we have assumed that everything works out, but it is not impossible,
215        that "g++" is not present. That will cause the launch of the process
216        to fail. The default behaviour of all functions is to throw a <a href="http://en.cppreference.com/w/cpp/error/system_error" target="_top">std::system_error</a>
217        on failure. As with many other functions in this library, passing an <a href="http://en.cppreference.com/w/cpp/error/error_code" target="_top">std::error_code</a>
218        will change the behaviour, so that instead of throwing an exception, the
219        error will be assigned to the error code.
220      </p>
221<p>
222</p>
223<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">;</span>
224<code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">bp::system</a></code><span class="special">(</span><span class="string">"g++ main.cpp"</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span>
225</pre>
226<p>
227      </p>
228</div>
229<div class="section">
230<div class="titlepage"><div><div><h3 class="title">
231<a name="boost_process.tutorial.io"></a><a class="link" href="tutorial.html#boost_process.tutorial.io" title="Synchronous I/O">Synchronous I/O</a>
232</h3></div></div></div>
233<p>
234        In the examples given above, we have only started a program, but did not
235        consider the output. The default depends on the system, but usually this
236        will just write it to the same output as the launching process. If this shall
237        be guaranteed, the streams can be explicitly forwarded like this.
238      </p>
239<p>
240</p>
241<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">bp::system</a></code><span class="special">(</span><span class="string">"g++ main.cpp"</span><span class="special">,</span> <code class="computeroutput"><a class="link" href="../boost/process/std_out.html" title="Global std_out">bp::std_out</a></code> <span class="special">&gt;</span> <span class="identifier">stdout</span><span class="special">,</span> <code class="computeroutput"><a class="link" href="../boost/process/std_err.html" title="Global std_err">bp::std_err</a></code> <span class="special">&gt;</span> <span class="identifier">stderr</span><span class="special">,</span> <code class="computeroutput"><a class="link" href="../boost/process/std_in.html" title="Global std_in">bp::std_in</a></code> <span class="special">&lt;</span> <span class="identifier">stdin</span><span class="special">);</span>
242</pre>
243<p>
244      </p>
245<p>
246        Now for the first example, we might want to just ignore the output, which
247        can be done by redirecting it to the null-device. This can be achieved this
248        way:
249      </p>
250<p>
251</p>
252<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">bp::system</a></code><span class="special">(</span><span class="string">"g++ main.cpp"</span><span class="special">,</span> <code class="computeroutput"><a class="link" href="../boost/process/std_out.html" title="Global std_out">bp::std_out</a></code> <span class="special">&gt;</span> <code class="computeroutput"><a class="link" href="../boost/process/null.html" title="Global null">bp::null</a></code><span class="special">);</span>
253</pre>
254<p>
255      </p>
256<p>
257        Alternatively we can also easily redirect the output to a file:
258      </p>
259<p>
260</p>
261<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">bp::system</a></code><span class="special">(</span><span class="string">"g++ main.cpp"</span><span class="special">,</span> <code class="computeroutput"><a class="link" href="../boost/process/std_out.html" title="Global std_out">bp::std_out</a></code> <span class="special">&gt;</span> <span class="string">"gcc_out.log"</span><span class="special">);</span>
262</pre>
263<p>
264      </p>
265<p>
266        Now, let's take a more visual example for reading data. <a href="http://pubs.opengroup.org/onlinepubs/009696699/utilities/nm.html" target="_top">nm</a>
267        is a tool on posix, which reads the outline, i.e. a list of all entry points,
268        of a binary. Every entry point will be put into a single line, and we will
269        use a pipe to read it. At the end an empty line is appended, which we use
270        as the indication to stop reading. Boost.process provides the pipestream
271        (<code class="computeroutput"><a class="link" href="../process/reference.html#boost.process.ipstream">ipstream</a></code>, <code class="computeroutput"><a class="link" href="../process/reference.html#boost.process.opstream">opstream</a></code>, <code class="computeroutput"><a class="link" href="../process/reference.html#boost.process.pstream">pstream</a></code>)
272        to wrap around the <code class="computeroutput"><a class="link" href="../process/reference.html#boost.process.pipe">pipe</a></code>
273        and provide an implementation of the <a href="http://en.cppreference.com/w/cpp/io/basic_istream" target="_top">std::istream</a>,
274        <a href="http://en.cppreference.com/w/cpp/io/basic_ostream" target="_top">std::ostream</a>
275        and <a href="http://en.cppreference.com/w/cpp/io/basic_iostream" target="_top">std::iostream</a>
276        interface.
277      </p>
278<p>
279</p>
280<pre class="programlisting"><a href="http://en.cppreference.com/w/cpp/container/vector" target="_top">std::vector</a><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">read_outline</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> <span class="identifier">file</span><span class="special">)</span>
281<span class="special">{</span>
282    <code class="computeroutput"><a class="link" href="../process/reference.html#boost.process.ipstream">bp::ipstream</a></code> <span class="identifier">is</span><span class="special">;</span> <span class="comment">//reading pipe-stream</span>
283    <code class="computeroutput"><a class="link" href="../boost/process/child.html" title="Class child">bp::child</a></code> <span class="identifier">c</span><span class="special">(</span><code class="computeroutput"><a class="link" href="../boost/process/search_path.html" title="Function search_path">bp::search_path</a></code><span class="special">(</span><span class="string">"nm"</span><span class="special">),</span> <span class="identifier">file</span><span class="special">,</span> <code class="computeroutput"><a class="link" href="../boost/process/std_out.html" title="Global std_out">bp::std_out</a></code> <span class="special">&gt;</span> <span class="identifier">is</span><span class="special">);</span>
284
285    <a href="http://en.cppreference.com/w/cpp/container/vector" target="_top">std::vector</a><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">data</span><span class="special">;</span>
286    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">line</span><span class="special">;</span>
287
288    <span class="keyword">while</span> <span class="special">(</span><span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675143440-bb">running</a></code><span class="special">()</span> <span class="special">&amp;&amp;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">getline</span><span class="special">(</span><span class="identifier">is</span><span class="special">,</span> <span class="identifier">line</span><span class="special">)</span> <span class="special">&amp;&amp;</span> <span class="special">!</span><span class="identifier">line</span><span class="special">.</span><span class="identifier">empty</span><span class="special">())</span>
289        <span class="identifier">data</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">line</span><span class="special">);</span>
290
291    <span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675140144-bb">wait</a></code><span class="special">();</span>
292
293    <span class="keyword">return</span> <span class="identifier">data</span><span class="special">;</span>
294<span class="special">}</span>
295</pre>
296<p>
297      </p>
298<p>
299        What this does is redirect the <code class="computeroutput"><span class="identifier">stdout</span></code>
300        of the process into a pipe and we read this synchronously.
301      </p>
302<div class="note"><table border="0" summary="Note">
303<tr>
304<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
305<th align="left">Note</th>
306</tr>
307<tr><td align="left" valign="top"><p>
308          You can do the same thing with <code class="computeroutput"><a class="link" href="../boost/process/std_err.html" title="Global std_err">std_err</a></code>.
309        </p></td></tr>
310</table></div>
311<p>
312        Now we get the name from <code class="computeroutput"><span class="identifier">nm</span></code>
313        and we might want to demangle it, so we use input and output. <code class="computeroutput"><span class="identifier">nm</span></code> has a demangle option, but for the sake
314        of the example, we'll use <a href="https://sourceware.org/binutils/docs/binutils/c_002b_002bfilt.html" target="_top">c++filt</a>
315        for this.
316      </p>
317<p>
318</p>
319<pre class="programlisting"><code class="computeroutput"><a class="link" href="../process/reference.html#boost.process.opstream">bp::opstream</a></code> <span class="identifier">in</span><span class="special">;</span>
320<code class="computeroutput"><a class="link" href="../process/reference.html#boost.process.ipstream">bp::ipstream</a></code> <span class="identifier">out</span><span class="special">;</span>
321
322<code class="computeroutput"><a class="link" href="../boost/process/child.html" title="Class child">bp::child</a></code> <span class="identifier">c</span><span class="special">(</span><span class="string">"c++filt"</span><span class="special">,</span> <span class="identifier">std_out</span> <span class="special">&gt;</span> <span class="identifier">out</span><span class="special">,</span> <span class="identifier">std_in</span> <span class="special">&lt;</span> <span class="identifier">in</span><span class="special">);</span>
323
324<span class="identifier">in</span> <span class="special">&lt;&lt;</span> <span class="string">"_ZN5boost7process8tutorialE"</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
325<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">value</span><span class="special">;</span>
326<span class="identifier">out</span> <span class="special">&gt;&gt;</span> <span class="identifier">value</span><span class="special">;</span>
327
328<span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675116704-bb">terminate</a></code><span class="special">();</span>
329</pre>
330<p>
331      </p>
332<p>
333        Now you might want to forward output from one process to another processes
334        input.
335      </p>
336<p>
337</p>
338<pre class="programlisting"><a href="http://en.cppreference.com/w/cpp/container/vector" target="_top">std::vector</a><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">read_demangled_outline</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> <span class="identifier">file</span><span class="special">)</span>
339<span class="special">{</span>
340    <code class="computeroutput"><a class="link" href="../process/reference.html#boost.process.pipe">bp::pipe</a></code> <span class="identifier">p</span><span class="special">;</span>
341    <code class="computeroutput"><a class="link" href="../process/reference.html#boost.process.ipstream">bp::ipstream</a></code> <span class="identifier">is</span><span class="special">;</span>
342
343    <a href="http://en.cppreference.com/w/cpp/container/vector" target="_top">std::vector</a><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">outline</span><span class="special">;</span>
344
345    <span class="comment">//we just use the same pipe, so the output of nm is directly passed as input to c++filt</span>
346    <code class="computeroutput"><a class="link" href="../boost/process/child.html" title="Class child">bp::child</a></code> <span class="identifier">nm</span><span class="special">(</span><code class="computeroutput"><a class="link" href="../boost/process/search_path.html" title="Function search_path">bp::search_path</a></code><span class="special">(</span><span class="string">"nm"</span><span class="special">),</span> <span class="identifier">file</span><span class="special">,</span>  <code class="computeroutput"><a class="link" href="../boost/process/std_out.html" title="Global std_out">bp::std_out</a></code> <span class="special">&gt;</span> <span class="identifier">p</span><span class="special">);</span>
347    <code class="computeroutput"><a class="link" href="../boost/process/child.html" title="Class child">bp::child</a></code> <span class="identifier">filt</span><span class="special">(</span><code class="computeroutput"><a class="link" href="../boost/process/search_path.html" title="Function search_path">bp::search_path</a></code><span class="special">(</span><span class="string">"c++filt"</span><span class="special">),</span> <code class="computeroutput"><a class="link" href="../boost/process/std_in.html" title="Global std_in">bp::std_in</a></code> <span class="special">&lt;</span> <span class="identifier">p</span><span class="special">,</span> <code class="computeroutput"><a class="link" href="../boost/process/std_out.html" title="Global std_out">bp::std_out</a></code> <span class="special">&gt;</span> <span class="identifier">is</span><span class="special">);</span>
348
349    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">line</span><span class="special">;</span>
350    <span class="keyword">while</span> <span class="special">(</span><span class="identifier">filt</span><span class="special">.</span><span class="identifier">running</span><span class="special">()</span> <span class="special">&amp;&amp;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">getline</span><span class="special">(</span><span class="identifier">is</span><span class="special">,</span> <span class="identifier">line</span><span class="special">))</span> <span class="comment">//when nm finished the pipe closes and c++filt exits</span>
351        <span class="identifier">outline</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">line</span><span class="special">);</span>
352
353    <span class="identifier">nm</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675140144-bb">wait</a></code><span class="special">();</span>
354    <span class="identifier">filt</span><span class="special">.</span><span class="identifier">wait</span><span class="special">();</span>
355<span class="special">}</span>
356</pre>
357<p>
358      </p>
359<p>
360        This forwards the data from <code class="computeroutput"><span class="identifier">nm</span></code>
361        to <code class="computeroutput"><span class="identifier">c</span><span class="special">++</span><span class="identifier">filt</span></code> without your process needing to do
362        anything.
363      </p>
364</div>
365<div class="section">
366<div class="titlepage"><div><div><h3 class="title">
367<a name="boost_process.tutorial.async_io"></a><a class="link" href="tutorial.html#boost_process.tutorial.async_io" title="Asynchronous I/O">Asynchronous I/O</a>
368</h3></div></div></div>
369<p>
370        Boost.process allows the usage of boost.asio to implement asynchronous I/O.
371        If you are familiar with <a href="http://www.boost.org/doc/libs/release/libs/asio/" target="_top">boost.asio</a>
372        (which we highly recommend), you can use <code class="computeroutput"><a class="link" href="../boost/process/async_pipe.html" title="Class async_pipe">async_pipe</a></code>
373        which is implemented as an I/O-Object and can be used like <code class="computeroutput"><a class="link" href="../process/reference.html#boost.process.pipe">pipe</a></code>
374        as shown above.
375      </p>
376<p>
377        Now we get back to our compiling example. For <code class="computeroutput"><span class="identifier">nm</span></code>
378        we might analyze the output line by line, but the compiler output will just
379        be put into one large buffer.
380      </p>
381<p>
382        With <a href="http://www.boost.org/doc/libs/release/libs/asio/" target="_top">boost.asio</a>
383        this is what it looks like.
384      </p>
385<p>
386</p>
387<pre class="programlisting"><a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service.html" target="_top">boost::asio::io_service</a> <span class="identifier">ios</span><span class="special">;</span>
388<a href="http://en.cppreference.com/w/cpp/container/vector" target="_top">std::vector</a><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span> <span class="identifier">buf</span><span class="special">(</span><span class="number">4096</span><span class="special">);</span>
389
390<code class="computeroutput"><a class="link" href="../boost/process/async_pipe.html" title="Class async_pipe">bp::async_pipe</a></code> <span class="identifier">ap</span><span class="special">(</span><span class="identifier">ios</span><span class="special">);</span>
391
392<code class="computeroutput"><a class="link" href="../boost/process/child.html" title="Class child">bp::child</a></code> <span class="identifier">c</span><span class="special">(</span><code class="computeroutput"><a class="link" href="../boost/process/search_path.html" title="Function search_path">bp::search_path</a></code><span class="special">(</span><span class="string">"g++"</span><span class="special">),</span> <span class="string">"main.cpp"</span><span class="special">,</span> <code class="computeroutput"><a class="link" href="../boost/process/std_out.html" title="Global std_out">bp::std_out</a></code> <span class="special">&gt;</span> <span class="identifier">ap</span><span class="special">);</span>
393
394<a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/async_read.html" target="_top">boost::asio::async_read</a><span class="special">(</span><span class="identifier">ap</span><span class="special">,</span> <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/buffer.html" target="_top">boost::asio::buffer</a><span class="special">(</span><span class="identifier">buf</span><span class="special">),</span>
395                <span class="special">[](</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="special">&amp;</span><span class="identifier">ec</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span><span class="special">){});</span>
396
397<span class="identifier">ios</span><span class="special">.</span><span class="identifier">run</span><span class="special">();</span>
398<span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">exit_code</span><span class="special">();</span>
399</pre>
400<p>
401      </p>
402<p>
403        To make it easier, boost.process provides a simpler interface for that, so
404        that the buffer can be passed directly, provided we also pass a reference
405        to an <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service.html" target="_top">boost::asio::io_service</a>.
406      </p>
407<p>
408</p>
409<pre class="programlisting"><a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service.html" target="_top">boost::asio::io_service</a> <span class="identifier">ios</span><span class="special">;</span>
410<a href="http://en.cppreference.com/w/cpp/container/vector" target="_top">std::vector</a><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span> <span class="identifier">buf</span><span class="special">(</span><span class="number">4096</span><span class="special">);</span>
411
412<code class="computeroutput"><a class="link" href="../boost/process/child.html" title="Class child">bp::child</a></code> <span class="identifier">c</span><span class="special">(</span><code class="computeroutput"><a class="link" href="../boost/process/search_path.html" title="Function search_path">bp::search_path</a></code><span class="special">(</span><span class="string">"g++"</span><span class="special">),</span> <span class="string">"main.cpp"</span><span class="special">,</span> <code class="computeroutput"><a class="link" href="../boost/process/std_out.html" title="Global std_out">bp::std_out</a></code> <span class="special">&gt;</span> <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/buffer.html" target="_top">boost::asio::buffer</a><span class="special">(</span><span class="identifier">buf</span><span class="special">),</span> <span class="identifier">ios</span><span class="special">);</span>
413
414<span class="identifier">ios</span><span class="special">.</span><span class="identifier">run</span><span class="special">();</span>
415<span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">exit_code</span><span class="special">();</span>
416</pre>
417<p>
418      </p>
419<div class="note"><table border="0" summary="Note">
420<tr>
421<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
422<th align="left">Note</th>
423</tr>
424<tr><td align="left" valign="top"><p>
425          Passing an instance of <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service.html" target="_top">boost::asio::io_service</a>
426          to the launching function automatically cause it to wait asynchronously
427          for the exit, so no call of <code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675140144-bb">wait</a></code>
428          is needed.
429        </p></td></tr>
430</table></div>
431<p>
432        To make it even easier, you can use <a href="http://en.cppreference.com/w/cpp/thread/future" target="_top">std::future</a>
433        for asynchronous operations (you will still need to pass a reference to a
434        <a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service.html" target="_top">boost::asio::io_service</a>)
435        to the launching function, unless you use <code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">bp::system</a></code>
436        or <code class="computeroutput"><a class="link" href="../boost/process/async_system.html" title="Function template async_system">bp::async_system</a></code>.
437      </p>
438<p>
439        Now we will revisit our first example and read the compiler output asynchronously:
440      </p>
441<p>
442</p>
443<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><a href="http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/io_service.html" target="_top">boost::asio::io_service</a> <span class="identifier">ios</span><span class="special">;</span>
444
445<span class="identifier">std</span><span class="special">::</span><span class="identifier">future</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">data</span><span class="special">;</span>
446
447<span class="identifier">child</span> <span class="identifier">c</span><span class="special">(</span><span class="string">"g++"</span><span class="special">,</span> <span class="string">"main.cpp"</span><span class="special">,</span> <span class="comment">//set the input</span>
448        <code class="computeroutput"><a class="link" href="../boost/process/std_in.html" title="Global std_in">bp::std_in</a></code><span class="special">.</span><span class="identifier">close</span><span class="special">(),</span>
449        <code class="computeroutput"><a class="link" href="../boost/process/std_out.html" title="Global std_out">bp::std_out</a></code> <span class="special">&gt;</span> <code class="computeroutput"><a class="link" href="../boost/process/null.html" title="Global null">bp::null</a></code><span class="special">,</span> <span class="comment">//so it can be written without anything</span>
450        <code class="computeroutput"><a class="link" href="../boost/process/std_err.html" title="Global std_err">bp::std_err</a></code> <span class="special">&gt;</span> <span class="identifier">data</span><span class="special">,</span>
451        <span class="identifier">ios</span><span class="special">);</span>
452
453
454<span class="identifier">ios</span><span class="special">.</span><span class="identifier">run</span><span class="special">();</span> <span class="comment">//this will actually block until the compiler is finished</span>
455
456<span class="keyword">auto</span> <span class="identifier">err</span> <span class="special">=</span>  <span class="identifier">data</span><span class="special">.</span><span class="identifier">get</span><span class="special">();</span>
457</pre>
458<p>
459      </p>
460</div>
461<div class="section">
462<div class="titlepage"><div><div><h3 class="title">
463<a name="boost_process.tutorial.group"></a><a class="link" href="tutorial.html#boost_process.tutorial.group" title="Groups">Groups</a>
464</h3></div></div></div>
465<p>
466        When launching several processes, they can be grouped together. This will
467        also apply for a child process, that launches other processes, if they do
468        not modify the group membership. E.g. if you call <code class="computeroutput"><span class="identifier">make</span></code>
469        which launches other processes and call terminate on it, it will not terminate
470        all the child processes of the child unless you use a group.
471      </p>
472<p>
473        The two main reasons to use groups are:
474      </p>
475<div class="orderedlist"><ol class="orderedlist" type="1">
476<li class="listitem">
477            Being able to terminate child processes of the child process
478          </li>
479<li class="listitem">
480            Grouping several processes into one, just so they can be terminated at
481            once
482          </li>
483</ol></div>
484<p>
485        If we have a program like <code class="computeroutput"><span class="identifier">make</span></code>,
486        which does launch its own child processes, a call of <code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675116704-bb">terminate</a></code>
487        might not suffice. I.e. if we have a makefile launching <code class="computeroutput"><span class="identifier">gcc</span></code>
488        and use the following code, the <code class="computeroutput"><span class="identifier">gcc</span></code>
489        process will still run afterwards:
490      </p>
491<p>
492</p>
493<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/process/child.html" title="Class child">bp::child</a></code> <span class="identifier">c</span><span class="special">(</span><span class="string">"make"</span><span class="special">);</span>
494<span class="keyword">if</span> <span class="special">(!</span><span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675136848-bb">wait_for</a></code><span class="special">(</span><a href="http://en.cppreference.com/w/cpp/chrono/duration" target="_top">std::chrono::seconds</a><span class="special">(</span><span class="number">10</span><span class="special">))</span> <span class="comment">//give it 10 seconds</span>
495    <span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675116704-bb">terminate</a></code><span class="special">();</span> <span class="comment">//then terminate</span>
496</pre>
497<p>
498      </p>
499<p>
500        So in order to also terminate <code class="computeroutput"><span class="identifier">gcc</span></code>
501        we can use a group.
502      </p>
503<p>
504</p>
505<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/process/group.html" title="Class group">bp::group</a></code> <span class="identifier">g</span><span class="special">;</span>
506<code class="computeroutput"><a class="link" href="../boost/process/child.html" title="Class child">bp::child</a></code> <span class="identifier">c</span><span class="special">(</span><span class="string">"make"</span><span class="special">,</span> <span class="identifier">g</span><span class="special">);</span>
507<span class="keyword">if</span> <span class="special">(!</span><span class="identifier">g</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/group.html#idm45243674604048-bb">wait_for</a></code><span class="special">(</span><a href="http://en.cppreference.com/w/cpp/chrono/duration" target="_top">std::chrono::seconds</a><span class="special">(</span><span class="number">10</span><span class="special">))</span>
508    <span class="identifier">g</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/group.html#idm45243674583872-bb">terminate</a></code><span class="special">();</span>
509
510<span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675140144-bb">wait</a></code><span class="special">();</span> <span class="comment">//to avoid a zombie process &amp; get the exit code</span>
511</pre>
512<p>
513      </p>
514<p>
515        Now given the example, we still call <code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45243675140144-bb">wait</a></code>
516        to avoid a zombie process. An easier solution for that might be to use <code class="computeroutput"><a class="link" href="../boost/process/spawn.html" title="Function template spawn">spawn</a></code>.
517      </p>
518<p>
519        To put two processes into one group, the following code suffices. Spawn already
520        launches a detached process (i.e. without a child-handle), but they can be
521        grouped, to that in the case of a problem, RAII is still a given.
522      </p>
523<p>
524</p>
525<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">f</span><span class="special">()</span>
526<span class="special">{</span>
527    <code class="computeroutput"><a class="link" href="../boost/process/group.html" title="Class group">bp::group</a></code> <span class="identifier">g</span><span class="special">;</span>
528    <code class="computeroutput"><a class="link" href="../boost/process/spawn.html" title="Function template spawn">bp::spawn</a></code><span class="special">(</span><span class="string">"foo"</span><span class="special">,</span> <span class="identifier">g</span><span class="special">);</span>
529    <code class="computeroutput"><a class="link" href="../boost/process/spawn.html" title="Function template spawn">bp::spawn</a></code><span class="special">(</span><span class="string">"bar"</span><span class="special">,</span> <span class="identifier">g</span><span class="special">);</span>
530
531    <span class="identifier">do_something</span><span class="special">();</span>
532
533    <span class="identifier">g</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/group.html#idm45243674607552-bb">wait</a></code><span class="special">();</span>
534<span class="special">};</span>
535</pre>
536<p>
537      </p>
538<p>
539        In the example, it will wait for both processes at the end of the function
540        unless an exception occurs. I.e. if an exception is thrown, the group will
541        be terminated.
542      </p>
543<p>
544        Please see the <code class="computeroutput"><a class="link" href="../process/reference.html#header.boost.process.group_hpp" title="Header &lt;boost/process/group.hpp&gt;">reference</a></code>
545        for more information.
546      </p>
547</div>
548<div class="section">
549<div class="titlepage"><div><div><h3 class="title">
550<a name="boost_process.tutorial.env"></a><a class="link" href="tutorial.html#boost_process.tutorial.env" title="Environment">Environment</a>
551</h3></div></div></div>
552<p>
553        This library provides access to the environment of the current process and
554        allows setting it for the child process.
555      </p>
556<p>
557</p>
558<pre class="programlisting"><span class="comment">//get a handle to the current environment</span>
559<span class="keyword">auto</span> <span class="identifier">env</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="../process/reference.html#boost.this_process.environment">boost::this_process::environment</a></code><span class="special">();</span>
560<span class="comment">//add a variable to the current environment</span>
561<span class="identifier">env</span><span class="special">[</span><span class="string">"VALUE_1"</span><span class="special">]</span> <span class="special">=</span> <span class="string">"foo"</span><span class="special">;</span>
562
563<span class="comment">//copy it into an environment separate to the one of this process</span>
564<code class="computeroutput"><a class="link" href="../boost/process/basic_environment.html" title="Class template basic_environment">bp::environment</a></code> <span class="identifier">env_</span> <span class="special">=</span> <span class="identifier">env</span><span class="special">;</span>
565<span class="comment">//append two values to a variable in the new env</span>
566<span class="identifier">env_</span><span class="special">[</span><span class="string">"VALUE_2"</span><span class="special">]</span> <span class="special">+=</span> <span class="special">{</span><span class="string">"bar1"</span><span class="special">,</span> <span class="string">"bar2"</span><span class="special">};</span>
567
568<span class="comment">//launch a process with `env_`</span>
569<code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">bp::system</a></code><span class="special">(</span><span class="string">"stuff"</span><span class="special">,</span> <span class="identifier">env_</span><span class="special">);</span>
570</pre>
571<p>
572      </p>
573<p>
574        A more convenient way to modify the environment for the child is the <code class="computeroutput"><a class="link" href="../boost/process/env.html" title="Global env">env</a></code> property, which can be used in
575        the example as following:
576      </p>
577<p>
578</p>
579<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/process/system.html" title="Function template system">bp::system</a></code><span class="special">(</span><span class="string">"stuff"</span><span class="special">,</span> <code class="computeroutput"><a class="link" href="../boost/process/env.html" title="Global env">bp::env</a></code><span class="special">[</span><span class="string">"VALUE_1"</span><span class="special">]=</span><span class="string">"foo"</span><span class="special">,</span> <code class="computeroutput"><a class="link" href="../boost/process/env.html" title="Global env">bp::env</a></code><span class="special">[</span><span class="string">"VALUE_2"</span><span class="special">]+={</span><span class="string">"bar1"</span><span class="special">,</span> <span class="string">"bar2"</span><span class="special">});</span>
580</pre>
581<p>
582      </p>
583<p>
584        Please see the <code class="computeroutput"><a class="link" href="../process/reference.html#header.boost.process.environment_hpp" title="Header &lt;boost/process/environment.hpp&gt;">reference</a></code>
585        for more information.
586      </p>
587</div>
588</div>
589<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
590<td align="left"></td>
591<td align="right"><div class="copyright-footer">Copyright © 2006-2012 Julio M. Merino Vidal, Ilya Sokolov,
592      Felipe Tanus, Jeff Flinn, Boris Schaeling<br>Copyright © 2016 Klemens D. Morgenstern<p>
593        Distributed under the Boost Software License, Version 1.0. (See accompanying
594        file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
595      </p>
596</div></td>
597</tr></table>
598<hr>
599<div class="spirit-nav">
600<a accesskey="p" href="concepts.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../process.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="design.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
601</div>
602</body>
603</html>
604