title: Deleting shared memory segment with shmctl
body: <p>I am confused by the linux <code>man pages</code> for <code>shmctl()</code>.  I use the following command: <code>shmctl (id , IPC_RMID , 0)</code> to remove a shared segment.  The <code>man pages</code> seem to contradict itself about the memory's lifetime.  </p>
  
  <p>The <a href="http://linux.die.net/man/2/shmctl">man pages</a> state: </p>
  
  <blockquote>
    <p>IPC_RMID</p>
    
    <p>Mark the segment to be destroyed. The segment will only actually be destroyed after the last process detaches it (i.e., when the
    shm_nattch member of the associated structure shmid_ds is zero). The
    caller must be the owner or creator, or be privileged. If a segment
    has been marked for destruction, then the (nonstandard) SHM_DEST flag
    of the shm_perm.mode field in the associated data structure retrieved
    by IPC_STAT will be set.</p>
  </blockquote>
  
  <p>If I am correct, I believe this means if you have two processes that both attach to shared memory, (Lets call them <code>Process1</code> and <code>Process2</code>), <code>Process1</code> could create the shared memory, attach, detach, destroy the shared memory, and ultimately terminate.  Then the memory will still exist until <code>Process2</code> also detaches.  </p>
  
  <p>Is this correct?</p>
  
  <p>Secondly, what does this statement in the <code>man pages</code> mean: </p>
  
  <blockquote>
    <p>The caller must ensure that a segment is eventually destroyed;
    otherwise its pages that were faulted in will remain in memory or
    swap.</p>
  </blockquote>
  
  <p>This makes it seem like <code>Process1</code>, since it marked the segment for deletion, would need to block until all other processes are detached in order to ensure the memory gets deleted.  But this seems to contradict the above statement.  I also have no idea how this would be done (if it is supposed to be done), so if that is the case, could you also explain how I would go about this. </p>