• 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>Frequently Asked Questions</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="extend.html" title="Extensions">
11<link rel="next" href="../process/reference.html" title="Reference">
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="extend.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="../process/reference.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.faq"></a><a class="link" href="faq.html" title="Frequently Asked Questions">Frequently Asked Questions</a>
29</h2></div></div></div>
30<div class="toc"><dl class="toc">
31<dt><span class="section"><a href="faq.html#boost_process.faq.dead_lock">Why does this produce a
32      deadlock?</a></span></dt>
33<dt><span class="section"><a href="faq.html#boost_process.faq.closep">Why does the pipe not close?</a></span></dt>
34<dt><span class="section"><a href="faq.html#boost_process.faq.wchar_t">When will the codecvt be used?</a></span></dt>
35</dl></div>
36<div class="section">
37<div class="titlepage"><div><div><h3 class="title">
38<a name="boost_process.faq.dead_lock"></a><a class="link" href="faq.html#boost_process.faq.dead_lock" title="Why does this produce a deadlock?">Why does this produce a
39      deadlock?</a>
40</h3></div></div></div>
41<p>
42        Now let's revisit our c++filt example and we will put in an obvious mistake.
43        This might however be not as obvious for more complex applications.
44      </p>
45<p>
46</p>
47<pre class="programlisting"><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">demangle</span><span class="special">(</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">in</span><span class="special">)</span>
48<span class="special">{</span>
49
50    <span class="identifier">ipstream</span> <span class="identifier">is</span><span class="special">;</span>
51    <span class="identifier">opstream</span> <span class="identifier">os</span><span class="special">;</span>
52    <span class="identifier">child</span> <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">is</span><span class="special">,</span> <span class="identifier">std_in</span> <span class="special">&lt;</span> <span class="identifier">os</span><span class="special">);</span>
53
54    <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">data</span><span class="special">;</span>
55    <span class="keyword">for</span> <span class="special">(</span><span class="keyword">auto</span> <span class="special">&amp;</span> <span class="identifier">elem</span> <span class="special">:</span> <span class="identifier">data</span><span class="special">)</span>
56    <span class="special">{</span>
57        <span class="identifier">string</span> <span class="identifier">line</span><span class="special">;</span>
58        <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>
59        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="identifier">elem</span><span class="special">;</span>
60    <span class="special">}</span>
61<span class="special">}</span>
62</pre>
63<p>
64        We switched the read and write operation up, so that's causing a dead-lock.
65        This locks immediately. This is because <code class="computeroutput"><span class="identifier">c</span><span class="special">++</span><span class="identifier">filt</span></code>
66        expects input, before outputting any data. The launching process on the other
67        hand waits for its output.
68      </p>
69</div>
70<div class="section">
71<div class="titlepage"><div><div><h3 class="title">
72<a name="boost_process.faq.closep"></a><a class="link" href="faq.html#boost_process.faq.closep" title="Why does the pipe not close?">Why does the pipe not close?</a>
73</h3></div></div></div>
74<p>
75        Now for another example, which might look correct, let's consider you want
76        to use <code class="computeroutput"><span class="identifier">ls</span></code> to read the current
77        directory.
78      </p>
79<p>
80</p>
81<pre class="programlisting"><span class="identifier">ipstream</span> <span class="identifier">is</span><span class="special">;</span>
82<span class="identifier">child</span> <span class="identifier">c</span><span class="special">(</span><span class="string">"ls"</span><span class="special">,</span> <span class="identifier">std_out</span> <span class="special">&gt;</span> <span class="identifier">is</span><span class="special">);</span>
83
84<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">file</span><span class="special">;</span>
85<span class="keyword">while</span> <span class="special">(</span><span class="identifier">is</span> <span class="special">&gt;&gt;</span> <span class="identifier">file</span><span class="special">)</span>
86    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"File: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">file</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
87</pre>
88<p>
89      </p>
90<p>
91        This will also deadlock, because the pipe does not close when the subprocess
92        exits. So the <code class="computeroutput"><span class="identifier">ipstream</span></code> will
93        still look for data even though the process has ended.
94      </p>
95<div class="note"><table border="0" summary="Note">
96<tr>
97<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
98<th align="left">Note</th>
99</tr>
100<tr><td align="left" valign="top"><p>
101          It is not possible to use automatic pipe-closing in this library, because
102          a pipe might be a file-handle (as for async pipes on windows).
103        </p></td></tr>
104</table></div>
105<p>
106        But, since pipes are buffered, you might get incomplete data if you do this:
107      </p>
108<p>
109</p>
110<pre class="programlisting"><span class="identifier">ipstream</span> <span class="identifier">is</span><span class="special">;</span>
111<span class="identifier">child</span> <span class="identifier">c</span><span class="special">(</span><span class="string">"ls"</span><span class="special">,</span> <span class="identifier">std_out</span> <span class="special">&gt;</span> <span class="identifier">is</span><span class="special">);</span>
112
113<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">file</span><span class="special">;</span>
114<span class="keyword">while</span> <span class="special">(</span><span class="identifier">c</span><span class="special">.</span><span class="identifier">running</span><span class="special">())</span>
115<span class="special">{</span>
116    <span class="identifier">is</span> <span class="special">&gt;&gt;</span> <span class="identifier">file</span><span class="special">;</span>
117    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"File: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">file</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
118<span class="special">}</span>
119</pre>
120<p>
121      </p>
122<p>
123        It is therefore highly recommended that you use the asynchronous API if you
124        are not absolutely sure how the output will look.
125      </p>
126</div>
127<div class="section">
128<div class="titlepage"><div><div><h3 class="title">
129<a name="boost_process.faq.wchar_t"></a><a class="link" href="faq.html#boost_process.faq.wchar_t" title="When will the codecvt be used?">When will the codecvt be used?</a>
130</h3></div></div></div>
131<p>
132        Since windows does not use UTF-8 it is sometimes unavoidable to use the
133        <code class="computeroutput"><span class="keyword">wchar_t</span></code> version of the WinApi.
134        To keep this library consistent it provides <code class="computeroutput"><span class="keyword">wchar_t</span></code>
135        support on posix also.
136      </p>
137<p>
138        Since the posix api is purely <code class="computeroutput"><span class="keyword">char</span></code>
139        every <code class="computeroutput"><span class="keyword">wchar_t</span></code> based type will
140        be converted into <code class="computeroutput"><span class="keyword">char</span></code>.
141      </p>
142<p>
143        Windows on the other hand is more selective; the default is to use <code class="computeroutput"><span class="keyword">char</span></code>, but if any parameter requires <code class="computeroutput"><span class="keyword">wchar_t</span></code>, everything will be converted to
144        <code class="computeroutput"><span class="keyword">wchar_t</span></code>. This also includes
145        <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>. Additionally, if the system does
146        not provide the <code class="computeroutput"><span class="keyword">char</span></code> api (as
147        is the case with Windows CE) everything will also be converted.
148      </p>
149</div>
150</div>
151<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
152<td align="left"></td>
153<td align="right"><div class="copyright-footer">Copyright © 2006-2012 Julio M. Merino Vidal, Ilya Sokolov,
154      Felipe Tanus, Jeff Flinn, Boris Schaeling<br>Copyright © 2016 Klemens D. Morgenstern<p>
155        Distributed under the Boost Software License, Version 1.0. (See accompanying
156        file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
157      </p>
158</div></td>
159</tr></table>
160<hr>
161<div class="spirit-nav">
162<a accesskey="p" href="extend.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="../process/reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
163</div>
164</body>
165</html>
166