3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Some basic explanations</title>
5 <link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
7 <link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
8 <link rel="up" href="../interprocess.html" title="Chapter 14. Boost.Interprocess">
9 <link rel="prev" href="quick_guide.html" title="Quick Guide for the Impatient">
10 <link rel="next" href="sharedmemorybetweenprocesses.html" title="Sharing memory between processes">
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
15 <td align="center"><a href="../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../more/index.htm">More</a></td>
22 <div class="spirit-nav">
23 <a accesskey="p" href="quick_guide.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="sharedmemorybetweenprocesses.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="interprocess.some_basic_explanations"></a><a class="link" href="some_basic_explanations.html" title="Some basic explanations">Some basic explanations</a>
28 </h2></div></div></div>
29 <div class="toc"><dl class="toc">
30 <dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.processes_and_threads">Processes
31 And Threads</a></span></dt>
32 <dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.sharing_information">Sharing
33 information between processes</a></span></dt>
34 <dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.persistence">Persistence
35 Of Interprocess Mechanisms</a></span></dt>
36 <dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.names">Names Of
37 Interprocess Mechanisms</a></span></dt>
38 <dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime">Constructors,
39 destructors and lifetime of Interprocess named resources</a></span></dt>
40 <dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.permissions">Permissions</a></span></dt>
43 <div class="titlepage"><div><div><h3 class="title">
44 <a name="interprocess.some_basic_explanations.processes_and_threads"></a><a class="link" href="some_basic_explanations.html#interprocess.some_basic_explanations.processes_and_threads" title="Processes And Threads">Processes
46 </h3></div></div></div>
48 <span class="bold"><strong>Boost.Interprocess</strong></span> does not work only with
49 processes but also with threads. <span class="bold"><strong>Boost.Interprocess</strong></span>
50 synchronization mechanisms can synchronize threads from different processes,
51 but also threads from the same process.
55 <div class="titlepage"><div><div><h3 class="title">
56 <a name="interprocess.some_basic_explanations.sharing_information"></a><a class="link" href="some_basic_explanations.html#interprocess.some_basic_explanations.sharing_information" title="Sharing information between processes">Sharing
57 information between processes</a>
58 </h3></div></div></div>
60 In the traditional programming model an operating system has multiple processes
61 running and each process has its own address space. To share information
62 between processes we have several alternatives:
64 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
66 Two processes share information using a <span class="bold"><strong>file</strong></span>.
67 To access to the data, each process uses the usual file read/write mechanisms.
68 When updating/reading a file shared between processes, we need some sort
69 of synchronization, to protect readers from writers.
72 Two processes share information that resides in the <span class="bold"><strong>kernel</strong></span>
73 of the operating system. This is the case, for example, of traditional
74 message queues. The synchronization is guaranteed by the operating system
78 Two processes can share a <span class="bold"><strong>memory</strong></span> region.
79 This is the case of classical shared memory or memory mapped files. Once
80 the processes set up the memory region, the processes can read/write
81 the data like any other memory segment without calling the operating
82 system's kernel. This also requires some kind of manual synchronization
88 <div class="titlepage"><div><div><h3 class="title">
89 <a name="interprocess.some_basic_explanations.persistence"></a><a class="link" href="some_basic_explanations.html#interprocess.some_basic_explanations.persistence" title="Persistence Of Interprocess Mechanisms">Persistence
90 Of Interprocess Mechanisms</a>
91 </h3></div></div></div>
93 One of the biggest issues with interprocess communication mechanisms is the
94 lifetime of the interprocess communication mechanism. It's important to know
95 when an interprocess communication mechanism disappears from the system.
96 In <span class="bold"><strong>Boost.Interprocess</strong></span>, we can have 3 types
99 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
100 <li class="listitem">
101 <span class="bold"><strong>Process-persistence</strong></span>: The mechanism lasts
102 until all the processes that have opened the mechanism close it, exit
105 <li class="listitem">
106 <span class="bold"><strong>Kernel-persistence</strong></span>: The mechanism exists
107 until the kernel of the operating system reboots or the mechanism is
110 <li class="listitem">
111 <span class="bold"><strong>Filesystem-persistence</strong></span>: The mechanism
112 exists until the mechanism is explicitly deleted.
116 Some native POSIX and Windows IPC mechanisms have different persistence so
117 it's difficult to achieve portability between Windows and POSIX native mechanisms.
118 <span class="bold"><strong>Boost.Interprocess</strong></span> classes have the following
122 <a name="interprocess.some_basic_explanations.persistence.t0"></a><p class="title"><b>Table 14.1. Boost.Interprocess Persistence Table</b></p>
123 <div class="table-contents"><table class="table" summary="Boost.Interprocess Persistence Table">
168 Process-shared mutex types
180 Process-shared semaphore
192 Process-shared condition
264 <br class="table-break"><p>
265 As you can see, <span class="bold"><strong>Boost.Interprocess</strong></span> defines
266 some mechanisms with "Kernel or Filesystem" persistence. This is
267 because POSIX allows this possibility to native interprocess communication
268 implementations. One could, for example, implement shared memory using memory
269 mapped files and obtain filesystem persistence (for example, there is no
270 proper known way to emulate kernel persistence with a user library for Windows
271 shared memory using native shared memory, or process persistence for POSIX
272 shared memory, so the only portable way is to define "Kernel or Filesystem"
276 <div class="section">
277 <div class="titlepage"><div><div><h3 class="title">
278 <a name="interprocess.some_basic_explanations.names"></a><a class="link" href="some_basic_explanations.html#interprocess.some_basic_explanations.names" title="Names Of Interprocess Mechanisms">Names Of
279 Interprocess Mechanisms</a>
280 </h3></div></div></div>
282 Some interprocess mechanisms are anonymous objects created in shared memory
283 or memory-mapped files but other interprocess mechanisms need a name or identifier
284 so that two unrelated processes can use the same interprocess mechanism object.
285 Examples of this are shared memory, named mutexes and named semaphores (for
286 example, native windows CreateMutex/CreateSemaphore API family).
289 The name used to identify an interprocess mechanism is not portable, even
290 between UNIX systems. For this reason, <span class="bold"><strong>Boost.Interprocess</strong></span>
291 limits this name to a C++ variable identifier or keyword:
293 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
294 <li class="listitem">
295 Starts with a letter, lowercase or uppercase, such as a letter from a
296 to z or from A to Z. Examples: <span class="emphasis"><em>Sharedmemory, sharedmemory,
297 sHaReDmEmOrY...</em></span>
299 <li class="listitem">
300 Can include letters, underscore, or digits. Examples: <span class="emphasis"><em>shm1,
301 shm2and3, ShM3plus4...</em></span>
305 <div class="section">
306 <div class="titlepage"><div><div><h3 class="title">
307 <a name="interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime"></a><a class="link" href="some_basic_explanations.html#interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime" title="Constructors, destructors and lifetime of Interprocess named resources">Constructors,
308 destructors and lifetime of Interprocess named resources</a>
309 </h3></div></div></div>
311 Named <span class="bold"><strong>Boost.Interprocess</strong></span> resources (shared
312 memory, memory mapped files, named mutexes/conditions/semaphores) have kernel
313 or filesystem persistency. This means that even if all processes that have
314 opened those resources end, the resource will still be accessible to be opened
315 again and the resource can only be destructed via an explicit call to their
316 static member <code class="computeroutput"><span class="identifier">remove</span></code> function.
317 This behavior can be easily understood, since it's the same mechanism used
318 by functions controlling file opening/creation/erasure:
321 <a name="interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime.t0"></a><p class="title"><b>Table 14.2. Boost.Interprocess-Filesystem Analogy</b></p>
322 <div class="table-contents"><table class="table" summary="Boost.Interprocess-Filesystem Analogy">
331 Named Interprocess resource
336 Corresponding std file
341 Corresponding POSIX operation
354 std::fstream constructor
371 std::fstream destructor
383 Member <code class="computeroutput"><span class="identifier">remove</span></code>
388 None. <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">remove</span></code>
400 <br class="table-break"><p>
401 Now the correspondence between POSIX and Boost.Interprocess regarding shared
402 memory and named semaphores:
405 <a name="interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime.t1"></a><p class="title"><b>Table 14.3. Boost.Interprocess-POSIX shared memory</b></p>
406 <div class="table-contents"><table class="table" summary="Boost.Interprocess-POSIX shared memory">
414 <code class="computeroutput"><span class="identifier">shared_memory_object</span></code>
452 Member <code class="computeroutput"><span class="identifier">remove</span></code>
464 <br class="table-break"><div class="table">
465 <a name="interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime.t2"></a><p class="title"><b>Table 14.4. Boost.Interprocess-POSIX named semaphore</b></p>
466 <div class="table-contents"><table class="table" summary="Boost.Interprocess-POSIX named semaphore">
474 <code class="computeroutput"><span class="identifier">named_semaphore</span></code>
512 Member <code class="computeroutput"><span class="identifier">remove</span></code>
524 <br class="table-break"><p>
525 The most important property is that <span class="bold"><strong>destructors of
526 named resources don't remove the resource from the system</strong></span>, they
527 only liberate resources allocated by the system for use by the process for
528 the named resource. <span class="bold"><strong>To remove the resource from the
529 system the programmer must use <code class="computeroutput"><span class="identifier">remove</span></code></strong></span>.
532 <div class="section">
533 <div class="titlepage"><div><div><h3 class="title">
534 <a name="interprocess.some_basic_explanations.permissions"></a><a class="link" href="some_basic_explanations.html#interprocess.some_basic_explanations.permissions" title="Permissions">Permissions</a>
535 </h3></div></div></div>
537 Named resources offered by <span class="bold"><strong>Boost.Interprocess</strong></span>
538 must cope with platform-dependant permission issues also present when creating
539 files. If a programmer wants to shared shared memory, memory mapped files
540 or named synchronization mechanisms (mutexes, semaphores, etc...) between
541 users, it's necessary to specify those permissions. Sadly, traditional UNIX
542 and Windows permissions are very different and <span class="bold"><strong>Boost.Interprocess</strong></span>
543 does not try to standardize permissions, but does not ignore them.
546 All named resource creation functions take an optional <code class="computeroutput"><a class="link" href="../boost/interprocess/permissions.html" title="Class permissions">permissions
547 object</a></code> that can be configured with platform-dependant permissions.
550 Since each mechanism can be emulated through diferent mechanisms (a semaphore
551 might be implement using mapped files or native semaphores) permissions types
552 could vary when the implementation of a named resource changes (eg.: in Windows
553 mutexes require <code class="computeroutput"><span class="identifier">synchronize</span> <span class="identifier">permissions</span></code>, but that's not the case of
554 files). To avoid this, <span class="bold"><strong>Boost.Interprocess</strong></span>
555 relies on file-like permissions, requiring file read-write-delete permissions
556 to open named synchronization mechanisms (mutex, semaphores, etc.) and appropiate
557 read or read-write-delete permissions for shared memory. This approach has
558 two advantages: it's similar to the UNIX philosophy and the programmer does
559 not need to know how the named resource is implemented.
563 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
564 <td align="left"></td>
565 <td align="right"><div class="copyright-footer">Copyright © 2005-2012 Ion Gaztanaga<p>
566 Distributed under the Boost Software License, Version 1.0. (See accompanying
567 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>)
572 <div class="spirit-nav">
573 <a accesskey="p" href="quick_guide.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="sharedmemorybetweenprocesses.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>