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>Some basic explanations</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="quick_guide.html" title="Quick Guide for the Impatient"> 11<link rel="next" href="sharedmemorybetweenprocesses.html" title="Sharing memory between processes"> 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="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> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="interprocess.some_basic_explanations"></a><a class="link" href="some_basic_explanations.html" title="Some basic explanations">Some basic explanations</a> 29</h2></div></div></div> 30<div class="toc"><dl class="toc"> 31<dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.processes_and_threads">Processes 32 And Threads</a></span></dt> 33<dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.sharing_information">Sharing 34 information between processes</a></span></dt> 35<dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.persistence">Persistence 36 Of Interprocess Mechanisms</a></span></dt> 37<dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.names">Names Of 38 Interprocess Mechanisms</a></span></dt> 39<dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime">Constructors, 40 destructors and lifetime of Interprocess named resources</a></span></dt> 41<dt><span class="section"><a href="some_basic_explanations.html#interprocess.some_basic_explanations.permissions">Permissions</a></span></dt> 42</dl></div> 43<div class="section"> 44<div class="titlepage"><div><div><h3 class="title"> 45<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 And Threads</a> 47</h3></div></div></div> 48<p> 49 <span class="bold"><strong>Boost.Interprocess</strong></span> does not work only with 50 processes but also with threads. <span class="bold"><strong>Boost.Interprocess</strong></span> 51 synchronization mechanisms can synchronize threads from different processes, 52 but also threads from the same process. 53 </p> 54</div> 55<div class="section"> 56<div class="titlepage"><div><div><h3 class="title"> 57<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 58 information between processes</a> 59</h3></div></div></div> 60<p> 61 In the traditional programming model an operating system has multiple processes 62 running and each process has its own address space. To share information 63 between processes we have several alternatives: 64 </p> 65<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 66<li class="listitem"> 67 Two processes share information using a <span class="bold"><strong>file</strong></span>. 68 To access to the data, each process uses the usual file read/write mechanisms. 69 When updating/reading a file shared between processes, we need some sort 70 of synchronization, to protect readers from writers. 71 </li> 72<li class="listitem"> 73 Two processes share information that resides in the <span class="bold"><strong>kernel</strong></span> 74 of the operating system. This is the case, for example, of traditional 75 message queues. The synchronization is guaranteed by the operating system 76 kernel. 77 </li> 78<li class="listitem"> 79 Two processes can share a <span class="bold"><strong>memory</strong></span> region. 80 This is the case of classical shared memory or memory mapped files. Once 81 the processes set up the memory region, the processes can read/write 82 the data like any other memory segment without calling the operating 83 system's kernel. This also requires some kind of manual synchronization 84 between processes. 85 </li> 86</ul></div> 87</div> 88<div class="section"> 89<div class="titlepage"><div><div><h3 class="title"> 90<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 91 Of Interprocess Mechanisms</a> 92</h3></div></div></div> 93<p> 94 One of the biggest issues with interprocess communication mechanisms is the 95 lifetime of the interprocess communication mechanism. It's important to know 96 when an interprocess communication mechanism disappears from the system. 97 In <span class="bold"><strong>Boost.Interprocess</strong></span>, we can have 3 types 98 of persistence: 99 </p> 100<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 101<li class="listitem"> 102 <span class="bold"><strong>Process-persistence</strong></span>: The mechanism lasts 103 until all the processes that have opened the mechanism close it, exit 104 or crash. 105 </li> 106<li class="listitem"> 107 <span class="bold"><strong>Kernel-persistence</strong></span>: The mechanism exists 108 until the kernel of the operating system reboots or the mechanism is 109 explicitly deleted. 110 </li> 111<li class="listitem"> 112 <span class="bold"><strong>Filesystem-persistence</strong></span>: The mechanism 113 exists until the mechanism is explicitly deleted. 114 </li> 115</ul></div> 116<p> 117 Some native POSIX and Windows IPC mechanisms have different persistence so 118 it's difficult to achieve portability between Windows and POSIX native mechanisms. 119 <span class="bold"><strong>Boost.Interprocess</strong></span> classes have the following 120 persistence: 121 </p> 122<div class="table"> 123<a name="interprocess.some_basic_explanations.persistence.boost_interprocess_persistence_table"></a><p class="title"><b>Table 18.1. Boost.Interprocess Persistence Table</b></p> 124<div class="table-contents"><table class="table" summary="Boost.Interprocess Persistence Table"> 125<colgroup> 126<col> 127<col> 128</colgroup> 129<thead><tr> 130<th> 131 <p> 132 Mechanism 133 </p> 134 </th> 135<th> 136 <p> 137 Persistence 138 </p> 139 </th> 140</tr></thead> 141<tbody> 142<tr> 143<td> 144 <p> 145 Shared memory 146 </p> 147 </td> 148<td> 149 <p> 150 Kernel or Filesystem 151 </p> 152 </td> 153</tr> 154<tr> 155<td> 156 <p> 157 Memory mapped file 158 </p> 159 </td> 160<td> 161 <p> 162 Filesystem 163 </p> 164 </td> 165</tr> 166<tr> 167<td> 168 <p> 169 Process-shared mutex types 170 </p> 171 </td> 172<td> 173 <p> 174 Process 175 </p> 176 </td> 177</tr> 178<tr> 179<td> 180 <p> 181 Process-shared semaphore 182 </p> 183 </td> 184<td> 185 <p> 186 Process 187 </p> 188 </td> 189</tr> 190<tr> 191<td> 192 <p> 193 Process-shared condition 194 </p> 195 </td> 196<td> 197 <p> 198 Process 199 </p> 200 </td> 201</tr> 202<tr> 203<td> 204 <p> 205 File lock 206 </p> 207 </td> 208<td> 209 <p> 210 Process 211 </p> 212 </td> 213</tr> 214<tr> 215<td> 216 <p> 217 Message queue 218 </p> 219 </td> 220<td> 221 <p> 222 Kernel or Filesystem 223 </p> 224 </td> 225</tr> 226<tr> 227<td> 228 <p> 229 Named mutex 230 </p> 231 </td> 232<td> 233 <p> 234 Kernel or Filesystem 235 </p> 236 </td> 237</tr> 238<tr> 239<td> 240 <p> 241 Named semaphore 242 </p> 243 </td> 244<td> 245 <p> 246 Kernel or Filesystem 247 </p> 248 </td> 249</tr> 250<tr> 251<td> 252 <p> 253 Named condition 254 </p> 255 </td> 256<td> 257 <p> 258 Kernel or Filesystem 259 </p> 260 </td> 261</tr> 262</tbody> 263</table></div> 264</div> 265<br class="table-break"><p> 266 As you can see, <span class="bold"><strong>Boost.Interprocess</strong></span> defines 267 some mechanisms with "Kernel or Filesystem" persistence. This is 268 because POSIX allows this possibility to native interprocess communication 269 implementations. One could, for example, implement shared memory using memory 270 mapped files and obtain filesystem persistence (for example, there is no 271 proper known way to emulate kernel persistence with a user library for Windows 272 shared memory using native shared memory, or process persistence for POSIX 273 shared memory, so the only portable way is to define "Kernel or Filesystem" 274 persistence). 275 </p> 276</div> 277<div class="section"> 278<div class="titlepage"><div><div><h3 class="title"> 279<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 280 Interprocess Mechanisms</a> 281</h3></div></div></div> 282<p> 283 Some interprocess mechanisms are anonymous objects created in shared memory 284 or memory-mapped files but other interprocess mechanisms need a name or identifier 285 so that two unrelated processes can use the same interprocess mechanism object. 286 Examples of this are shared memory, named mutexes and named semaphores (for 287 example, native windows CreateMutex/CreateSemaphore API family). 288 </p> 289<p> 290 The name used to identify an interprocess mechanism is not portable, even 291 between UNIX systems. For this reason, <span class="bold"><strong>Boost.Interprocess</strong></span> 292 limits this name to a C++ variable identifier or keyword: 293 </p> 294<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 295<li class="listitem"> 296 Starts with a letter, lowercase or uppercase, such as a letter from a 297 to z or from A to Z. Examples: <span class="emphasis"><em>Sharedmemory, sharedmemory, 298 sHaReDmEmOrY...</em></span> 299 </li> 300<li class="listitem"> 301 Can include letters, underscore, or digits. Examples: <span class="emphasis"><em>shm1, 302 shm2and3, ShM3plus4...</em></span> 303 </li> 304</ul></div> 305</div> 306<div class="section"> 307<div class="titlepage"><div><div><h3 class="title"> 308<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, 309 destructors and lifetime of Interprocess named resources</a> 310</h3></div></div></div> 311<p> 312 Named <span class="bold"><strong>Boost.Interprocess</strong></span> resources (shared 313 memory, memory mapped files, named mutexes/conditions/semaphores) have kernel 314 or filesystem persistency. This means that even if all processes that have 315 opened those resources end, the resource will still be accessible to be opened 316 again and the resource can only be destructed via an explicit call to their 317 static member <code class="computeroutput"><span class="identifier">remove</span></code> function. 318 This behavior can be easily understood, since it's the same mechanism used 319 by functions controlling file opening/creation/erasure: 320 </p> 321<div class="table"> 322<a name="interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime.boost_interprocess_filesystem_analogy"></a><p class="title"><b>Table 18.2. Boost.Interprocess-Filesystem Analogy</b></p> 323<div class="table-contents"><table class="table" summary="Boost.Interprocess-Filesystem Analogy"> 324<colgroup> 325<col> 326<col> 327<col> 328</colgroup> 329<thead><tr> 330<th> 331 <p> 332 Named Interprocess resource 333 </p> 334 </th> 335<th> 336 <p> 337 Corresponding std file 338 </p> 339 </th> 340<th> 341 <p> 342 Corresponding POSIX operation 343 </p> 344 </th> 345</tr></thead> 346<tbody> 347<tr> 348<td> 349 <p> 350 Constructor 351 </p> 352 </td> 353<td> 354 <p> 355 std::fstream constructor 356 </p> 357 </td> 358<td> 359 <p> 360 open 361 </p> 362 </td> 363</tr> 364<tr> 365<td> 366 <p> 367 Destructor 368 </p> 369 </td> 370<td> 371 <p> 372 std::fstream destructor 373 </p> 374 </td> 375<td> 376 <p> 377 close 378 </p> 379 </td> 380</tr> 381<tr> 382<td> 383 <p> 384 Member <code class="computeroutput"><span class="identifier">remove</span></code> 385 </p> 386 </td> 387<td> 388 <p> 389 None. <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">remove</span></code> 390 </p> 391 </td> 392<td> 393 <p> 394 unlink 395 </p> 396 </td> 397</tr> 398</tbody> 399</table></div> 400</div> 401<br class="table-break"><p> 402 Now the correspondence between POSIX and Boost.Interprocess regarding shared 403 memory and named semaphores: 404 </p> 405<div class="table"> 406<a name="interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime.boost_interprocess_posix_shared_memory"></a><p class="title"><b>Table 18.3. Boost.Interprocess-POSIX shared memory</b></p> 407<div class="table-contents"><table class="table" summary="Boost.Interprocess-POSIX shared memory"> 408<colgroup> 409<col> 410<col> 411</colgroup> 412<thead><tr> 413<th> 414 <p> 415 <code class="computeroutput"><span class="identifier">shared_memory_object</span></code> 416 operation 417 </p> 418 </th> 419<th> 420 <p> 421 POSIX operation 422 </p> 423 </th> 424</tr></thead> 425<tbody> 426<tr> 427<td> 428 <p> 429 Constructor 430 </p> 431 </td> 432<td> 433 <p> 434 shm_open 435 </p> 436 </td> 437</tr> 438<tr> 439<td> 440 <p> 441 Destructor 442 </p> 443 </td> 444<td> 445 <p> 446 close 447 </p> 448 </td> 449</tr> 450<tr> 451<td> 452 <p> 453 Member <code class="computeroutput"><span class="identifier">remove</span></code> 454 </p> 455 </td> 456<td> 457 <p> 458 shm_unlink 459 </p> 460 </td> 461</tr> 462</tbody> 463</table></div> 464</div> 465<br class="table-break"><div class="table"> 466<a name="interprocess.some_basic_explanations.constructors_destructors_and_resource_lifetime.boost_interprocess_posix_named_semaphore"></a><p class="title"><b>Table 18.4. Boost.Interprocess-POSIX named semaphore</b></p> 467<div class="table-contents"><table class="table" summary="Boost.Interprocess-POSIX named semaphore"> 468<colgroup> 469<col> 470<col> 471</colgroup> 472<thead><tr> 473<th> 474 <p> 475 <code class="computeroutput"><span class="identifier">named_semaphore</span></code> 476 operation 477 </p> 478 </th> 479<th> 480 <p> 481 POSIX operation 482 </p> 483 </th> 484</tr></thead> 485<tbody> 486<tr> 487<td> 488 <p> 489 Constructor 490 </p> 491 </td> 492<td> 493 <p> 494 sem_open 495 </p> 496 </td> 497</tr> 498<tr> 499<td> 500 <p> 501 Destructor 502 </p> 503 </td> 504<td> 505 <p> 506 close 507 </p> 508 </td> 509</tr> 510<tr> 511<td> 512 <p> 513 Member <code class="computeroutput"><span class="identifier">remove</span></code> 514 </p> 515 </td> 516<td> 517 <p> 518 sem_unlink 519 </p> 520 </td> 521</tr> 522</tbody> 523</table></div> 524</div> 525<br class="table-break"><p> 526 The most important property is that <span class="bold"><strong>destructors of 527 named resources don't remove the resource from the system</strong></span>, they 528 only liberate resources allocated by the system for use by the process for 529 the named resource. <span class="bold"><strong>To remove the resource from the 530 system the programmer must use <code class="computeroutput"><span class="identifier">remove</span></code></strong></span>. 531 </p> 532</div> 533<div class="section"> 534<div class="titlepage"><div><div><h3 class="title"> 535<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> 536</h3></div></div></div> 537<p> 538 Named resources offered by <span class="bold"><strong>Boost.Interprocess</strong></span> 539 must cope with platform-dependant permission issues also present when creating 540 files. If a programmer wants to shared shared memory, memory mapped files 541 or named synchronization mechanisms (mutexes, semaphores, etc...) between 542 users, it's necessary to specify those permissions. Sadly, traditional UNIX 543 and Windows permissions are very different and <span class="bold"><strong>Boost.Interprocess</strong></span> 544 does not try to standardize permissions, but does not ignore them. 545 </p> 546<p> 547 All named resource creation functions take an optional <code class="computeroutput"><a class="link" href="../boost/interprocess/permissions.html" title="Class permissions">permissions 548 object</a></code> that can be configured with platform-dependant permissions. 549 </p> 550<p> 551 Since each mechanism can be emulated through different mechanisms (a semaphore 552 might be implement using mapped files or native semaphores) permissions types 553 could vary when the implementation of a named resource changes (eg.: in Windows 554 mutexes require <code class="computeroutput"><span class="identifier">synchronize</span> <span class="identifier">permissions</span></code>, but that's not the case of 555 files). To avoid this, <span class="bold"><strong>Boost.Interprocess</strong></span> 556 relies on file-like permissions, requiring file read-write-delete permissions 557 to open named synchronization mechanisms (mutex, semaphores, etc.) and appropriate 558 read or read-write-delete permissions for shared memory. This approach has 559 two advantages: it's similar to the UNIX philosophy and the programmer does 560 not need to know how the named resource is implemented. 561 </p> 562</div> 563</div> 564<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 565<td align="left"></td> 566<td align="right"><div class="copyright-footer">Copyright © 2005-2015 Ion Gaztanaga<p> 567 Distributed under the Boost Software License, Version 1.0. (See accompanying 568 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>) 569 </p> 570</div></td> 571</tr></table> 572<hr> 573<div class="spirit-nav"> 574<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> 575</div> 576</body> 577</html> 578