• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Fork</title>
5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../boost_asio.html" title="Boost.Asio">
8<link rel="up" href="../posix.html" title="POSIX-Specific Functionality">
9<link rel="prev" href="stream_descriptor.html" title="Stream-Oriented File Descriptors">
10<link rel="next" href="../windows.html" title="Windows-Specific Functionality">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="stream_descriptor.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../posix.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../windows.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h4 class="title">
27<a name="boost_asio.overview.posix.fork"></a><a class="link" href="fork.html" title="Fork">Fork</a>
28</h4></div></div></div>
29<p>
30          Boost.Asio supports programs that utilise the <code class="computeroutput">fork()</code> system
31          call. Provided the program calls <code class="computeroutput">io_context.notify_fork()</code>
32          at the appropriate times, Boost.Asio will recreate any internal file descriptors
33          (such as the "self-pipe trick" descriptor used for waking up
34          a reactor). The notification is usually performed as follows:
35        </p>
36<pre class="programlisting">io_context_.notify_fork(boost::asio::io_context::fork_prepare);
37if (fork() == 0)
38{
39  io_context_.notify_fork(boost::asio::io_context::fork_child);
40  ...
41}
42else
43{
44  io_context_.notify_fork(boost::asio::io_context::fork_parent);
45  ...
46}
47</pre>
48<p>
49          User-defined services can also be made fork-aware by overriding the <code class="computeroutput">io_context::service::notify_fork()</code>
50          virtual function.
51        </p>
52<p>
53          Note that any file descriptors accessible via Boost.Asio's public API (e.g.
54          the descriptors underlying <code class="computeroutput">basic_socket&lt;&gt;</code>, <code class="computeroutput">posix::stream_descriptor</code>,
55          etc.) are not altered during a fork. It is the program's responsibility
56          to manage these as required.
57        </p>
58<h6>
59<a name="boost_asio.overview.posix.fork.h0"></a>
60          <span class="phrase"><a name="boost_asio.overview.posix.fork.see_also"></a></span><a class="link" href="fork.html#boost_asio.overview.posix.fork.see_also">See
61          Also</a>
62        </h6>
63<p>
64          <a class="link" href="../../reference/io_context/notify_fork.html" title="io_context::notify_fork">io_context::notify_fork()</a>,
65          <a class="link" href="../../reference/io_context/fork_event.html" title="io_context::fork_event">io_context::fork_event</a>,
66          <a class="link" href="../../reference/execution_context__service/notify_fork.html" title="execution_context::service::notify_fork">io_context::service::notify_fork()</a>,
67          <a class="link" href="../../examples/cpp03_examples.html#boost_asio.examples.cpp03_examples.fork">Fork examples</a>.
68        </p>
69</div>
70<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
71<td align="left"></td>
72<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
73      Kohlhoff<p>
74        Distributed under the Boost Software License, Version 1.0. (See accompanying
75        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
76      </p>
77</div></td>
78</tr></table>
79<hr>
80<div class="spirit-nav">
81<a accesskey="p" href="stream_descriptor.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../posix.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../windows.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
82</div>
83</body>
84</html>
85