Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because you're using <code>shm_unlink()</code> immediately before <code>shm_open()</code>, your two processes never open the same shared memory object - each one is creating a new, distinct object (even though they have the same name).</p> <hr> <p>It's not clear what you mean by "doesn't work". The following minimal example, based on your code, works fine for me. What does it do for you?</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;sys/mman.h&gt; #include &lt;sys/types.h&gt; #include &lt;fcntl.h&gt; #include &lt;unistd.h&gt; #include &lt;semaphore.h&gt; typedef struct container { sem_t resource, mutex; int counter; } container; int main() { container *memory; int fd = shm_open("MYSHM", O_RDWR|O_CREAT|O_EXCL, 0); if(fd == -1) { perror("shm_open"); return 1; } ftruncate(fd, sizeof(container)); memory = mmap(NULL, sizeof(container), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); memory-&gt;counter = 5; printf("%d\n", memory-&gt;counter); return 0; } </code></pre> <hr> <p>On reviewing your code, it's failing because you are trying to access the file descriptor in a child process after <code>exec()</code>, and <code>shm_open</code> sets the <code>FD_CLOEXEC</code> flag by default so the file descriptor is no longer open in the child. So you just need to unset that flag in the main process (eg. right after checking for the error after <code>shm_open</code>):</p> <pre><code>fdflags = fcntl(fd, F_GETFD); fdflags &amp;= ~FD_CLOEXEC; fcntl(fd, F_SETFD, fdflags); </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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