1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
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 30. Boost.Process">
10 <link rel="prev" href="concepts.html" title="Concepts">
11 <link rel="next" href="design.html" title="Design Rationale">
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>
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>
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>
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.
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
49 </h3></div></div></div>
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.
55 With the standard library this looks like this.
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>
64 Which we can write exactly like this in boost.process.
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>
74 If a single string (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.
81 <div class="note"><table border="0" summary="Note">
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>
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>
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.
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>
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>,
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>
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>.
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>
124 <div class="note"><table border="0" summary="Note">
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>
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>.
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>
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.
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>
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:
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>
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>.
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>
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#idm45170605253472-bb">running</a></code><span class="special">())</span>
174 <span class="identifier">do_some_stuff</span><span class="special">();</span>
176 <span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45170605250176-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#idm45170605257888-bb">exit_code</a></code><span class="special">();</span>
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#idm45170605250176-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.
188 <div class="note"><table border="0" summary="Note">
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>
193 <tr><td align="left" valign="top"><p>
194 You can also wait for a time span or a until a time point with <code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45170605246880-bb">wait_for</a></code> and <code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45170605240496-bb">wait_until</a></code>
197 <div class="warning"><table border="0" summary="Warning">
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>
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#idm45170605262864-bb">detach</a></code>
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>
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.
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>
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>
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.
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">></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">></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"><</span> <span class="identifier">stdin</span><span class="special">);</span>
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
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">></span> <code class="computeroutput"><a class="link" href="../boost/process/null.html" title="Global null">bp::null</a></code><span class="special">);</span>
257 Alternatively we can also easily redirect the output to a file:
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">></span> <span class="string">"gcc_out.log"</span><span class="special">);</span>
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>
280 <pre class="programlisting"><a href="http://en.cppreference.com/w/cpp/container/vector" target="_top">std::vector</a><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></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">&</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">></span> <span class="identifier">is</span><span class="special">);</span>
285 <a href="http://en.cppreference.com/w/cpp/container/vector" target="_top">std::vector</a><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></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>
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#idm45170605253472-bb">running</a></code><span class="special">()</span> <span class="special">&&</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">&&</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>
291 <span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45170605250176-bb">wait</a></code><span class="special">();</span>
293 <span class="keyword">return</span> <span class="identifier">data</span><span class="special">;</span>
294 <span class="special">}</span>
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.
302 <div class="note"><table border="0" summary="Note">
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>
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>
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>
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>
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">></span> <span class="identifier">out</span><span class="special">,</span> <span class="identifier">std_in</span> <span class="special"><</span> <span class="identifier">in</span><span class="special">);</span>
324 <span class="identifier">in</span> <span class="special"><<</span> <span class="string">"_ZN5boost7process8tutorialE"</span> <span class="special"><<</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">>></span> <span class="identifier">value</span><span class="special">;</span>
328 <span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45170605226736-bb">terminate</a></code><span class="special">();</span>
333 Now you might want to forward output from one process to another processes
338 <pre class="programlisting"><a href="http://en.cppreference.com/w/cpp/container/vector" target="_top">std::vector</a><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></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">&</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>
343 <a href="http://en.cppreference.com/w/cpp/container/vector" target="_top">std::vector</a><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></span> <span class="identifier">outline</span><span class="special">;</span>
345 <span class="comment">//we just use the same pipe, so the </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">></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"><</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">></span> <span class="identifier">is</span><span class="special">);</span>
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">&&</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>
353 <span class="identifier">nm</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45170605250176-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>
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
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>
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>
377 Now we get back to our compiling example. <code class="computeroutput"><span class="identifier">nm</span></code>
378 we might analyze it line by line, but the compiler output will just be put
379 into one large buffer.
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.
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"><</span><span class="keyword">char</span><span class="special">></span> <span class="identifier">buf</span><span class="special">(</span><span class="number">4096</span><span class="special">);</span>
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>
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">></span> <span class="identifier">ap</span><span class="special">);</span>
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">&</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>
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>
403 To make it easier, boost.process provides 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>.
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"><</span><span class="keyword">char</span><span class="special">></span> <span class="identifier">buf</span><span class="special">(</span><span class="number">4096</span><span class="special">);</span>
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">></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>
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>
419 <div class="note"><table border="0" summary="Note">
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>
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#idm45170605250176-bb">wait</a></code>
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>.
439 Now we will revisit our first example and read the compiler output asynchronously:
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>
445 <span class="identifier">std</span><span class="special">::</span><span class="identifier">future</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></span> <span class="identifier">data</span><span class="special">;</span>
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">></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">></span> <span class="identifier">data</span><span class="special">,</span>
451 <span class="identifier">ios</span><span class="special">);</span>
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>
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>
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>
466 When launching several processes, processes can be grouped together. This
467 will also apply for a child process, that launches other processes, if they
468 do 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.
473 The two main reasons to use groups are:
475 <div class="orderedlist"><ol class="orderedlist" type="1">
476 <li class="listitem">
477 Being able to terminate child processes of the child process
479 <li class="listitem">
480 Grouping several processes into one, just so they can be terminated at
485 If we have 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#idm45170605226736-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:
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#idm45170605246880-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#idm45170605226736-bb">terminate</a></code><span class="special">();</span> <span class="comment">//then terminate</span>
500 So in order to also terminate <code class="computeroutput"><span class="identifier">gcc</span></code>
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#idm45170604717728-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#idm45170604697552-bb">terminate</a></code><span class="special">();</span>
510 <span class="identifier">c</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45170605250176-bb">wait</a></code><span class="special">();</span> <span class="comment">//to avoid a zombie process & get the exit code</span>
515 Now given the example, we still call <code class="computeroutput"><a class="link" href="../boost/process/child.html#idm45170605250176-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>.
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.
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>
531 <span class="identifier">do_something</span><span class="special">();</span>
533 <span class="identifier">g</span><span class="special">.</span><code class="computeroutput"><a class="link" href="../boost/process/group.html#idm45170604721232-bb">wait</a></code><span class="special">();</span>
534 <span class="special">};</span>
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
544 Please see the <code class="computeroutput"><a class="link" href="../process/reference.html#header.boost.process.group_hpp" title="Header <boost/process/group.hpp>">reference</a></code>
545 for more information.
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>
553 This library provides access to the environment of the current process and
554 allows setting it for the child process.
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:deadlock :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>
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>
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>
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 the example as
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>
584 Please see to the <code class="computeroutput"><a class="link" href="../process/reference.html#header.boost.process.environment_hpp" title="Header <boost/process/environment.hpp>">reference</a></code>
585 for more information.
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)
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>