Imported Upstream version 1.57.0
[platform/upstream/boost.git] / doc / html / interprocess / some_basic_explanations.html
1 <html>
2 <head>
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&#160;14.&#160;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">
11 </head>
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
15 <td align="center"><a href="../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../more/index.htm">More</a></td>
20 </tr></table>
21 <hr>
22 <div class="spirit-nav">
23 <a accesskey="p" href="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>
24 </div>
25 <div class="section">
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>
41 </dl></div>
42 <div class="section">
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
45       And Threads</a>
46 </h3></div></div></div>
47 <p>
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.
52       </p>
53 </div>
54 <div class="section">
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>
59 <p>
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:
63       </p>
64 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
65 <li class="listitem">
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.
70           </li>
71 <li class="listitem">
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
75             kernel.
76           </li>
77 <li class="listitem">
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
83             between processes.
84           </li>
85 </ul></div>
86 </div>
87 <div class="section">
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>
92 <p>
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
97         of persistence:
98       </p>
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
103             or crash.
104           </li>
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
108             explicitly deleted.
109           </li>
110 <li class="listitem">
111             <span class="bold"><strong>Filesystem-persistence</strong></span>: The mechanism
112             exists until the mechanism is explicitly deleted.
113           </li>
114 </ul></div>
115 <p>
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
119         persistence:
120       </p>
121 <div class="table">
122 <a name="interprocess.some_basic_explanations.persistence.t0"></a><p class="title"><b>Table&#160;14.1.&#160;Boost.Interprocess Persistence Table</b></p>
123 <div class="table-contents"><table class="table" summary="Boost.Interprocess Persistence Table">
124 <colgroup>
125 <col>
126 <col>
127 </colgroup>
128 <thead><tr>
129 <th>
130                 <p>
131                   Mechanism
132                 </p>
133               </th>
134 <th>
135                 <p>
136                   Persistence
137                 </p>
138               </th>
139 </tr></thead>
140 <tbody>
141 <tr>
142 <td>
143                 <p>
144                   Shared memory
145                 </p>
146               </td>
147 <td>
148                 <p>
149                   Kernel or Filesystem
150                 </p>
151               </td>
152 </tr>
153 <tr>
154 <td>
155                 <p>
156                   Memory mapped file
157                 </p>
158               </td>
159 <td>
160                 <p>
161                   Filesystem
162                 </p>
163               </td>
164 </tr>
165 <tr>
166 <td>
167                 <p>
168                   Process-shared mutex types
169                 </p>
170               </td>
171 <td>
172                 <p>
173                   Process
174                 </p>
175               </td>
176 </tr>
177 <tr>
178 <td>
179                 <p>
180                   Process-shared semaphore
181                 </p>
182               </td>
183 <td>
184                 <p>
185                   Process
186                 </p>
187               </td>
188 </tr>
189 <tr>
190 <td>
191                 <p>
192                   Process-shared condition
193                 </p>
194               </td>
195 <td>
196                 <p>
197                   Process
198                 </p>
199               </td>
200 </tr>
201 <tr>
202 <td>
203                 <p>
204                   File lock
205                 </p>
206               </td>
207 <td>
208                 <p>
209                   Process
210                 </p>
211               </td>
212 </tr>
213 <tr>
214 <td>
215                 <p>
216                   Message queue
217                 </p>
218               </td>
219 <td>
220                 <p>
221                   Kernel or Filesystem
222                 </p>
223               </td>
224 </tr>
225 <tr>
226 <td>
227                 <p>
228                   Named mutex
229                 </p>
230               </td>
231 <td>
232                 <p>
233                   Kernel or Filesystem
234                 </p>
235               </td>
236 </tr>
237 <tr>
238 <td>
239                 <p>
240                   Named semaphore
241                 </p>
242               </td>
243 <td>
244                 <p>
245                   Kernel or Filesystem
246                 </p>
247               </td>
248 </tr>
249 <tr>
250 <td>
251                 <p>
252                   Named condition
253                 </p>
254               </td>
255 <td>
256                 <p>
257                   Kernel or Filesystem
258                 </p>
259               </td>
260 </tr>
261 </tbody>
262 </table></div>
263 </div>
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"
273         persistence).
274       </p>
275 </div>
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>
281 <p>
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).
287       </p>
288 <p>
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:
292       </p>
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>
298           </li>
299 <li class="listitem">
300             Can include letters, underscore, or digits. Examples: <span class="emphasis"><em>shm1,
301             shm2and3, ShM3plus4...</em></span>
302           </li>
303 </ul></div>
304 </div>
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>
310 <p>
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:
319       </p>
320 <div class="table">
321 <a name="interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime.t0"></a><p class="title"><b>Table&#160;14.2.&#160;Boost.Interprocess-Filesystem Analogy</b></p>
322 <div class="table-contents"><table class="table" summary="Boost.Interprocess-Filesystem Analogy">
323 <colgroup>
324 <col>
325 <col>
326 <col>
327 </colgroup>
328 <thead><tr>
329 <th>
330                 <p>
331                   Named Interprocess resource
332                 </p>
333               </th>
334 <th>
335                 <p>
336                   Corresponding std file
337                 </p>
338               </th>
339 <th>
340                 <p>
341                   Corresponding POSIX operation
342                 </p>
343               </th>
344 </tr></thead>
345 <tbody>
346 <tr>
347 <td>
348                 <p>
349                   Constructor
350                 </p>
351               </td>
352 <td>
353                 <p>
354                   std::fstream constructor
355                 </p>
356               </td>
357 <td>
358                 <p>
359                   open
360                 </p>
361               </td>
362 </tr>
363 <tr>
364 <td>
365                 <p>
366                   Destructor
367                 </p>
368               </td>
369 <td>
370                 <p>
371                   std::fstream destructor
372                 </p>
373               </td>
374 <td>
375                 <p>
376                   close
377                 </p>
378               </td>
379 </tr>
380 <tr>
381 <td>
382                 <p>
383                   Member <code class="computeroutput"><span class="identifier">remove</span></code>
384                 </p>
385               </td>
386 <td>
387                 <p>
388                   None. <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">remove</span></code>
389                 </p>
390               </td>
391 <td>
392                 <p>
393                   unlink
394                 </p>
395               </td>
396 </tr>
397 </tbody>
398 </table></div>
399 </div>
400 <br class="table-break"><p>
401         Now the correspondence between POSIX and Boost.Interprocess regarding shared
402         memory and named semaphores:
403       </p>
404 <div class="table">
405 <a name="interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime.t1"></a><p class="title"><b>Table&#160;14.3.&#160;Boost.Interprocess-POSIX shared memory</b></p>
406 <div class="table-contents"><table class="table" summary="Boost.Interprocess-POSIX shared memory">
407 <colgroup>
408 <col>
409 <col>
410 </colgroup>
411 <thead><tr>
412 <th>
413                 <p>
414                   <code class="computeroutput"><span class="identifier">shared_memory_object</span></code>
415                   operation
416                 </p>
417               </th>
418 <th>
419                 <p>
420                   POSIX operation
421                 </p>
422               </th>
423 </tr></thead>
424 <tbody>
425 <tr>
426 <td>
427                 <p>
428                   Constructor
429                 </p>
430               </td>
431 <td>
432                 <p>
433                   shm_open
434                 </p>
435               </td>
436 </tr>
437 <tr>
438 <td>
439                 <p>
440                   Destructor
441                 </p>
442               </td>
443 <td>
444                 <p>
445                   close
446                 </p>
447               </td>
448 </tr>
449 <tr>
450 <td>
451                 <p>
452                   Member <code class="computeroutput"><span class="identifier">remove</span></code>
453                 </p>
454               </td>
455 <td>
456                 <p>
457                   shm_unlink
458                 </p>
459               </td>
460 </tr>
461 </tbody>
462 </table></div>
463 </div>
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&#160;14.4.&#160;Boost.Interprocess-POSIX named semaphore</b></p>
466 <div class="table-contents"><table class="table" summary="Boost.Interprocess-POSIX named semaphore">
467 <colgroup>
468 <col>
469 <col>
470 </colgroup>
471 <thead><tr>
472 <th>
473                 <p>
474                   <code class="computeroutput"><span class="identifier">named_semaphore</span></code>
475                   operation
476                 </p>
477               </th>
478 <th>
479                 <p>
480                   POSIX operation
481                 </p>
482               </th>
483 </tr></thead>
484 <tbody>
485 <tr>
486 <td>
487                 <p>
488                   Constructor
489                 </p>
490               </td>
491 <td>
492                 <p>
493                   sem_open
494                 </p>
495               </td>
496 </tr>
497 <tr>
498 <td>
499                 <p>
500                   Destructor
501                 </p>
502               </td>
503 <td>
504                 <p>
505                   close
506                 </p>
507               </td>
508 </tr>
509 <tr>
510 <td>
511                 <p>
512                   Member <code class="computeroutput"><span class="identifier">remove</span></code>
513                 </p>
514               </td>
515 <td>
516                 <p>
517                   sem_unlink
518                 </p>
519               </td>
520 </tr>
521 </tbody>
522 </table></div>
523 </div>
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>.
530       </p>
531 </div>
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>
536 <p>
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.
544       </p>
545 <p>
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.
548       </p>
549 <p>
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.
560       </p>
561 </div>
562 </div>
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 &#169; 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>)
568       </p>
569 </div></td>
570 </tr></table>
571 <hr>
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>
574 </div>
575 </body>
576 </html>