Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since you use the <code>O_EXCL</code> flag I will assume that you have a set of processes gathered around one master (the creator of the segment).</p> <p>Then, your master process will create the shared memory segment using a call to <code>shm_open</code> :</p> <pre><code>shmid = shm_open("/insert/name/here", O_CREAT|O_EXCL, 0644); if (-1 == shmid) { printf("Oops ..\n"); } </code></pre> <p>Here, the slaves are ready to use the segment. Since the master HAS to create the segment, there is no need to use the <code>O_CREAT</code> flag in the slaves calls. You'll just have to handle possible errors if the slave call is performed when the segment is not created yet or already destroyed.</p> <p>When any of your processes is done with the segment, it should call <code>shm_unlink()</code>. In this kind of architecture, the <em>master</em> is usually feeding the <em>slaves</em>. When it has nothing more to say, it just shuts up. The <em>slaves</em> have then the responsibility to handle corresponding errors gracefully.</p> <p>As you stated, if a process dies <em>before</em> calling the <code>shm_unlink</code> procedure, then the segment will continue to live thereafter. To avoid this in some cases, you could define your own signal handlers in order to perform the operation when signals such as <code>SIGINT</code> are received. Anyway, you won't be able to cover the mess in case <code>SIGKILL</code> is sent to your process.</p> <p>EDIT : To be more specific, the use of <code>O_CREAT | O_EXCL</code> is wrong when unnecessary. With the little example above, you can see that it is required for the master to create the segment, thus those flags are needed. On the other hand, none of the slave processes would have to ever create it. Thus, you will absolutely forbid the use of <code>O_CREAT</code> in the related calls.</p> <p>Now, if another process calls <code>shm_open(..., O_CREAT, ...)</code> when the segment is already created, it will just retrieve a file descriptor related to this very segment. It will thus be on the right channel (if it has the rights to do so, see the <code>mode</code> argument) </p>
 

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