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>Sharing memory between processes</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="../interprocess.html" title="Chapter 18. Boost.Interprocess"> 10<link rel="prev" href="some_basic_explanations.html" title="Some basic explanations"> 11<link rel="next" href="offset_ptr.html" title="Mapping Address Independent Pointer: offset_ptr"> 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="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> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="interprocess.sharedmemorybetweenprocesses"></a><a class="link" href="sharedmemorybetweenprocesses.html" title="Sharing memory between processes">Sharing memory 29 between processes</a> 30</h2></div></div></div> 31<div class="toc"><dl class="toc"> 32<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory">Shared 33 memory</a></span></dt> 34<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file">Memory 35 Mapped Files</a></span></dt> 36<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region">More 37 About Mapped Regions</a></span></dt> 38<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations">Limitations 39 When Constructing Objects In Mapped Regions</a></span></dt> 40</dl></div> 41<div class="section"> 42<div class="titlepage"><div><div><h3 class="title"> 43<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory" title="Shared memory">Shared 44 memory</a> 45</h3></div></div></div> 46<div class="toc"><dl class="toc"> 47<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_what_is">What 48 is shared memory?</a></span></dt> 49<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_steps">Creating 50 memory segments that can be shared between processes</a></span></dt> 51<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_header">Header</a></span></dt> 52<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_creating_shared_memory_segments">Creating 53 shared memory segments</a></span></dt> 54<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_mapping_shared_memory_segments">Mapping 55 Shared Memory Segments</a></span></dt> 56<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.shared_memory_a_simple_example">A 57 Simple Example</a></span></dt> 58<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.emulation">Emulation 59 for systems without shared memory objects</a></span></dt> 60<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.removing">Removing 61 shared memory</a></span></dt> 62<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.anonymous_shared_memory">Anonymous 63 shared memory for UNIX systems</a></span></dt> 64<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.windows_shared_memory">Native 65 windows shared memory</a></span></dt> 66<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.xsi_shared_memory">XSI 67 shared memory</a></span></dt> 68</dl></div> 69<div class="section"> 70<div class="titlepage"><div><div><h4 class="title"> 71<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 72 is shared memory?</a> 73</h4></div></div></div> 74<p> 75 Shared memory is the fastest interprocess communication mechanism. The 76 operating system maps a memory segment in the address space of several 77 processes, so that several processes can read and write in that memory 78 segment without calling operating system functions. However, we need some 79 kind of synchronization between processes that read and write shared memory. 80 </p> 81<p> 82 Consider what happens when a server process wants to send an HTML file 83 to a client process that resides in the same machine using network mechanisms: 84 </p> 85<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 86<li class="listitem"> 87 The server must read the file to memory and pass it to the network 88 functions, that copy that memory to the OS's internal memory. 89 </li> 90<li class="listitem"> 91 The client uses the network functions to copy the data from the OS's 92 internal memory to its own memory. 93 </li> 94</ul></div> 95<p> 96 As we can see, there are two copies, one from memory to the network and 97 another one from the network to memory. And those copies are made using 98 operating system calls that normally are expensive. Shared memory avoids 99 this overhead, but we need to synchronize both processes: 100 </p> 101<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 102<li class="listitem"> 103 The server maps a shared memory in its address space and also gets 104 access to a synchronization mechanism. The server obtains exclusive 105 access to the memory using the synchronization mechanism and copies 106 the file to memory. 107 </li> 108<li class="listitem"> 109 The client maps the shared memory in its address space. Waits until 110 the server releases the exclusive access and uses the data. 111 </li> 112</ul></div> 113<p> 114 Using shared memory, we can avoid two data copies, but we have to synchronize 115 the access to the shared memory segment. 116 </p> 117</div> 118<div class="section"> 119<div class="titlepage"><div><div><h4 class="title"> 120<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 121 memory segments that can be shared between processes</a> 122</h4></div></div></div> 123<p> 124 To use shared memory, we have to perform 2 basic steps: 125 </p> 126<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 127<li class="listitem"> 128 Request to the operating system a memory segment that can be shared 129 between processes. The user can create/destroy/open this memory using 130 a <span class="bold"><strong>shared memory object</strong></span>: <span class="emphasis"><em>An 131 object that represents memory that can be mapped concurrently into 132 the address space of more than one process.</em></span>. 133 </li> 134<li class="listitem"> 135 Associate a part of that memory or the whole memory with the address 136 space of the calling process. The operating system looks for a big 137 enough memory address range in the calling process' address space and 138 marks that address range as an special range. Changes in that address 139 range are automatically seen by other process that also have mapped 140 the same shared memory object. 141 </li> 142</ul></div> 143<p> 144 Once the two steps have been successfully completed, the process can start 145 writing to and reading from the address space to send to and receive data 146 from other processes. Now, let's see how can we do this using <span class="bold"><strong>Boost.Interprocess</strong></span>: 147 </p> 148</div> 149<div class="section"> 150<div class="titlepage"><div><div><h4 class="title"> 151<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> 152</h4></div></div></div> 153<p> 154 To manage shared memory, you just need to include the following header: 155 </p> 156<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> 157</pre> 158</div> 159<div class="section"> 160<div class="titlepage"><div><div><h4 class="title"> 161<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 162 shared memory segments</a> 163</h4></div></div></div> 164<p> 165 As we've mentioned we have to use the <code class="computeroutput"><span class="identifier">shared_memory_object</span></code> 166 class to create, open and destroy shared memory segments that can be mapped 167 by several processes. We can specify the access mode of that shared memory 168 object (read only or read-write), just as if it was a file: 169 </p> 170<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 171 Create a shared memory segment. Throws if already created: 172 </li></ul></div> 173<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> 174<span class="identifier">shared_memory_object</span> <span class="identifier">shm_obj</span> 175 <span class="special">(</span><span class="identifier">create_only</span> <span class="comment">//only create</span> 176 <span class="special">,</span><span class="string">"shared_memory"</span> <span class="comment">//name</span> 177 <span class="special">,</span><span class="identifier">read_write</span> <span class="comment">//read-write mode</span> 178 <span class="special">);</span> 179</pre> 180<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 181 To open or create a shared memory segment: 182 </li></ul></div> 183<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> 184<span class="identifier">shared_memory_object</span> <span class="identifier">shm_obj</span> 185 <span class="special">(</span><span class="identifier">open_or_create</span> <span class="comment">//open or create</span> 186 <span class="special">,</span><span class="string">"shared_memory"</span> <span class="comment">//name</span> 187 <span class="special">,</span><span class="identifier">read_only</span> <span class="comment">//read-only mode</span> 188 <span class="special">);</span> 189</pre> 190<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 191 To only open a shared memory segment. Throws if does not exist: 192 </li></ul></div> 193<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> 194<span class="identifier">shared_memory_object</span> <span class="identifier">shm_obj</span> 195 <span class="special">(</span><span class="identifier">open_only</span> <span class="comment">//only open</span> 196 <span class="special">,</span><span class="string">"shared_memory"</span> <span class="comment">//name</span> 197 <span class="special">,</span><span class="identifier">read_write</span> <span class="comment">//read-write mode</span> 198 <span class="special">);</span> 199</pre> 200<p> 201 When a shared memory object is created, its size is 0. To set the size 202 of the shared memory, the user must use the <code class="computeroutput"><span class="identifier">truncate</span></code> 203 function call, in a shared memory that has been opened with read-write 204 attributes: 205 </p> 206<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> 207</pre> 208<p> 209 As shared memory has kernel or filesystem persistence, the user must explicitly 210 destroy it. The <code class="computeroutput"><span class="identifier">remove</span></code> 211 operation might fail returning false if the shared memory does not exist, 212 the file is open or the file is still memory mapped by other processes: 213 </p> 214<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> 215<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> 216</pre> 217<p> 218 For more details regarding <code class="computeroutput"><span class="identifier">shared_memory_object</span></code> 219 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> 220 class reference. 221 </p> 222</div> 223<div class="section"> 224<div class="titlepage"><div><div><h4 class="title"> 225<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 226 Shared Memory Segments</a> 227</h4></div></div></div> 228<p> 229 Once created or opened, a process just has to map the shared memory object 230 in the process' address space. The user can map the whole shared memory 231 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 232 a memory region that has been mapped from a shared memory or from other 233 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 234 <code class="computeroutput"><span class="identifier">memory_mappable</span></code> object 235 and as you might imagine, <code class="computeroutput"><span class="identifier">shared_memory_object</span></code> 236 is a <code class="computeroutput"><span class="identifier">memory_mappable</span></code> object: 237 </p> 238<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> 239<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> 240 241<span class="comment">//Map the second half of the memory</span> 242<span class="identifier">mapped_region</span> <span class="identifier">region</span> 243 <span class="special">(</span> <span class="identifier">shm</span> <span class="comment">//Memory-mappable object</span> 244 <span class="special">,</span> <span class="identifier">read_write</span> <span class="comment">//Access mode</span> 245 <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> 246 <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> 247 <span class="special">);</span> 248 249<span class="comment">//Get the address of the region</span> 250<span class="identifier">region</span><span class="special">.</span><span class="identifier">get_address</span><span class="special">();</span> 251 252<span class="comment">//Get the size of the region</span> 253<span class="identifier">region</span><span class="special">.</span><span class="identifier">get_size</span><span class="special">();</span> 254</pre> 255<p> 256 The user can specify the offset from the mappable object where the mapped 257 region should start and the size of the mapped region. If no offset or 258 size is specified, the whole mappable object (in this case, shared memory) 259 is mapped. If the offset is specified, but not the size, the mapped region 260 covers from the offset until the end of the mappable object. 261 </p> 262<p> 263 For more details regarding <code class="computeroutput"><span class="identifier">mapped_region</span></code> 264 see the <code class="computeroutput"><a class="link" href="../boost/interprocess/mapped_region.html" title="Class mapped_region">boost::interprocess::mapped_region</a></code> 265 class reference. 266 </p> 267</div> 268<div class="section"> 269<div class="titlepage"><div><div><h4 class="title"> 270<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 271 Simple Example</a> 272</h4></div></div></div> 273<p> 274 Let's see a simple example of shared memory use. A server process creates 275 a shared memory object, maps it and initializes all the bytes to a value. 276 After that, a client process opens the shared memory, maps it, and checks 277 that the data is correctly initialized: 278 </p> 279<p> 280</p> 281<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> 282<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> 283<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstring</span><span class="special">></span> 284<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstdlib</span><span class="special">></span> 285<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 286 287<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> 288<span class="special">{</span> 289 <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> 290 291 <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> 292 <span class="comment">//Remove shared memory on construction and destruction</span> 293 <span class="keyword">struct</span> <span class="identifier">shm_remove</span> 294 <span class="special">{</span> 295 <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> 296 <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> 297 <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span> 298 299 <span class="comment">//Create a shared memory object.</span> 300 <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> 301 302 <span class="comment">//Set size</span> 303 <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> 304 305 <span class="comment">//Map the whole shared memory in this process</span> 306 <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> 307 308 <span class="comment">//Write all the memory to 1</span> 309 <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> 310 311 <span class="comment">//Launch child process</span> 312 <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> 313 <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> 314 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> 315 <span class="special">}</span> 316 <span class="keyword">else</span><span class="special">{</span> 317 <span class="comment">//Open already created shared memory object.</span> 318 <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> 319 320 <span class="comment">//Map the whole shared memory in this process</span> 321 <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> 322 323 <span class="comment">//Check that memory was initialized to 1</span> 324 <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> 325 <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> 326 <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> 327 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> <span class="comment">//Error checking memory</span> 328 <span class="special">}</span> 329 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 330<span class="special">}</span> 331</pre> 332<p> 333 </p> 334</div> 335<div class="section"> 336<div class="titlepage"><div><div><h4 class="title"> 337<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 338 for systems without shared memory objects</a> 339</h4></div></div></div> 340<p> 341 <span class="bold"><strong>Boost.Interprocess</strong></span> provides portable shared 342 memory in terms of POSIX semantics. Some operating systems don't support 343 shared memory as defined by POSIX: 344 </p> 345<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 346<li class="listitem"> 347 Windows operating systems provide shared memory using memory backed 348 by the paging file but the lifetime semantics are different from the 349 ones defined by POSIX (see <a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.windows_shared_memory" title="Native windows shared memory">Native 350 windows shared memory</a> section for more information). 351 </li> 352<li class="listitem"> 353 Some UNIX systems don't fully support POSIX shared memory objects at 354 all. 355 </li> 356</ul></div> 357<p> 358 In those platforms, shared memory is emulated with mapped files created 359 in a "boost_interprocess" folder created in a temporary files 360 directory. In Windows platforms, if "Common AppData" key is present 361 in the registry, "boost_interprocess" folder is created in that 362 directory (in XP usually "C:\Documents and Settings\All Users\Application 363 Data" and in Vista "C:\ProgramData"). For Windows platforms 364 without that registry key and Unix systems, shared memory is created in 365 the system temporary files directory ("/tmp" or similar). 366 </p> 367<p> 368 Because of this emulation, shared memory has filesystem lifetime in some 369 of those systems. 370 </p> 371</div> 372<div class="section"> 373<div class="titlepage"><div><div><h4 class="title"> 374<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.removing"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.removing" title="Removing shared memory">Removing 375 shared memory</a> 376</h4></div></div></div> 377<p> 378 <code class="computeroutput"><a class="link" href="../boost/interprocess/shared_memory_object.html" title="Class shared_memory_object">shared_memory_object</a></code> 379 provides a static <code class="computeroutput"><span class="identifier">remove</span></code> 380 function to remove a shared memory objects. 381 </p> 382<p> 383 This function <span class="bold"><strong>can</strong></span> fail if the shared memory 384 objects does not exist or it's opened by another process. Note that this 385 function is similar to the standard C <code class="computeroutput"><span class="keyword">int</span> 386 <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>: 387 </p> 388<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 389<li class="listitem"> 390 The function will remove the name of the shared memory object named 391 by the string pointed to by name. 392 </li> 393<li class="listitem"> 394 If one or more references to the shared memory object exist when is 395 unlinked, the name will be removed before the function returns, but 396 the removal of the memory object contents will be postponed until all 397 open and map references to the shared memory object have been removed. 398 </li> 399<li class="listitem"> 400 Even if the object continues to exist after the last function call, 401 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> 402 instance to behave as if no shared memory object of this name exists 403 (that is, trying to open an object with that name will fail and an 404 object of the same name can be created again). 405 </li> 406</ul></div> 407<p> 408 In Windows operating systems, current version supports an usually acceptable 409 emulation of the UNIX unlink behaviour: the file is renamed with a random 410 name and marked as <span class="emphasis"><em>to be deleted when the last open handle is 411 closed</em></span>. 412 </p> 413</div> 414<div class="section"> 415<div class="titlepage"><div><div><h4 class="title"> 416<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 417 shared memory for UNIX systems</a> 418</h4></div></div></div> 419<p> 420 Creating a shared memory segment and mapping it can be a bit tedious when 421 several processes are involved. When processes are related via <code class="computeroutput"><span class="identifier">fork</span><span class="special">()</span></code> 422 operating system call in UNIX systems a simpler method is available using 423 anonymous shared memory. 424 </p> 425<p> 426 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> 427 or just using the <code class="computeroutput"><span class="identifier">MAP_ANONYMOUS</span></code> 428 in a POSIX conformant <code class="computeroutput"><span class="identifier">mmap</span></code> 429 system call. 430 </p> 431<p> 432 This feature is wrapped in <span class="bold"><strong>Boost.Interprocess</strong></span> 433 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 434 shared memory segment that can be shared by related processes. 435 </p> 436<p> 437 Here is an example: 438 </p> 439<p> 440</p> 441<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> 442<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> 443<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 444<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstring</span><span class="special">></span> 445 446<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span> 447<span class="special">{</span> 448 <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> 449 <span class="keyword">try</span><span class="special">{</span> 450 <span class="comment">//Create an anonymous shared memory segment with size 1000</span> 451 <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> 452 453 <span class="comment">//Write all the memory to 1</span> 454 <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> 455 456 <span class="comment">//The segment is unmapped when "region" goes out of scope</span> 457 <span class="special">}</span> 458 <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> 459 <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> 460 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> 461 <span class="special">}</span> 462 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 463<span class="special">}</span> 464</pre> 465<p> 466 </p> 467<p> 468 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 469 processes. 470 </p> 471</div> 472<div class="section"> 473<div class="titlepage"><div><div><h4 class="title"> 474<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 475 windows shared memory</a> 476</h4></div></div></div> 477<p> 478 Windows operating system also offers shared memory, but the lifetime of 479 this shared memory is very different to kernel or filesystem lifetime. 480 The shared memory is created backed by the pagefile and it's automatically 481 destroyed when the last process attached to the shared memory is destroyed. 482 </p> 483<p> 484 Because of this reason, there is no effective way to simulate kernel or 485 filesystem persistence using native windows shared memory and <span class="bold"><strong>Boost.Interprocess</strong></span> emulates shared memory using 486 memory mapped files. This assures portability between POSIX and Windows 487 operating systems. 488 </p> 489<p> 490 However, accessing native windows shared memory is a common request of 491 <span class="bold"><strong>Boost.Interprocess</strong></span> users because they 492 want to access to shared memory created with other process that don't use 493 <span class="bold"><strong>Boost.Interprocess</strong></span>. In order to manage 494 the native windows shared memory <span class="bold"><strong>Boost.Interprocess</strong></span> 495 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> 496 class. 497 </p> 498<p> 499 Windows shared memory creation is a bit different from portable shared 500 memory creation: the size of the segment must be specified when creating 501 the object and can't be specified through <code class="computeroutput"><span class="identifier">truncate</span></code> 502 like with the shared memory object. Take in care that when the last process 503 attached to a shared memory is destroyed <span class="bold"><strong>the shared 504 memory is destroyed</strong></span> so there is <span class="bold"><strong>no persistency</strong></span> 505 with native windows shared memory. 506 </p> 507<p> 508 Sharing memory between services and user applications is also different. 509 To share memory between services and user applications the name of the 510 shared memory must start with the global namespace prefix <code class="computeroutput"><span class="string">"Global\\"</span></code>. This global namespace 511 enables processes on multiple client sessions to communicate with a service 512 application. The server component can create the shared memory in the global 513 namespace. Then a client session can use the "Global" prefix 514 to open that memory. 515 </p> 516<p> 517 The creation of a shared memory object in the global namespace from a session 518 other than session zero is a privileged operation. 519 </p> 520<p> 521 Let's repeat the same example presented for the portable shared memory 522 object: A server process creates a shared memory object, maps it and initializes 523 all the bytes to a value. After that, a client process opens the shared 524 memory, maps it, and checks that the data is correctly initialized. Take 525 in care that <span class="bold"><strong>if the server exits before the client 526 connects to the shared memory the client connection will fail</strong></span>, 527 because the shared memory segment is destroyed when no proces is attached 528 to the memory. 529 </p> 530<p> 531 This is the server process: 532 </p> 533<p> 534</p> 535<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> 536<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> 537<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstring</span><span class="special">></span> 538<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstdlib</span><span class="special">></span> 539<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 540 541<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> 542<span class="special">{</span> 543 <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> 544 545 <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> 546 <span class="comment">//Create a native windows shared memory object.</span> 547 <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> 548 549 <span class="comment">//Map the whole shared memory in this process</span> 550 <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> 551 552 <span class="comment">//Write all the memory to 1</span> 553 <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> 554 555 <span class="comment">//Launch child process</span> 556 <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> 557 <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> 558 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> 559 <span class="comment">//windows_shared_memory is destroyed when the last attached process dies...</span> 560 <span class="special">}</span> 561 <span class="keyword">else</span><span class="special">{</span> 562 <span class="comment">//Open already created shared memory object.</span> 563 <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> 564 565 <span class="comment">//Map the whole shared memory in this process</span> 566 <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> 567 568 <span class="comment">//Check that memory was initialized to 1</span> 569 <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> 570 <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> 571 <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> 572 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> <span class="comment">//Error checking memory</span> 573 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 574 <span class="special">}</span> 575 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 576<span class="special">}</span> 577</pre> 578<p> 579 </p> 580<p> 581 As we can see, native windows shared memory needs synchronization to make 582 sure that the shared memory won't be destroyed before the client is launched. 583 </p> 584</div> 585<div class="section"> 586<div class="titlepage"><div><div><h4 class="title"> 587<a name="interprocess.sharedmemorybetweenprocesses.sharedmemory.xsi_shared_memory"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.xsi_shared_memory" title="XSI shared memory">XSI 588 shared memory</a> 589</h4></div></div></div> 590<p> 591 In many UNIX systems, the OS offers another shared memory memory mechanism, 592 XSI (X/Open System Interfaces) shared memory segments, also known as "System 593 V" shared memory. This shared memory mechanism is quite popular and 594 portable, and it's not based in file-mapping semantics, but it uses special 595 functions (<code class="computeroutput"><span class="identifier">shmget</span></code>, <code class="computeroutput"><span class="identifier">shmat</span></code>, <code class="computeroutput"><span class="identifier">shmdt</span></code>, 596 <code class="computeroutput"><span class="identifier">shmctl</span></code>...). 597 </p> 598<p> 599 Unlike POSIX shared memory segments, XSI shared memory segments are not 600 identified by names but by 'keys' usually created with <code class="computeroutput"><span class="identifier">ftok</span></code>. 601 XSI shared memory segments have kernel lifetime and must be explicitly 602 removed. XSI shared memory does not support copy-on-write and partial shared 603 memory mapping but it supports anonymous shared memory. 604 </p> 605<p> 606 <span class="bold"><strong>Boost.Interprocess</strong></span> offers simple (<code class="computeroutput"><a class="link" href="../boost/interprocess/xsi_shared_memory.html" title="Class xsi_shared_memory">xsi_shared_memory</a></code>) 607 and managed (<code class="computeroutput"><a class="link" href="../boost/interprocess/managed_xsi_shared_memory.html" title="Type definition managed_xsi_shared_memory">managed_xsi_shared_memory</a></code>) 608 shared memory classes to ease the use of XSI shared memory. It also wraps 609 key creation with the simple <code class="computeroutput"><a class="link" href="../boost/interprocess/xsi_key.html" title="Class xsi_key">xsi_key</a></code> 610 class. 611 </p> 612<p> 613 Let's repeat the same example presented for the portable shared memory 614 object: A server process creates a shared memory object, maps it and initializes 615 all the bytes to a value. After that, a client process opens the shared 616 memory, maps it, and checks that the data is correctly initialized. 617 </p> 618<p> 619 This is the server process: 620 </p> 621<p> 622</p> 623<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">xsi_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 624<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> 625<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstring</span><span class="special">></span> 626<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstdlib</span><span class="special">></span> 627<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 628 629<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> 630 631<span class="keyword">void</span> <span class="identifier">remove_old_shared_memory</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">xsi_key</span> <span class="special">&</span><span class="identifier">key</span><span class="special">)</span> 632<span class="special">{</span> 633 <span class="keyword">try</span><span class="special">{</span> 634 <span class="identifier">xsi_shared_memory</span> <span class="identifier">xsi</span><span class="special">(</span><span class="identifier">open_only</span><span class="special">,</span> <span class="identifier">key</span><span class="special">);</span> 635 <span class="identifier">xsi_shared_memory</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="identifier">xsi</span><span class="special">.</span><span class="identifier">get_shmid</span><span class="special">());</span> 636 <span class="special">}</span> 637 <span class="keyword">catch</span><span class="special">(</span><span class="identifier">interprocess_exception</span> <span class="special">&</span><span class="identifier">e</span><span class="special">){</span> 638 <span class="keyword">if</span><span class="special">(</span><span class="identifier">e</span><span class="special">.</span><span class="identifier">get_error_code</span><span class="special">()</span> <span class="special">!=</span> <span class="identifier">not_found_error</span><span class="special">)</span> 639 <span class="keyword">throw</span><span class="special">;</span> 640 <span class="special">}</span> 641<span class="special">}</span> 642 643<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> 644<span class="special">{</span> 645 <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> 646 <span class="comment">//Build XSI key (ftok based)</span> 647 <span class="identifier">xsi_key</span> <span class="identifier">key</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="number">1</span><span class="special">);</span> 648 649 <span class="identifier">remove_old_shared_memory</span><span class="special">(</span><span class="identifier">key</span><span class="special">);</span> 650 651 <span class="comment">//Create a shared memory object.</span> 652 <span class="identifier">xsi_shared_memory</span> <span class="identifier">shm</span> <span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span> <span class="identifier">key</span><span class="special">,</span> <span class="number">1000</span><span class="special">);</span> 653 654 <span class="comment">//Remove shared memory on destruction</span> 655 <span class="keyword">struct</span> <span class="identifier">shm_remove</span> 656 <span class="special">{</span> 657 <span class="keyword">int</span> <span class="identifier">shmid_</span><span class="special">;</span> 658 <span class="identifier">shm_remove</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">shmid</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">shmid_</span><span class="special">(</span><span class="identifier">shmid</span><span class="special">){}</span> 659 <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">xsi_shared_memory</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="identifier">shmid_</span><span class="special">);</span> <span class="special">}</span> 660 <span class="special">}</span> <span class="identifier">remover</span><span class="special">(</span><span class="identifier">shm</span><span class="special">.</span><span class="identifier">get_shmid</span><span class="special">());</span> 661 662 <span class="comment">//Map the whole shared memory in this process</span> 663 <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> 664 665 <span class="comment">//Write all the memory to 1</span> 666 <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> 667 668 <span class="comment">//Launch child process</span> 669 <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> 670 <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> 671 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> 672 <span class="special">}</span> 673 <span class="keyword">else</span><span class="special">{</span> 674 <span class="comment">//Build XSI key (ftok based)</span> 675 <span class="identifier">xsi_key</span> <span class="identifier">key</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="number">1</span><span class="special">);</span> 676 677 <span class="comment">//Create a shared memory object.</span> 678 <span class="identifier">xsi_shared_memory</span> <span class="identifier">shm</span> <span class="special">(</span><span class="identifier">open_only</span><span class="special">,</span> <span class="identifier">key</span><span class="special">);</span> 679 680 <span class="comment">//Map the whole shared memory in this process</span> 681 <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> 682 683 <span class="comment">//Check that memory was initialized to 1</span> 684 <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> 685 <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> 686 <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> 687 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> <span class="comment">//Error checking memory</span> 688 <span class="special">}</span> 689 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 690<span class="special">}</span> 691</pre> 692<p> 693 </p> 694</div> 695</div> 696<div class="section"> 697<div class="titlepage"><div><div><h3 class="title"> 698<a name="interprocess.sharedmemorybetweenprocesses.mapped_file"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file" title="Memory Mapped Files">Memory 699 Mapped Files</a> 700</h3></div></div></div> 701<div class="toc"><dl class="toc"> 702<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_what_is">What 703 is a memory mapped file?</a></span></dt> 704<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_steps">Using 705 mapped files</a></span></dt> 706<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_header">Header</a></span></dt> 707<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_creating_file">Creating 708 a file mapping</a></span></dt> 709<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_mapping_regions">Mapping 710 File's Contents In Memory</a></span></dt> 711<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_a_simple_example">A 712 Simple Example</a></span></dt> 713</dl></div> 714<div class="section"> 715<div class="titlepage"><div><div><h4 class="title"> 716<a name="interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_what_is"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_what_is" title="What is a memory mapped file?">What 717 is a memory mapped file?</a> 718</h4></div></div></div> 719<p> 720 File mapping is the association of a file's contents with a portion of 721 the address space of a process. The system creates a file mapping to associate 722 the file and the address space of the process. A mapped region is the portion 723 of address space that the process uses to access the file's contents. A 724 single file mapping can have several mapped regions, so that the user can 725 associate parts of the file with the address space of the process without 726 mapping the entire file in the address space, since the file can be bigger 727 than the whole address space of the process (a 9GB DVD image file in a 728 usual 32 bit systems). Processes read from and write to the file using 729 pointers, just like with dynamic memory. File mapping has the following 730 advantages: 731 </p> 732<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 733<li class="listitem"> 734 Uniform resource use. Files and memory can be treated using the same 735 functions. 736 </li> 737<li class="listitem"> 738 Automatic file data synchronization and cache from the OS. 739 </li> 740<li class="listitem"> 741 Reuse of C++ utilities (STL containers, algorithms) in files. 742 </li> 743<li class="listitem"> 744 Shared memory between two or more applications. 745 </li> 746<li class="listitem"> 747 Allows efficient work with a large files, without mapping the whole 748 file into memory 749 </li> 750<li class="listitem"> 751 If several processes use the same file mapping to create mapped regions 752 of a file, each process' views contain identical copies of the file 753 on disk. 754 </li> 755</ul></div> 756<p> 757 File mapping is not only used for interprocess communication, it can be 758 used also to simplify file usage, so the user does not need to use file-management 759 functions to write the file. The user just writes data to the process memory, 760 and the operating systems dumps the data to the file. 761 </p> 762<p> 763 When two processes map the same file in memory, the memory that one process 764 writes is seen by another process, so memory mapped files can be used as 765 an interprocess communication mechanism. We can say that memory-mapped 766 files offer the same interprocess communication services as shared memory 767 with the addition of filesystem persistence. However, as the operating 768 system has to synchronize the file contents with the memory contents, memory-mapped 769 files are not as fast as shared memory. 770 </p> 771</div> 772<div class="section"> 773<div class="titlepage"><div><div><h4 class="title"> 774<a name="interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_steps"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_steps" title="Using mapped files">Using 775 mapped files</a> 776</h4></div></div></div> 777<p> 778 To use memory-mapped files, we have to perform 2 basic steps: 779 </p> 780<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 781<li class="listitem"> 782 Create a mappable object that represent an already created file of 783 the filesystem. This object will be used to create multiple mapped 784 regions of the the file. 785 </li> 786<li class="listitem"> 787 Associate the whole file or parts of the file with the address space 788 of the calling process. The operating system looks for a big enough 789 memory address range in the calling process' address space and marks 790 that address range as an special range. Changes in that address range 791 are automatically seen by other process that also have mapped the same 792 file and those changes are also transferred to the disk automatically. 793 </li> 794</ul></div> 795<p> 796 Once the two steps have been successfully completed, the process can start 797 writing to and reading from the address space to send to and receive data 798 from other processes and synchronize the file's contents with the changes 799 made to the mapped region. Now, let's see how can we do this using <span class="bold"><strong>Boost.Interprocess</strong></span>: 800 </p> 801</div> 802<div class="section"> 803<div class="titlepage"><div><div><h4 class="title"> 804<a name="interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_header"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_header" title="Header">Header</a> 805</h4></div></div></div> 806<p> 807 To manage mapped files, you just need to include the following header: 808 </p> 809<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">file_mapping</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 810</pre> 811</div> 812<div class="section"> 813<div class="titlepage"><div><div><h4 class="title"> 814<a name="interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_creating_file"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_creating_file" title="Creating a file mapping">Creating 815 a file mapping</a> 816</h4></div></div></div> 817<p> 818 First, we have to link a file's contents with the process' address space. 819 To do this, we have to create a mappable object that represents that file. 820 This is achieved in <span class="bold"><strong>Boost.Interprocess</strong></span> 821 creating a <code class="computeroutput"><span class="identifier">file_mapping</span></code> 822 object: 823 </p> 824<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> 825<span class="identifier">file_mapping</span> <span class="identifier">m_file</span> 826 <span class="special">(</span><span class="string">"/usr/home/file"</span> <span class="comment">//filename</span> 827 <span class="special">,</span><span class="identifier">read_write</span> <span class="comment">//read-write mode</span> 828 <span class="special">);</span> 829</pre> 830<p> 831 Now we can use the newly created object to create mapped regions. For more 832 details regarding this class see the <code class="computeroutput"><a class="link" href="../boost/interprocess/file_mapping.html" title="Class file_mapping">boost::interprocess::file_mapping</a></code> 833 class reference. 834 </p> 835</div> 836<div class="section"> 837<div class="titlepage"><div><div><h4 class="title"> 838<a name="interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_mapping_regions"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_mapping_regions" title="Mapping File's Contents In Memory">Mapping 839 File's Contents In Memory</a> 840</h4></div></div></div> 841<p> 842 After creating a file mapping, a process just has to map the shared memory 843 in the process' address space. The user can map the whole shared memory 844 or just part of it. The mapping process is done using the <code class="computeroutput"><span class="identifier">mapped_region</span></code> class. as we have said 845 before The class represents a memory region that has been mapped from a 846 shared memory or from other devices that have also mapping capabilities: 847 </p> 848<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> 849<span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">FileSize</span> <span class="special">=</span> <span class="special">...</span> 850 851<span class="comment">//Map the second half of the file</span> 852<span class="identifier">mapped_region</span> <span class="identifier">region</span> 853 <span class="special">(</span> <span class="identifier">m_file</span> <span class="comment">//Memory-mappable object</span> 854 <span class="special">,</span> <span class="identifier">read_write</span> <span class="comment">//Access mode</span> 855 <span class="special">,</span> <span class="identifier">FileSize</span><span class="special">/</span><span class="number">2</span> <span class="comment">//Offset from the beginning of shm</span> 856 <span class="special">,</span> <span class="identifier">FileSize</span><span class="special">-</span><span class="identifier">FileSize</span><span class="special">/</span><span class="number">2</span> <span class="comment">//Length of the region</span> 857 <span class="special">);</span> 858 859<span class="comment">//Get the address of the region</span> 860<span class="identifier">region</span><span class="special">.</span><span class="identifier">get_address</span><span class="special">();</span> 861 862<span class="comment">//Get the size of the region</span> 863<span class="identifier">region</span><span class="special">.</span><span class="identifier">get_size</span><span class="special">();</span> 864</pre> 865<p> 866 The user can specify the offset from the file where the mapped region should 867 start and the size of the mapped region. If no offset or size is specified, 868 the whole file is mapped. If the offset is specified, but not the size, 869 the mapped region covers from the offset until the end of the file. 870 </p> 871<p> 872 If several processes map the same file, and a process modifies a memory 873 range from a mapped region that is also mapped by other process, the changes 874 are inmedially visible to other processes. However, the file contents on 875 disk are not updated immediately, since that would hurt performance (writing 876 to disk is several times slower than writing to memory). If the user wants 877 to make sure that file's contents have been updated, it can flush a range 878 from the view to disk. When the function returns, the flushing process 879 has started but there is no guarantee that all data has been written to 880 disk: 881 </p> 882<pre class="programlisting"><span class="comment">//Flush the whole region</span> 883<span class="identifier">region</span><span class="special">.</span><span class="identifier">flush</span><span class="special">();</span> 884 885<span class="comment">//Flush from an offset until the end of the region</span> 886<span class="identifier">region</span><span class="special">.</span><span class="identifier">flush</span><span class="special">(</span><span class="identifier">offset</span><span class="special">);</span> 887 888<span class="comment">//Flush a memory range starting on an offset</span> 889<span class="identifier">region</span><span class="special">.</span><span class="identifier">flush</span><span class="special">(</span><span class="identifier">offset</span><span class="special">,</span> <span class="identifier">size</span><span class="special">);</span> 890</pre> 891<p> 892 Remember that the offset is <span class="bold"><strong>not</strong></span> an offset 893 on the file, but an offset in the mapped region. If a region covers the 894 second half of a file and flushes the whole region, only the half of the 895 file is guaranteed to have been flushed. 896 </p> 897<p> 898 For more details regarding <code class="computeroutput"><span class="identifier">mapped_region</span></code> 899 see the <code class="computeroutput"><a class="link" href="../boost/interprocess/mapped_region.html" title="Class mapped_region">boost::interprocess::mapped_region</a></code> 900 class reference. 901 </p> 902</div> 903<div class="section"> 904<div class="titlepage"><div><div><h4 class="title"> 905<a name="interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_a_simple_example"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file.mapped_file_a_simple_example" title="A Simple Example">A 906 Simple Example</a> 907</h4></div></div></div> 908<p> 909 Let's reproduce the same example described in the shared memory section, 910 using memory mapped files. A server process creates a shared memory segment, 911 maps it and initializes all the bytes to a value. After that, a client 912 process opens the shared memory, maps it, and checks that the data is correctly 913 initialized:: 914 </p> 915<p> 916</p> 917<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">file_mapping</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 918<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> 919<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 920<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">fstream</span><span class="special">></span> 921<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 922<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">vector</span><span class="special">></span> 923<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstring</span><span class="special">></span> 924<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstddef</span><span class="special">></span> 925<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cstdlib</span><span class="special">></span> 926 927<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> 928<span class="special">{</span> 929 <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> 930 931 <span class="comment">//Define file names</span> 932 <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">FileName</span> <span class="special">=</span> <span class="string">"file.bin"</span><span class="special">;</span> 933 <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">FileSize</span> <span class="special">=</span> <span class="number">10000</span><span class="special">;</span> 934 935 <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 executes this</span> 936 <span class="special">{</span> <span class="comment">//Create a file</span> 937 <span class="identifier">file_mapping</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="identifier">FileName</span><span class="special">);</span> 938 <span class="identifier">std</span><span class="special">::</span><span class="identifier">filebuf</span> <span class="identifier">fbuf</span><span class="special">;</span> 939 <span class="identifier">fbuf</span><span class="special">.</span><span class="identifier">open</span><span class="special">(</span><span class="identifier">FileName</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">in</span> <span class="special">|</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">out</span> 940 <span class="special">|</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">trunc</span> <span class="special">|</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">binary</span><span class="special">);</span> 941 <span class="comment">//Set the size</span> 942 <span class="identifier">fbuf</span><span class="special">.</span><span class="identifier">pubseekoff</span><span class="special">(</span><span class="identifier">FileSize</span><span class="special">-</span><span class="number">1</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">beg</span><span class="special">);</span> 943 <span class="identifier">fbuf</span><span class="special">.</span><span class="identifier">sputc</span><span class="special">(</span><span class="number">0</span><span class="special">);</span> 944 <span class="special">}</span> 945 946 <span class="comment">//Remove on exit</span> 947 <span class="keyword">struct</span> <span class="identifier">file_remove</span> 948 <span class="special">{</span> 949 <span class="identifier">file_remove</span><span class="special">(</span><span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">FileName</span><span class="special">)</span> 950 <span class="special">:</span> <span class="identifier">FileName_</span><span class="special">(</span><span class="identifier">FileName</span><span class="special">)</span> <span class="special">{}</span> 951 <span class="special">~</span><span class="identifier">file_remove</span><span class="special">(){</span> <span class="identifier">file_mapping</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="identifier">FileName_</span><span class="special">);</span> <span class="special">}</span> 952 <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">FileName_</span><span class="special">;</span> 953 <span class="special">}</span> <span class="identifier">remover</span><span class="special">(</span><span class="identifier">FileName</span><span class="special">);</span> 954 955 <span class="comment">//Create a file mapping</span> 956 <span class="identifier">file_mapping</span> <span class="identifier">m_file</span><span class="special">(</span><span class="identifier">FileName</span><span class="special">,</span> <span class="identifier">read_write</span><span class="special">);</span> 957 958 <span class="comment">//Map the whole file with read-write permissions in this process</span> 959 <span class="identifier">mapped_region</span> <span class="identifier">region</span><span class="special">(</span><span class="identifier">m_file</span><span class="special">,</span> <span class="identifier">read_write</span><span class="special">);</span> 960 961 <span class="comment">//Get the address of the mapped region</span> 962 <span class="keyword">void</span> <span class="special">*</span> <span class="identifier">addr</span> <span class="special">=</span> <span class="identifier">region</span><span class="special">.</span><span class="identifier">get_address</span><span class="special">();</span> 963 <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span> <span class="special">=</span> <span class="identifier">region</span><span class="special">.</span><span class="identifier">get_size</span><span class="special">();</span> 964 965 <span class="comment">//Write all the memory to 1</span> 966 <span class="identifier">std</span><span class="special">::</span><span class="identifier">memset</span><span class="special">(</span><span class="identifier">addr</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">size</span><span class="special">);</span> 967 968 <span class="comment">//Launch child process</span> 969 <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> 970 <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> 971 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> 972 <span class="special">}</span> 973 <span class="keyword">else</span><span class="special">{</span> <span class="comment">//Child process executes this</span> 974 <span class="special">{</span> <span class="comment">//Open the file mapping and map it as read-only</span> 975 <span class="identifier">file_mapping</span> <span class="identifier">m_file</span><span class="special">(</span><span class="identifier">FileName</span><span class="special">,</span> <span class="identifier">read_only</span><span class="special">);</span> 976 977 <span class="identifier">mapped_region</span> <span class="identifier">region</span><span class="special">(</span><span class="identifier">m_file</span><span class="special">,</span> <span class="identifier">read_only</span><span class="special">);</span> 978 979 <span class="comment">//Get the address of the mapped region</span> 980 <span class="keyword">void</span> <span class="special">*</span> <span class="identifier">addr</span> <span class="special">=</span> <span class="identifier">region</span><span class="special">.</span><span class="identifier">get_address</span><span class="special">();</span> 981 <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span> <span class="special">=</span> <span class="identifier">region</span><span class="special">.</span><span class="identifier">get_size</span><span class="special">();</span> 982 983 <span class="comment">//Check that memory was initialized to 1</span> 984 <span class="keyword">const</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">addr</span><span class="special">);</span> 985 <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">size</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 986 <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> 987 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> <span class="comment">//Error checking memory</span> 988 <span class="special">}</span> 989 <span class="special">{</span> <span class="comment">//Now test it reading the file</span> 990 <span class="identifier">std</span><span class="special">::</span><span class="identifier">filebuf</span> <span class="identifier">fbuf</span><span class="special">;</span> 991 <span class="identifier">fbuf</span><span class="special">.</span><span class="identifier">open</span><span class="special">(</span><span class="identifier">FileName</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">in</span> <span class="special">|</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">binary</span><span class="special">);</span> 992 993 <span class="comment">//Read it to memory</span> 994 <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">char</span><span class="special">></span> <span class="identifier">vect</span><span class="special">(</span><span class="identifier">FileSize</span><span class="special">,</span> <span class="number">0</span><span class="special">);</span> 995 <span class="identifier">fbuf</span><span class="special">.</span><span class="identifier">sgetn</span><span class="special">(&</span><span class="identifier">vect</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">streamsize</span><span class="special">(</span><span class="identifier">vect</span><span class="special">.</span><span class="identifier">size</span><span class="special">()));</span> 996 997 <span class="comment">//Check that memory was initialized to 1</span> 998 <span class="keyword">const</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">vect</span><span class="special">[</span><span class="number">0</span><span class="special">]);</span> 999 <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">FileSize</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 1000 <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> 1001 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> <span class="comment">//Error checking memory</span> 1002 <span class="special">}</span> 1003 <span class="special">}</span> 1004 1005 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 1006<span class="special">}</span> 1007</pre> 1008<p> 1009 </p> 1010</div> 1011</div> 1012<div class="section"> 1013<div class="titlepage"><div><div><h3 class="title"> 1014<a name="interprocess.sharedmemorybetweenprocesses.mapped_region"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region" title="More About Mapped Regions">More 1015 About Mapped Regions</a> 1016</h3></div></div></div> 1017<div class="toc"><dl class="toc"> 1018<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_one_class">One 1019 Class To Rule Them All</a></span></dt> 1020<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_address_mapping">Mapping 1021 Address In Several Processes</a></span></dt> 1022<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_fixed_address_mapping">Fixed 1023 Address Mapping</a></span></dt> 1024<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_mapping_problems">Mapping 1025 Offset And Address Limitations</a></span></dt> 1026</dl></div> 1027<div class="section"> 1028<div class="titlepage"><div><div><h4 class="title"> 1029<a name="interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_one_class"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_one_class" title="One Class To Rule Them All">One 1030 Class To Rule Them All</a> 1031</h4></div></div></div> 1032<p> 1033 As we have seen, both <code class="computeroutput"><span class="identifier">shared_memory_object</span></code> 1034 and <code class="computeroutput"><span class="identifier">file_mapping</span></code> objects 1035 can be used to create <code class="computeroutput"><span class="identifier">mapped_region</span></code> 1036 objects. A mapped region created from a shared memory object or a file 1037 mapping are the same class and this has many advantages. 1038 </p> 1039<p> 1040 One can, for example, mix in STL containers mapped regions from shared 1041 memory and memory mapped files. Libraries that only depend on mapped regions 1042 can be used to work with shared memory or memory mapped files without recompiling 1043 them. 1044 </p> 1045</div> 1046<div class="section"> 1047<div class="titlepage"><div><div><h4 class="title"> 1048<a name="interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_address_mapping"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_address_mapping" title="Mapping Address In Several Processes">Mapping 1049 Address In Several Processes</a> 1050</h4></div></div></div> 1051<p> 1052 In the example we have seen, the file or shared memory contents are mapped 1053 to the address space of the process, but the address was chosen by the 1054 operating system. 1055 </p> 1056<p> 1057 If several processes map the same file/shared memory, the mapping address 1058 will be surely different in each process. Since each process might have 1059 used its address space in a different way (allocation of more or less dynamic 1060 memory, for example), there is no guarantee that the file/shared memory 1061 is going to be mapped in the same address. 1062 </p> 1063<p> 1064 If two processes map the same object in different addresses, this invalidates 1065 the use of pointers in that memory, since the pointer (which is an absolute 1066 address) would only make sense for the process that wrote it. The solution 1067 for this is to use offsets (distance) between objects instead of pointers: 1068 If two objects are placed in the same shared memory segment by one process, 1069 <span class="bold"><strong>the address of each object will be different</strong></span> 1070 in another process but <span class="bold"><strong>the distance between them 1071 (in bytes) will be the same</strong></span>. 1072 </p> 1073<p> 1074 So the first advice when mapping shared memory and memory mapped files 1075 is to avoid using raw pointers, unless you know what you are doing. Use 1076 offsets between data or relative pointers to obtain pointer functionality 1077 when an object placed in a mapped region wants to point to an object placed 1078 in the same mapped region. <span class="bold"><strong>Boost.Interprocess</strong></span> 1079 offers a smart pointer called <code class="computeroutput"><a class="link" href="../boost/interprocess/offset_ptr.html" title="Class template offset_ptr">boost::interprocess::offset_ptr</a></code> 1080 that can be safely placed in shared memory and that can be used to point 1081 to another object placed in the same shared memory / memory mapped file. 1082 </p> 1083</div> 1084<div class="section"> 1085<div class="titlepage"><div><div><h4 class="title"> 1086<a name="interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_fixed_address_mapping"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_fixed_address_mapping" title="Fixed Address Mapping">Fixed 1087 Address Mapping</a> 1088</h4></div></div></div> 1089<p> 1090 The use of relative pointers is less efficient than using raw pointers, 1091 so if a user can succeed mapping the same file or shared memory object 1092 in the same address in two processes, using raw pointers can be a good 1093 idea. 1094 </p> 1095<p> 1096 To map an object in a fixed address, the user can specify that address 1097 in the <code class="computeroutput"><span class="identifier">mapped</span> <span class="identifier">region</span></code>'s 1098 constructor: 1099 </p> 1100<pre class="programlisting"><span class="identifier">mapped_region</span> <span class="identifier">region</span> <span class="special">(</span> <span class="identifier">shm</span> <span class="comment">//Map shared memory</span> 1101 <span class="special">,</span> <span class="identifier">read_write</span> <span class="comment">//Map it as read-write</span> 1102 <span class="special">,</span> <span class="number">0</span> <span class="comment">//Map from offset 0</span> 1103 <span class="special">,</span> <span class="number">0</span> <span class="comment">//Map until the end</span> 1104 <span class="special">,</span> <span class="special">(</span><span class="keyword">void</span><span class="special">*)</span><span class="number">0x3F000000</span> <span class="comment">//Map it exactly there</span> 1105 <span class="special">);</span> 1106</pre> 1107<p> 1108 However, the user can't map the region in any address, even if the address 1109 is not being used. The offset parameter that marks the start of the mapping 1110 region is also limited. These limitations are explained in the next section. 1111 </p> 1112</div> 1113<div class="section"> 1114<div class="titlepage"><div><div><h4 class="title"> 1115<a name="interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_mapping_problems"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region.mapped_region_mapping_problems" title="Mapping Offset And Address Limitations">Mapping 1116 Offset And Address Limitations</a> 1117</h4></div></div></div> 1118<p> 1119 As mentioned, the user can't map the memory mappable object at any address 1120 and it can specify the offset of the mappable object that is equivalent 1121 to the start of the mapping region to an arbitrary value. Most operating 1122 systems limit the mapping address and the offset of the mappable object 1123 to a multiple of a value called <span class="bold"><strong>page size</strong></span>. 1124 This is due to the fact that the <span class="bold"><strong>operating system 1125 performs mapping operations over whole pages</strong></span>. 1126 </p> 1127<p> 1128 If fixed mapping address is used, <span class="emphasis"><em>offset</em></span> and <span class="emphasis"><em>address</em></span> 1129 parameters should be multiples of that value. This value is, typically, 1130 4KB or 8KB for 32 bit operating systems. 1131 </p> 1132<pre class="programlisting"><span class="comment">//These might fail because the offset is not a multiple of the page size</span> 1133<span class="comment">//and we are using fixed address mapping</span> 1134<span class="identifier">mapped_region</span> <span class="identifier">region1</span><span class="special">(</span> <span class="identifier">shm</span> <span class="comment">//Map shared memory</span> 1135 <span class="special">,</span> <span class="identifier">read_write</span> <span class="comment">//Map it as read-write</span> 1136 <span class="special">,</span> <span class="number">1</span> <span class="comment">//Map from offset 1</span> 1137 <span class="special">,</span> <span class="number">1</span> <span class="comment">//Map 1 byte</span> 1138 <span class="special">,</span> <span class="special">(</span><span class="keyword">void</span><span class="special">*)</span><span class="number">0x3F000000</span> <span class="comment">//Aligned mapping address</span> 1139 <span class="special">);</span> 1140 1141<span class="comment">//These might fail because the address is not a multiple of the page size</span> 1142<span class="identifier">mapped_region</span> <span class="identifier">region2</span><span class="special">(</span> <span class="identifier">shm</span> <span class="comment">//Map shared memory</span> 1143 <span class="special">,</span> <span class="identifier">read_write</span> <span class="comment">//Map it as read-write</span> 1144 <span class="special">,</span> <span class="number">0</span> <span class="comment">//Map from offset 0</span> 1145 <span class="special">,</span> <span class="number">1</span> <span class="comment">//Map 1 byte</span> 1146 <span class="special">,</span> <span class="special">(</span><span class="keyword">void</span><span class="special">*)</span><span class="number">0x3F000001</span> <span class="comment">//Not aligned mapping address</span> 1147 <span class="special">);</span> 1148</pre> 1149<p> 1150 Since the operating system performs mapping operations over whole pages, 1151 specifying a mapping <span class="emphasis"><em>size</em></span> or <span class="emphasis"><em>offset</em></span> 1152 that are not multiple of the page size will waste more resources than necessary. 1153 If the user specifies the following 1 byte mapping: 1154 </p> 1155<pre class="programlisting"><span class="comment">//Map one byte of the shared memory object.</span> 1156<span class="comment">//A whole memory page will be used for this.</span> 1157<span class="identifier">mapped_region</span> <span class="identifier">region</span> <span class="special">(</span> <span class="identifier">shm</span> <span class="comment">//Map shared memory</span> 1158 <span class="special">,</span> <span class="identifier">read_write</span> <span class="comment">//Map it as read-write</span> 1159 <span class="special">,</span> <span class="number">0</span> <span class="comment">//Map from offset 0</span> 1160 <span class="special">,</span> <span class="number">1</span> <span class="comment">//Map 1 byte</span> 1161 <span class="special">);</span> 1162</pre> 1163<p> 1164 The operating system will reserve a whole page that will not be reused 1165 by any other mapping so we are going to waste <span class="bold"><strong>(page 1166 size - 1)</strong></span> bytes. If we want to use efficiently operating system 1167 resources, we should create regions whose size is a multiple of <span class="bold"><strong>page size</strong></span> bytes. If the user specifies the following 1168 two mapped regions for a file with which has <code class="computeroutput"><span class="number">2</span><span class="special">*</span><span class="identifier">page_size</span></code> 1169 bytes: 1170 </p> 1171<pre class="programlisting"><span class="comment">//Map the first quarter of the file</span> 1172<span class="comment">//This will use a whole page</span> 1173<span class="identifier">mapped_region</span> <span class="identifier">region1</span><span class="special">(</span> <span class="identifier">shm</span> <span class="comment">//Map shared memory</span> 1174 <span class="special">,</span> <span class="identifier">read_write</span> <span class="comment">//Map it as read-write</span> 1175 <span class="special">,</span> <span class="number">0</span> <span class="comment">//Map from offset 0</span> 1176 <span class="special">,</span> <span class="identifier">page_size</span><span class="special">/</span><span class="number">2</span> <span class="comment">//Map page_size/2 bytes</span> 1177 <span class="special">);</span> 1178 1179<span class="comment">//Map the rest of the file</span> 1180<span class="comment">//This will use a 2 pages</span> 1181<span class="identifier">mapped_region</span> <span class="identifier">region2</span><span class="special">(</span> <span class="identifier">shm</span> <span class="comment">//Map shared memory</span> 1182 <span class="special">,</span> <span class="identifier">read_write</span> <span class="comment">//Map it as read-write</span> 1183 <span class="special">,</span> <span class="identifier">page_size</span><span class="special">/</span><span class="number">2</span> <span class="comment">//Map from offset 0</span> 1184 <span class="special">,</span> <span class="number">3</span><span class="special">*</span><span class="identifier">page_size</span><span class="special">/</span><span class="number">2</span> <span class="comment">//Map the rest of the shared memory</span> 1185 <span class="special">);</span> 1186</pre> 1187<p> 1188 In this example, a half of the page is wasted in the first mapping and 1189 another half is wasted in the second because the offset is not a multiple 1190 of the page size. The mapping with the minimum resource usage would be 1191 to map whole pages: 1192 </p> 1193<pre class="programlisting"><span class="comment">//Map the whole first half: uses 1 page</span> 1194<span class="identifier">mapped_region</span> <span class="identifier">region1</span><span class="special">(</span> <span class="identifier">shm</span> <span class="comment">//Map shared memory</span> 1195 <span class="special">,</span> <span class="identifier">read_write</span> <span class="comment">//Map it as read-write</span> 1196 <span class="special">,</span> <span class="number">0</span> <span class="comment">//Map from offset 0</span> 1197 <span class="special">,</span> <span class="identifier">page_size</span> <span class="comment">//Map a full page_size</span> 1198 <span class="special">);</span> 1199 1200<span class="comment">//Map the second half: uses 1 page</span> 1201<span class="identifier">mapped_region</span> <span class="identifier">region2</span><span class="special">(</span> <span class="identifier">shm</span> <span class="comment">//Map shared memory</span> 1202 <span class="special">,</span> <span class="identifier">read_write</span> <span class="comment">//Map it as read-write</span> 1203 <span class="special">,</span> <span class="identifier">page_size</span> <span class="comment">//Map from offset 0</span> 1204 <span class="special">,</span> <span class="identifier">page_size</span> <span class="comment">//Map the rest</span> 1205 <span class="special">);</span> 1206</pre> 1207<p> 1208 How can we obtain the <span class="bold"><strong>page size</strong></span>? The 1209 <code class="computeroutput"><span class="identifier">mapped_region</span></code> class has 1210 a static function that returns that value: 1211 </p> 1212<pre class="programlisting"><span class="comment">//Obtain the page size of the system</span> 1213<span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">page_size</span> <span class="special">=</span> <span class="identifier">mapped_region</span><span class="special">::</span><span class="identifier">get_page_size</span><span class="special">();</span> 1214</pre> 1215<p> 1216 The operating system might also limit the number of mapped memory regions 1217 per process or per system. 1218 </p> 1219</div> 1220</div> 1221<div class="section"> 1222<div class="titlepage"><div><div><h3 class="title"> 1223<a name="interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations" title="Limitations When Constructing Objects In Mapped Regions">Limitations 1224 When Constructing Objects In Mapped Regions</a> 1225</h3></div></div></div> 1226<div class="toc"><dl class="toc"> 1227<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.offset_pointer">Offset 1228 pointers instead of raw pointers</a></span></dt> 1229<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.references_forbidden">References 1230 forbidden</a></span></dt> 1231<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.virtuality_limitation">Virtuality 1232 forbidden</a></span></dt> 1233<dt><span class="section"><a href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.statics_warning">Be 1234 careful with static class members</a></span></dt> 1235</dl></div> 1236<p> 1237 When two processes create a mapped region of the same mappable object, two 1238 processes can communicate writing and reading that memory. A process could 1239 construct a C++ object in that memory so that the second process can use 1240 it. However, a mapped region shared by multiple processes, can't hold any 1241 C++ object, because not every class is ready to be a process-shared object, 1242 specially, if the mapped region is mapped in different address in each process. 1243 </p> 1244<div class="section"> 1245<div class="titlepage"><div><div><h4 class="title"> 1246<a name="interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.offset_pointer"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.offset_pointer" title="Offset pointers instead of raw pointers">Offset 1247 pointers instead of raw pointers</a> 1248</h4></div></div></div> 1249<p> 1250 When placing objects in a mapped region and mapping that region in different 1251 address in every process, raw pointers are a problem since they are only 1252 valid for the process that placed them there. To solve this, <span class="bold"><strong>Boost.Interprocess</strong></span> offers a special smart pointer 1253 that can be used instead of a raw pointer. So user classes containing raw 1254 pointers (or Boost smart pointers, that internally own a raw pointer) can't 1255 be safely placed in a process shared mapped region. These pointers must 1256 be replaced with offset pointers, and these pointers must point only to 1257 objects placed in the same mapped region if you want to use these shared 1258 objects from different processes. 1259 </p> 1260<p> 1261 Of course, a pointer placed in a mapped region shared between processes 1262 should only point to an object of that mapped region. Otherwise, the pointer 1263 would point to an address that it's only valid one process and other processes 1264 may crash when accessing to that address. 1265 </p> 1266</div> 1267<div class="section"> 1268<div class="titlepage"><div><div><h4 class="title"> 1269<a name="interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.references_forbidden"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.references_forbidden" title="References forbidden">References 1270 forbidden</a> 1271</h4></div></div></div> 1272<p> 1273 References suffer from the same problem as pointers (mainly because they 1274 are implemented as pointers). However, it is not possible to create a fully 1275 workable smart reference currently in C++ (for example, <code class="computeroutput"><span class="keyword">operator</span> 1276 <span class="special">.()</span></code> can't be overloaded). Because 1277 of this, if the user wants to put an object in shared memory, the object 1278 can't have any (smart or not) reference as a member. 1279 </p> 1280<p> 1281 References will only work if the mapped region is mapped in the same base 1282 address in all processes sharing a memory segment. Like pointers, a reference 1283 placed in a mapped region should only point to an object of that mapped 1284 region. 1285 </p> 1286</div> 1287<div class="section"> 1288<div class="titlepage"><div><div><h4 class="title"> 1289<a name="interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.virtuality_limitation"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.virtuality_limitation" title="Virtuality forbidden">Virtuality 1290 forbidden</a> 1291</h4></div></div></div> 1292<p> 1293 The virtual table pointer and the virtual table are in the address space 1294 of the process that constructs the object, so if we place a class with 1295 a virtual function or virtual base class, the virtual pointer placed in 1296 shared memory will be invalid for other processes and they will crash. 1297 </p> 1298<p> 1299 This problem is very difficult to solve, since each process needs a different 1300 virtual table pointer and the object that contains that pointer is shared 1301 across many processes. Even if we map the mapped region in the same address 1302 in every process, the virtual table can be in a different address in every 1303 process. To enable virtual functions for objects shared between processes, 1304 deep compiler changes are needed and virtual functions would suffer a performance 1305 hit. That's why <span class="bold"><strong>Boost.Interprocess</strong></span> does 1306 not have any plan to support virtual function and virtual inheritance in 1307 mapped regions shared between processes. 1308 </p> 1309</div> 1310<div class="section"> 1311<div class="titlepage"><div><div><h4 class="title"> 1312<a name="interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.statics_warning"></a><a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_region_object_limitations.statics_warning" title="Be careful with static class members">Be 1313 careful with static class members</a> 1314</h4></div></div></div> 1315<p> 1316 Static members of classes are global objects shared by all instances of 1317 the class. Because of this, static members are implemented as global variables 1318 in processes. 1319 </p> 1320<p> 1321 When constructing a class with static members, each process has its own 1322 copy of the static member, so updating a static member in one process does 1323 not change the value of the static member the another process. So be careful 1324 with these classes. Static members are not dangerous if they are just constant 1325 variables initialized when the process starts, but they don't change at 1326 all (for example, when used like enums) and their value is the same for 1327 all processes. 1328 </p> 1329</div> 1330</div> 1331</div> 1332<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 1333<td align="left"></td> 1334<td align="right"><div class="copyright-footer">Copyright © 2005-2015 Ion Gaztanaga<p> 1335 Distributed under the Boost Software License, Version 1.0. (See accompanying 1336 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>) 1337 </p> 1338</div></td> 1339</tr></table> 1340<hr> 1341<div class="spirit-nav"> 1342<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> 1343</div> 1344</body> 1345</html> 1346