title: fork exec and mmap issues
body: <p>For the application I'm developing (under Linux, but I'm trying to maintain portability) I need to switch to shared memory for sharing data across different processes (and threads inside processes). There is a father process generating different children</p>
  
  <p>I need for example to get every process able to increment a shared counter using a named semaphore. </p>
  
  <p>In this case everything is ok:</p>
  
  <pre><code>#include &lt;sys/mman.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  using namespace std;
  
  #define SEM_NAME "/mysem"
  #define SM_NAME "tmp_sm.txt"
  
  int main(){
      int fd, nloop, counter_reset;
      int *smo;
      sem_t *mutex;
  
      nloop = 100;
      counter_reset = 1000;
  
      if (fork() == 0) {
          /* child */
          /* create, initialize, and unlink semaphore */
          mutex = sem_open(SEM_NAME, O_CREAT, 0777, 1);
          //sem_unlink(SEM_NAME);
          /* open file, initialize to 0, map into memory */
          fd = open(SM_NAME, O_RDWR | O_CREAT);
          smo = (int *) mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
          close(fd);
          /* INCREMENT */
          for (int i = 0; i &lt; nloop; i++) {
              sem_wait(mutex);
              cout &lt;&lt; "child: " &lt;&lt; (*smo)++ &lt;&lt; endl;
              if(*smo&gt;=counter_reset){
                  (*smo)=0;
              }
              sem_post(mutex);
          }
          exit(0);
      }
      /* parent */
      /* create, initialize, and unlink semaphore */
      mutex = sem_open(SEM_NAME, O_CREAT, 0777, 1);
      sem_unlink(SEM_NAME);
      /* open file, initialize to 0, map into memory */
      fd = open(SM_NAME, O_RDWR | O_CREAT);
      smo = (int *) mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
      close(fd);
      /* INCREMENT */
      for (int i = 0; i &lt; nloop; i++) {
          sem_wait(mutex);
          cout &lt;&lt; "parent: " &lt;&lt; (*smo)++ &lt;&lt; endl;
          if(*smo&gt;=counter_reset){
              (*smo)=0;
          }
          sem_post(mutex);
      }
      exit(0);
  }
  </code></pre>
  
  <p>So far so good: both semaphore and shared counter are ok (same address in memory) and increment and reset work fine.</p>
  
  <p>The program fails simply by moving child source code into a new source file invoked by exec. Shared memory and named semaphore addresses are different therefore increment fails.</p>
  
  <p>Any suggestion? I used named semaphores and named shared memory (using a file) to try to get the same pointer values.</p>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>as requested by Joachim Pileborg, this is the "server side" improvements respect above original code:</p>
  
  <pre><code>...
  if (fork() == 0) {
      /* child */
      /*spawn child by execl*/
      char cmd[] = "/path_to_bin/client";
      execl(cmd, cmd, (char *)0);
      cerr &lt;&lt; "error while istantiating new process" &lt;&lt; endl;
      exit(EXIT_FAILURE);
  }
  ...
  </code></pre>
  
  <p>And this is the "client" source code:</p>
  
  <pre><code>#include &lt;sys/mman.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  using namespace std;
  
  #define SEM_NAME "/mysem"
  #define SM_NAME "tmp_ssm.txt"
  
  int main(){
      int nloop, counter_reset;
      int *smo;
      sem_t *mutex;
      /* create, initialize, and unlink semaphore */
      mutex = sem_open(SEM_NAME, O_CREAT, 0777, 1);
      //sem_unlink(SEM_NAME);
      /* open file, initialize to 0, map into memory */
      int fd = open(SM_NAME, O_RDWR | O_CREAT);
      smo = (int *) mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
      close(fd);
      nloop=100;
      counter_reset=1000;
      /* INCREMENT */
      for (int i = 0; i &lt; nloop; i++) {
          sem_wait(mutex);
          cout &lt;&lt; "child: " &lt;&lt; (*smo)++ &lt;&lt; endl;
          if(*smo&gt;=counter_reset){
              (*smo)=0;
          }
          sem_post(mutex);
      }
      exit(0);
  }
  </code></pre>
  
  <p>executing this code cause the process to block (deadlock) and waiting for an infinite time. looking at addresses they are tipically found to be:</p>
  
  <pre><code>father semaphore: 0x7f2fe1813000
  child semahpore: 0x7f0c4c793000
  father shared memory: 0x7f2fe1811000
  child shared memory: 0x7ffd175cb000
  </code></pre>
  
  <p>removing 'sem_post' and 'sem_wait' everything is fine but I need mutual exlusion while incrementing...</p>