2021-10-05 21:37:46 +02:00

166 lines
12 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Frequently Asked Questions</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../process.html" title="Chapter 30. Boost.Process">
<link rel="prev" href="extend.html" title="Extensions">
<link rel="next" href="../process/reference.html" title="Reference">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_process.faq"></a><a class="link" href="faq.html" title="Frequently Asked Questions">Frequently Asked Questions</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="faq.html#boost_process.faq.dead_lock">Why does this produce a
deadlock?</a></span></dt>
<dt><span class="section"><a href="faq.html#boost_process.faq.closep">Why does the pipe not close?</a></span></dt>
<dt><span class="section"><a href="faq.html#boost_process.faq.wchar_t">When will the codecvt be used?</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<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
deadlock?</a>
</h3></div></div></div>
<p>
Now let's revisit our c++filt example and we will put in an obvious mistake.
This might however be not as obvious for more complex applications.
</p>
<p>
</p>
<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>
<span class="special">{</span>
<span class="identifier">ipstream</span> <span class="identifier">is</span><span class="special">;</span>
<span class="identifier">opstream</span> <span class="identifier">os</span><span class="special">;</span>
<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>
<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>
<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>
<span class="special">{</span>
<span class="identifier">string</span> <span class="identifier">line</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="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="identifier">elem</span><span class="special">;</span>
<span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
We switched the read and write operation up, so that's causing a dead-lock.
This locks immediately. This is because <code class="computeroutput"><span class="identifier">c</span><span class="special">++</span><span class="identifier">filt</span></code>
expects input, before outputting any data. The launching process on the other
hand waits for its output.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<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>
</h3></div></div></div>
<p>
Now for another example, which might look correct, let's consider you want
to use <code class="computeroutput"><span class="identifier">ls</span></code> to read the current
directory.
</p>
<p>
</p>
<pre class="programlisting"><span class="identifier">ipstream</span> <span class="identifier">is</span><span class="special">;</span>
<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>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">file</span><span class="special">;</span>
<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>
<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>
</pre>
<p>
</p>
<p>
This will also deadlock, because the pipe does not close when the subprocess
exits. So the <code class="computeroutput"><span class="identifier">ipstream</span></code> will
still look for data even though the process has ended.
</p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
It is not possible to use automatic pipe-closing in this library, because
a pipe might be a file-handle (as for async pipes on windows).
</p></td></tr>
</table></div>
<p>
But, since pipes are buffered, you might get incomplete data if you do this:
</p>
<p>
</p>
<pre class="programlisting"><span class="identifier">ipstream</span> <span class="identifier">is</span><span class="special">;</span>
<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>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">file</span><span class="special">;</span>
<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>
<span class="special">{</span>
<span class="identifier">is</span> <span class="special">&gt;&gt;</span> <span class="identifier">file</span><span class="special">;</span>
<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>
<span class="special">}</span>
</pre>
<p>
</p>
<p>
It is therefore highly recommended that you use the asynchronous API if you
are not absolutely sure how the output will look.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<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>
</h3></div></div></div>
<p>
Since windows does not use UTF-8 it is sometimes unavoidable to use the
<code class="computeroutput"><span class="keyword">wchar_t</span></code> version of the WinApi.
To keep this library consistent it provides <code class="computeroutput"><span class="keyword">wchar_t</span></code>
support on posix also.
</p>
<p>
Since the posix api is purely <code class="computeroutput"><span class="keyword">char</span></code>
every <code class="computeroutput"><span class="keyword">wchar_t</span></code> based type will
be converted into <code class="computeroutput"><span class="keyword">char</span></code>.
</p>
<p>
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
<code class="computeroutput"><span class="keyword">wchar_t</span></code>. This also includes
<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
not provide the <code class="computeroutput"><span class="keyword">char</span></code> api (as
is the case with Windows CE) everything will also be converted.
</p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2006-2012 Julio M. Merino Vidal, Ilya Sokolov,
Felipe Tanus, Jeff Flinn, Boris Schaeling<br>Copyright © 2016 Klemens D. Morgenstern<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
</body>
</html>