boost-react-native-bundle
Version:
Boost library as in https://sourceforge.net/projects/boost/files/boost/1.57.0/
569 lines (547 loc) • 122 kB
HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Sharing memory between processes</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../interprocess.html" title="Chapter 14. Boost.Interprocess">
<link rel="prev" href="some_basic_explanations.html" title="Some basic explanations">
<link rel="next" href="offset_ptr.html" title="Mapping Address Independent Pointer: offset_ptr">
</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="some_basic_explanations.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../interprocess.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="offset_ptr.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="interprocess.sharedmemorybetweenprocesses"></a><a class="link" href="sharedmemorybetweenprocesses.html" title="Sharing memory between processes">Sharing memory
between processes</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory">Shared
memory</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file">Memory
Mapped Files</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region">More
About Mapped Regions</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations">Limitations
When Constructing Objects In Mapped Regions</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory" title="Shared memory">Shared
memory</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_what_is">What
is shared memory?</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_steps">Creating
memory segments that can be shared between processes</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_header">Header</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_creating_shared_memory_segments">Creating
shared memory segments</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_mapping_shared_memory_segments">Mapping
Shared Memory Segments</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_a_simple_example">A
Simple Example</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.emulation">Emulation
for systems without shared memory objects</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.removing">Removing
shared memory</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.anonymous_shared_memory">Anonymous
shared memory for UNIX systems</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.windows_shared_memory">Native
windows shared memory</a></span></dt>
<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.xsi_shared_memory">XSI
shared memory</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_what_is"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_what_is" title="What is shared memory?">What
is shared memory?</a>
</h4></div></div></div>
<p>
Shared memory is the fastest interprocess communication mechanism. The
operating system maps a memory segment in the address space of several
processes, so that several processes can read and write in that memory
segment without calling operating system functions. However, we need some
kind of synchronization between processes that read and write shared memory.
</p>
<p>
Consider what happens when a server process wants to send an HTML file
to a client process that resides in the same machine using network mechanisms:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
The server must read the file to memory and pass it to the network
functions, that copy that memory to the OS's internal memory.
</li>
<li class="listitem">
The client uses the network functions to copy the data from the OS's
internal memory to its own memory.
</li>
</ul></div>
<p>
As we can see, there are two copies, one from memory to the network and
another one from the network to memory. And those copies are made using
operating system calls that normally are expensive. Shared memory avoids
this overhead, but we need to synchronize both processes:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
The server maps a shared memory in its address space and also gets
access to a synchronization mechanism. The server obtains exclusive
access to the memory using the synchronization mechanism and copies
the file to memory.
</li>
<li class="listitem">
The client maps the shared memory in its address space. Waits until
the server releases the exclusive access and uses the data.
</li>
</ul></div>
<p>
Using shared memory, we can avoid two data copies, but we have to synchronize
the access to the shared memory segment.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_steps"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_steps" title="Creating memory segments that can be shared between processes">Creating
memory segments that can be shared between processes</a>
</h4></div></div></div>
<p>
To use shared memory, we have to perform 2 basic steps:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
Request to the operating system a memory segment that can be shared
between processes. The user can create/destroy/open this memory using
a <span class="bold"><strong>shared memory object</strong></span>: <span class="emphasis"><em>An
object that represents memory that can be mapped concurrently into
the address space of more than one process.</em></span>.
</li>
<li class="listitem">
Associate a part of that memory or the whole memory with the address
space of the calling process. The operating system looks for a big
enough memory address range in the calling process' address space and
marks that address range as an special range. Changes in that address
range are automatically seen by other process that also have mapped
the same shared memory object.
</li>
</ul></div>
<p>
Once the two steps have been successfully completed, the process can start
writing to and reading from the address space to send to and receive data
from other processes. Now, let's see how can we do this using <span class="bold"><strong>Boost.Interprocess</strong></span>:
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_header"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_header" title="Header">Header</a>
</h4></div></div></div>
<p>
To manage shared memory, you just need to include the following header:
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">shared_memory_object</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_creating_shared_memory_segments"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_creating_shared_memory_segments" title="Creating shared memory segments">Creating
shared memory segments</a>
</h4></div></div></div>
<p>
As we've mentioned we have to use the <code class="computeroutput"><span class="identifier">shared_memory_object</span></code>
class to create, open and destroy shared memory segments that can be mapped
by several processes. We can specify the access mode of that shared memory
object (read only or read-write), just as if it was a file:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
Create a shared memory segment. Throws if already created:
</li></ul></div>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
<span class="identifier">shared_memory_object</span> <span class="identifier">shm_obj</span>
<span class="special">(</span><span class="identifier">create_only</span> <span class="comment">//only create</span>
<span class="special">,</span><span class="string">"shared_memory"</span> <span class="comment">//name</span>
<span class="special">,</span><span class="identifier">read_write</span> <span class="comment">//read-write mode</span>
<span class="special">);</span>
</pre>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
To open or create a shared memory segment:
</li></ul></div>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
<span class="identifier">shared_memory_object</span> <span class="identifier">shm_obj</span>
<span class="special">(</span><span class="identifier">open_or_create</span> <span class="comment">//open or create</span>
<span class="special">,</span><span class="string">"shared_memory"</span> <span class="comment">//name</span>
<span class="special">,</span><span class="identifier">read_only</span> <span class="comment">//read-only mode</span>
<span class="special">);</span>
</pre>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
To only open a shared memory segment. Throws if does not exist:
</li></ul></div>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
<span class="identifier">shared_memory_object</span> <span class="identifier">shm_obj</span>
<span class="special">(</span><span class="identifier">open_only</span> <span class="comment">//only open</span>
<span class="special">,</span><span class="string">"shared_memory"</span> <span class="comment">//name</span>
<span class="special">,</span><span class="identifier">read_write</span> <span class="comment">//read-write mode</span>
<span class="special">);</span>
</pre>
<p>
When a shared memory object is created, its size is 0. To set the size
of the shared memory, the user must use the <code class="computeroutput"><span class="identifier">truncate</span></code>
function call, in a shared memory that has been opened with read-write
attributes:
</p>
<pre class="programlisting"><span class="identifier">shm_obj</span><span class="special">.</span><span class="identifier">truncate</span><span class="special">(</span><span class="number">10000</span><span class="special">);</span>
</pre>
<p>
As shared memory has kernel or filesystem persistence, the user must explicitly
destroy it. The <code class="computeroutput"><span class="identifier">remove</span></code>
operation might fail returning false if the shared memory does not exist,
the file is open or the file is still memory mapped by other processes:
</p>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
<span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"shared_memory"</span><span class="special">);</span>
</pre>
<p>
For more details regarding <code class="computeroutput"><span class="identifier">shared_memory_object</span></code>
see the <code class="computeroutput"><a class="link" href="../boost/interprocess/shared_memory_object.html" title="Class shared_memory_object">boost::interprocess::shared_memory_object</a></code>
class reference.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_mapping_shared_memory_segments"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_mapping_shared_memory_segments" title="Mapping Shared Memory Segments">Mapping
Shared Memory Segments</a>
</h4></div></div></div>
<p>
Once created or opened, a process just has to map the shared memory object
in the process' address space. The user can map the whole shared memory
or just part of it. The mapping process is done using the <code class="computeroutput"><span class="identifier">mapped_region</span></code> class. The class represents
a memory region that has been mapped from a shared memory or from other
devices that have also mapping capabilities (for example, files). A <code class="computeroutput"><span class="identifier">mapped_region</span></code> can be created from any
<code class="computeroutput"><span class="identifier">memory_mappable</span></code> object
and as you might imagine, <code class="computeroutput"><span class="identifier">shared_memory_object</span></code>
is a <code class="computeroutput"><span class="identifier">memory_mappable</span></code> object:
</p>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">ShmSize</span> <span class="special">=</span> <span class="special">...</span>
<span class="comment">//Map the second half of the memory</span>
<span class="identifier">mapped_region</span> <span class="identifier">region</span>
<span class="special">(</span> <span class="identifier">shm</span> <span class="comment">//Memory-mappable object</span>
<span class="special">,</span> <span class="identifier">read_write</span> <span class="comment">//Access mode</span>
<span class="special">,</span> <span class="identifier">ShmSize</span><span class="special">/</span><span class="number">2</span> <span class="comment">//Offset from the beginning of shm</span>
<span class="special">,</span> <span class="identifier">ShmSize</span><span class="special">-</span><span class="identifier">ShmSize</span><span class="special">/</span><span class="number">2</span> <span class="comment">//Length of the region</span>
<span class="special">);</span>
<span class="comment">//Get the address of the region</span>
<span class="identifier">region</span><span class="special">.</span><span class="identifier">get_address</span><span class="special">();</span>
<span class="comment">//Get the size of the region</span>
<span class="identifier">region</span><span class="special">.</span><span class="identifier">get_size</span><span class="special">();</span>
</pre>
<p>
The user can specify the offset from the mappable object where the mapped
region should start and the size of the mapped region. If no offset or
size is specified, the whole mappable object (in this case, shared memory)
is mapped. If the offset is specified, but not the size, the mapped region
covers from the offset until the end of the mappable object.
</p>
<p>
For more details regarding <code class="computeroutput"><span class="identifier">mapped_region</span></code>
see the <code class="computeroutput"><a class="link" href="../boost/interprocess/mapped_region.html" title="Class mapped_region">boost::interprocess::mapped_region</a></code>
class reference.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_a_simple_example"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_a_simple_example" title="A Simple Example">A
Simple Example</a>
</h4></div></div></div>
<p>
Let's see a simple example of shared memory use. A server process creates
a shared memory object, maps it and initializes all the bytes to a value.
After that, a client process opens the shared memory, maps it, and checks
that the data is correctly initialized:
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">shared_memory_object</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">mapped_region</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstring</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstdlib</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">argv</span><span class="special">[])</span>
<span class="special">{</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
<span class="keyword">if</span><span class="special">(</span><span class="identifier">argc</span> <span class="special">==</span> <span class="number">1</span><span class="special">){</span> <span class="comment">//Parent process</span>
<span class="comment">//Remove shared memory on construction and destruction</span>
<span class="keyword">struct</span> <span class="identifier">shm_remove</span>
<span class="special">{</span>
<span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
<span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
<span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
<span class="comment">//Create a shared memory object.</span>
<span class="identifier">shared_memory_object</span> <span class="identifier">shm</span> <span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span> <span class="string">"MySharedMemory"</span><span class="special">,</span> <span class="identifier">read_write</span><span class="special">);</span>
<span class="comment">//Set size</span>
<span class="identifier">shm</span><span class="special">.</span><span class="identifier">truncate</span><span class="special">(</span><span class="number">1000</span><span class="special">);</span>
<span class="comment">//Map the whole shared memory in this process</span>
<span class="identifier">mapped_region</span> <span class="identifier">region</span><span class="special">(</span><span class="identifier">shm</span><span class="special">,</span> <span class="identifier">read_write</span><span class="special">);</span>
<span class="comment">//Write all the memory to 1</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">memset</span><span class="special">(</span><span class="identifier">region</span><span class="special">.</span><span class="identifier">get_address</span><span class="special">(),</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">region</span><span class="special">.</span><span class="identifier">get_size</span><span class="special">());</span>
<span class="comment">//Launch child process</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">(</span><span class="identifier">argv</span><span class="special">[</span><span class="number">0</span><span class="special">]);</span> <span class="identifier">s</span> <span class="special">+=</span> <span class="string">" child "</span><span class="special">;</span>
<span class="keyword">if</span><span class="special">(</span><span class="number">0</span> <span class="special">!=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">system</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">c_str</span><span class="special">()))</span>
<span class="keyword">return</span> <span class="number">1</span><span class="special">;</span>
<span class="special">}</span>
<span class="keyword">else</span><span class="special">{</span>
<span class="comment">//Open already created shared memory object.</span>
<span class="identifier">shared_memory_object</span> <span class="identifier">shm</span> <span class="special">(</span><span class="identifier">open_only</span><span class="special">,</span> <span class="string">"MySharedMemory"</span><span class="special">,</span> <span class="identifier">read_only</span><span class="special">);</span>
<span class="comment">//Map the whole shared memory in this process</span>
<span class="identifier">mapped_region</span> <span class="identifier">region</span><span class="special">(</span><span class="identifier">shm</span><span class="special">,</span> <span class="identifier">read_only</span><span class="special">);</span>
<span class="comment">//Check that memory was initialized to 1</span>
<span class="keyword">char</span> <span class="special">*</span><span class="identifier">mem</span> <span class="special">=</span> <span class="keyword">static_cast</span><span class="special"><</span><span class="keyword">char</span><span class="special">*>(</span><span class="identifier">region</span><span class="special">.</span><span class="identifier">get_address</span><span class="special">());</span>
<span class="keyword">for</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="identifier">region</span><span class="special">.</span><span class="identifier">get_size</span><span class="special">();</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span>
<span class="keyword">if</span><span class="special">(*</span><span class="identifier">mem</span><span class="special">++</span> <span class="special">!=</span> <span class="number">1</span><span class="special">)</span>
<span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> <span class="comment">//Error checking memory</span>
<span class="special">}</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.emulation"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.emulation" title="Emulation for systems without shared memory objects">Emulation
for systems without shared memory objects</a>
</h4></div></div></div>
<p>
<span class="bold"><strong>Boost.Interprocess</strong></span> provides portable shared
memory in terms of POSIX semantics. Some operating systems don't support
shared memory as defined by POSIX:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
Windows operating systems provide shared memory using memory backed
by the paging file but the lifetime semantics are different from the
ones defined by POSIX (see <a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.windows_shared_memory" title="Native windows shared memory">Native
windows shared memory</a> section for more information).
</li>
<li class="listitem">
Some UNIX systems don't fully support POSIX shared memory objects at
all.
</li>
</ul></div>
<p>
In those platforms, shared memory is emulated with mapped files created
in a "boost_interprocess" folder created in a temporary files
directory. In Windows platforms, if "Common AppData" key is present
in the registry, "boost_interprocess" folder is created in that
directory (in XP usually "C:\Documents and Settings\All Users\Application
Data" and in Vista "C:\ProgramData"). For Windows platforms
without that registry key and Unix systems, shared memory is created in
the system temporary files directory ("/tmp" or similar).
</p>
<p>
Because of this emulation, shared memory has filesystem lifetime in some
of those systems.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.removing"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.removing" title="Removing shared memory">Removing
shared memory</a>
</h4></div></div></div>
<p>
<code class="computeroutput"><a class="link" href="../boost/interprocess/shared_memory_object.html" title="Class shared_memory_object">shared_memory_object</a></code>
provides a static <code class="computeroutput"><span class="identifier">remove</span></code>
function to remove a shared memory objects.
</p>
<p>
This function <span class="bold"><strong>can</strong></span> fail if the shared memory
objects does not exist or it's opened by another process. Note that this
function is similar to the standard C <code class="computeroutput"><span class="keyword">int</span>
<span class="identifier">remove</span><span class="special">(</span><span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">path</span><span class="special">)</span></code> function. In UNIX systems, <code class="computeroutput"><span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span></code> calls <code class="computeroutput"><span class="identifier">shm_unlink</span></code>:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
The function will remove the name of the shared memory object named
by the string pointed to by name.
</li>
<li class="listitem">
If one or more references to the shared memory object exist when is
unlinked, the name will be removed before the function returns, but
the removal of the memory object contents will be postponed until all
open and map references to the shared memory object have been removed.
</li>
<li class="listitem">
Even if the object continues to exist after the last function call,
reuse of the name will subsequently cause the creation of a <code class="computeroutput"><a class="link" href="../boost/interprocess/shared_memory_object.html" title="Class shared_memory_object">boost::interprocess::shared_memory_object</a></code>
instance to behave as if no shared memory object of this name exists
(that is, trying to open an object with that name will fail and an
object of the same name can be created again).
</li>
</ul></div>
<p>
In Windows operating systems, current version supports an usually acceptable
emulation of the UNIX unlink behaviour: the file is renamed with a random
name and marked as <span class="emphasis"><em>to be deleted when the last open handle is
closed</em></span>.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.anonymous_shared_memory"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.anonymous_shared_memory" title="Anonymous shared memory for UNIX systems">Anonymous
shared memory for UNIX systems</a>
</h4></div></div></div>
<p>
Creating a shared memory segment and mapping it can be a bit tedious when
several processes are involved. When processes are related via <code class="computeroutput"><span class="identifier">fork</span><span class="special">()</span></code>
operating system call in UNIX systems a simpler method is available using
anonymous shared memory.
</p>
<p>
This feature has been implemented in UNIX systems mapping the device <code class="computeroutput"><span class="special">\</span><span class="identifier">dev</span><span class="special">\</span><span class="identifier">zero</span></code>
or just using the <code class="computeroutput"><span class="identifier">MAP_ANONYMOUS</span></code>
in a POSIX conformant <code class="computeroutput"><span class="identifier">mmap</span></code>
system call.
</p>
<p>
This feature is wrapped in <span class="bold"><strong>Boost.Interprocess</strong></span>
using the <code class="computeroutput"><span class="identifier">anonymous_shared_memory</span><span class="special">()</span></code> function, which returns a <code class="computeroutput"><span class="identifier">mapped_region</span></code> object holding an anonymous
shared memory segment that can be shared by related processes.
</p>
<p>
Here is an example:
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">anonymous_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">mapped_region</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstring</span><span class="special">></span>
<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
<span class="special">{</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
<span class="keyword">try</span><span class="special">{</span>
<span class="comment">//Create an anonymous shared memory segment with size 1000</span>
<span class="identifier">mapped_region</span> <span class="identifier">region</span><span class="special">(</span><span class="identifier">anonymous_shared_memory</span><span class="special">(</span><span class="number">1000</span><span class="special">));</span>
<span class="comment">//Write all the memory to 1</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">memset</span><span class="special">(</span><span class="identifier">region</span><span class="special">.</span><span class="identifier">get_address</span><span class="special">(),</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">region</span><span class="special">.</span><span class="identifier">get_size</span><span class="special">());</span>
<span class="comment">//The segment is unmapped when "region" goes out of scope</span>
<span class="special">}</span>
<span class="keyword">catch</span><span class="special">(</span><span class="identifier">interprocess_exception</span> <span class="special">&</span><span class="identifier">ex</span><span class="special">){</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">ex</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="keyword">return</span> <span class="number">1</span><span class="special">;</span>
<span class="special">}</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
</p>
<p>
Once the segment is created, a <code class="computeroutput"><span class="identifier">fork</span><span class="special">()</span></code> call can be used so that <code class="computeroutput"><span class="identifier">region</span></code> is used to communicate two related
processes.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.windows_shared_memory"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.windows_shared_memory" title="Native windows shared memory">Native
windows shared memory</a>
</h4></div></div></div>
<p>
Windows operating system also offers shared memory, but the lifetime of
this shared memory is very different to kernel or filesystem lifetime.
The shared memory is created backed by the pagefile and it's automatically
destroyed when the last process attached to the shared memory is destroyed.
</p>
<p>
Because of this reason, there is no effective way to simulate kernel or
filesystem persistence using native windows shared memory and <span class="bold"><strong>Boost.Interprocess</strong></span> emulates shared memory using
memory mapped files. This assures portability between POSIX and Windows
operating systems.
</p>
<p>
However, accessing native windows shared memory is a common request of
<span class="bold"><strong>Boost.Interprocess</strong></span> users because they
want to access to shared memory created with other process that don't use
<span class="bold"><strong>Boost.Interprocess</strong></span>. In order to manage
the native windows shared memory <span class="bold"><strong>Boost.Interprocess</strong></span>
offers the <code class="computeroutput"><a class="link" href="../boost/interprocess/windows_shared_memory.html" title="Class windows_shared_memory">windows_shared_memory</a></code>
class.
</p>
<p>
Windows shared memory creation is a bit different from portable shared
memory creation: the size of the segment must be specified when creating
the object and can't be specified through <code class="computeroutput"><span class="identifier">truncate</span></code>
like with the shared memory object. Take in care that when the last process
attached to a shared memory is destroyed <span class="bold"><strong>the shared
memory is destroyed</strong></span> so there is <span class="bold"><strong>no persistency</strong></span>
with native windows shared memory.
</p>
<p>
Sharing memory between services and user applications is also different.
To share memory between services and user applications the name of the
shared memory must start with the global namespace prefix <code class="computeroutput"><span class="string">"Global\\"</span></code>. This global namespace
enables processes on multiple client sessions to communicate with a service
application. The server component can create the shared memory in the global
namespace. Then a client session can use the "Global" prefix
to open that memory.
</p>
<p>
The creation of a shared memory object in the global namespace from a session
other than session zero is a privileged operation.
</p>
<p>
Let's repeat the same example presented for the portable shared memory
object: A server process creates a shared memory object, maps it and initializes
all the bytes to a value. After that, a client process opens the shared
memory, maps it, and checks that the data is correctly initialized. Take
in care that <span class="bold"><strong>if the server exits before the client
connects to the shared memory the client connection will fail</strong></span>,
because the shared memory segment is destroyed when no proces is attached
to the memory.
</p>
<p>
This is the server process:
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">windows_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">mapped_region</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstring</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstdlib</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">argv</span><span class="special">[])</span>
<span class="special">{</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
<span class="keyword">if</span><span class="special">(</span><span class="identifier">argc</span> <span class="special">==</span> <span class="number">1</span><span class="special">){</span> <span class="comment">//Parent process</span>
<span class="comment">//Create a native windows shared memory object.</span>
<span class="identifier">windows_shared_memory</span> <span class="identifier">shm</span> <span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span> <span class="string">"MySharedMemory"</span><span class="special">,</span> <span class="identifier">read_write</span><span class="special">,</span> <span class="number">1000</span><span class="special">);</span>
<span class="comment">//Map the whole shared memory in this process</span>
<span class="identifier">mapped_region</span> <span class="identifier">region</span><span class="special">(</span><span class="identifier">shm</span><span class="special">,</span> <span class="identifier">read_write</span><span class="special">);</span>
<span class="comment">//Write all the memory to 1</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">memset</span><span class="special">(</span><span class="identifier">region</span><span class="special">.</span><span class="identifier">get_address</span><span class="special">(),</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">region</span><span class="special">.</span><span class="identifier">get_size</span><span class="special">());</span>
<span class="comment">//Launch child process</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">(</span><span class="identifier">argv</span><span class="special">[</span><span class="number">0</span><span class="special">]);</span> <span class="identifier">s</span> <span class="special">+=</span> <span class="string">" child "</span><span class="special">;</span>
<span class="keyword">if</span><span class="special">(</span><span class="number">0</span> <span class="special">!=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">system</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">c_str</span><span class="special">()))</span>
<span class="keyword">return</span> <span class="number">1</span><span class="special">;</span>
<span class="comment">//windows_shared_memory is destroyed when the last attached process dies...</span>
<span class="special">}</span>
<span class="keyword">else</span><span class="special">{</span>
<span class="comment">//Open already created shared memory object.</span>
<span class="identifier">windows_shared_memory</span> <span class="identifier">shm</span> <span class="special">(</span><span class="identifier">open_only</span><span class="special">,</span> <span class="string">"MySharedMemory"</span><span class="special">,</span> <span class="identifier">read_only</span><span class="special">);</span>
<span class="comment">//Map the whole shared memory in this process</span>
<span class="identifier">mapped_region</span> <span class="identifier">region</span><span class="special">(</span><span class="identifier">shm</span><span class="special">,</span> <span class="identifier">read_only</span><span class="special">);</span>
<span class="comment">//Check that memory was initialized to 1</span>
<span class="keyword">char</span> <span class="special">*</span><span class="identifier">mem</span> <span class="special">=</span> <span class="keyword">static_cast</span><span class="special"><</span><span class="keyword">char</span><span class="special">*>(</span><span class="identifier">region</span><span class="special">.</span><span class="identifier">get_address</span><span class="special">());</span>
<span class="keyword">for</span><span class="special">(</span><span class="identifier">std</span><s