Note that there are some explanatory texts on larger screens.

plurals
  1. POPosix shared memory initialization
    primarykey
    data
    text
    <p>My question is regarding initializing memory obtained from using <code>shm_open()</code> and <code>mmap()</code>. One common advice I have seen in several places is to call <code>shm_open()</code> with flags <code>O_CREAT|O_EXCL</code>: if that succeeds then we are the first user of the shared memory and can initialize it, otherwise we are not the first and the shared memory has already been initialized by another process.</p> <p>However, from what I understand about <code>shm_open</code> and from the testing that I did on Linux, this wouldn't work: the shared memory objects get left over in the system, even after the last user of the shared memory object has unmapped and closed. A simple test program which calls <code>shm_open</code> with <code>O_CREAT|O_EXCL</code>, then closes the descriptor and exit, will succeed on the first run, but will still fail on the second run, even though nobody else is using the shared memory at that time.</p> <p>It actually seems to me that (at least on the system that I tested) the behavior of <code>shm_open</code> is pretty much identical to <code>open()</code>: if I modify my simple test program to write something to the shared memory (through the pointer obtained by <code>mmap</code>) and exit, then the shared memory object will keep its contents persistently (I can run another simple program to read back the data I wrote previously).</p> <p>So is the advice about using <code>shm_open</code> with <code>O_CREAT|O_EXCL</code> just wrong, or am I missing something?</p> <p>I do know that the shared memory object can be removed with <code>shm_unlink()</code>, but it seems that will only cause more problems:</p> <ol> <li><p>If a process dies before calling <code>shm_unlink()</code> then we are back to the problem described above.</p></li> <li><p>If one process calls <code>shm_unlink()</code> while some other processes are still mapped into the same shared memory, these other processes will still continue using it as usual. Now, if another process comes and calls <code>shm_open()</code> with the same name and <code>O_CREAT</code> specified, it will actually succeed in creating new shared memory object with the same name, which is totally unrelated to the old shared memory object the other processes are still using. Now we have a process trying to communicate with other processes via the shared memory and totally unaware that it is using a wrong channel.</p></li> </ol> <p>I m used to Windows semantics where shared memory object exists only as long as at least one handle is open to it, so this Posix stuff is very confusing.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload