title: InterProcess communication -- Locking Mutex in shared memory
body: <p>I have 2 processes that will execute the same code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;pthread.h&gt;
  
  
  #ifndef _POSIX_THREAD_PROCESS_SHARED
  #error This system does not support process shared mutex
  #endif
  
  struct shm_content
  {
        pthread_mutex_t   mutex;
  };
  
  pthread_mutex_t    *mptr; //Mutex Pointer
  pthread_mutexattr_t matr; //Mutex Attribute
  
  int   shared_mem_id;      //shared memory Id
  int   *mp_shared_mem_ptr; //shared memory ptr -- pointing to mutex
  
  int main (void)
  {
      int  rtn;
      size_t shm_size;
  
      /* initialize shared memory segment */
      shm_size = 1*sizeof(pthread_mutex_t);
  
      if ((shared_mem_id = shmget(IPC_PRIVATE, shm_size, 0660)) &lt; 0)
      {
          perror("shmget"), exit(1) ;
      }
      if ((mp_shared_mem_ptr = (int *)shmat(shared_mem_id, (void *)0, 0)) == NULL)
      {
          perror("shmat"), exit(1);
      }
  
      //Offset to find the location of the mutex variable in the shared memory
      shm_content* pcontent = reinterpret_cast&lt;shm_content*&gt;(mp_shared_mem_ptr);
  
      mptr = &amp;(pcontent-&gt;mutex);
  
      // Setup Mutex
      if (rtn = pthread_mutexattr_init(&amp;matr))
      {
          fprintf(stderr,"pthreas_mutexattr_init: %s",strerror(rtn)),exit(1);
      }
      if (rtn = pthread_mutexattr_setpshared(&amp;matr,PTHREAD_PROCESS_SHARED))
      {
          fprintf(stderr,"pthread_mutexattr_setpshared %s",strerror(rtn)),exit(1);
      }
      if (rtn = pthread_mutex_init(mptr, &amp;matr))
      {
          fprintf(stderr,"pthread_mutex_init %s",strerror(rtn)), exit(1);
      }
  
          // Lock mutex and then wait for signal to relase mutex
      printf("child mutex lock \n");
      pthread_mutex_lock( mptr );
      printf("child mutex locked\n");
  
      int i = 0; // :)
  
      //busy wait
      while (i&lt;10)
      {
          printf("Busy Wait!!! I AM PROCESS 1\n");
          //in the second process will change this line to :
          //printf("Busy Wait!!! I AM PROCESS 2\n");
          sleep(2);
      }
  
      printf("child mutex unlock\n");
      pthread_mutex_unlock( mptr );
      printf("child mutex unlocked\n");
  
  }
  </code></pre>
  
  <p>I am expecting that the second process will only be able to acquire the mutex once the first process releases it, but now it seams that there's 2 copies of the mutex and each process can lock its own.</p>
  
  <p>Any ideas?</p>