title: how to attach to an existing shared memory segment
body: <p>I am having trouble with shared memory. I have one process that creates and writes to a shared memory segment just fine. But I cannot get a second process to attach that same existing segment. My second process can create a new shared segment if I use IPC_CREATE flag but I need to attach to the existing shared segment that was created by the 1st process.</p>
  
  <p>This is my code in the 2nd process:</p>
  
  <pre><code>int nSharedMemoryID = 10;
  key_t tKey = ftok("/dev/null", nSharedMemoryID);
  if (tKey == -1)  {
      std::cerr &lt;&lt; "ERROR: ftok(id: " &lt;&lt; nSharedMemoryID &lt;&lt; ") failed, " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
      exit(3);
  }
  std::cout &lt;&lt; "ftok() successful " &lt;&lt; std::endl;
  
  size_t nSharedMemorySize = 10000;
  int id = shmget(tKey, nSharedMemorySize, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (id == -1)  {
      std::cerr &lt;&lt; "ERROR: shmget() failed, " &lt;&lt; strerror(errno) &lt;&lt; std::endl &lt;&lt; std::endl;
      exit(4);
  }
  std::cout &lt;&lt; "shmget() successful, id: " &lt;&lt; id &lt;&lt; std::endl;
  
  unsigned char *pBaseSM = (unsigned char *)shmat(id, (const void *)NULL, SHM_RDONLY);
  if (pBaseSM == (unsigned char *)-1)  {
      std::cerr &lt;&lt; "ERROR: shmat() failed, " &lt;&lt; strerror(errno) &lt;&lt; std::endl &lt;&lt; std::endl;
      exit(5);
  }
  std::cout &lt;&lt; "shmat() successful " &lt;&lt; std::endl;
  </code></pre>
  
  <p>The problem is that the 2nd process always errors out on the call to shmget() with a "No such file or directory" error. But this is the exact same code I used in the 1st process and it works just fine there. In the 1st process that created the shared segment, I can write to the memory segment, I can see it with "ipcs -m"  Also, if I get the shmid from the "ipcs -m" command of the segment and hard code it in my 2nd process and the 2nd process can attach to it just fine. So the problem seems to be generation of the common id that both processes use to identify a single shared segment.</p>
  
  <p>I have several questions:</p>
  
  <p>(1) Is there an easier way to get the shmid of an existing shared memory segment? It seems crazy to me that I have to pass <em>three</em> separate parameters from the 1st process (that created the segment) to the 2nd process just so the 2nd process can get the same shared segment. I can live with having to pass 2 parameters: the file name like "/dev/null" and the same shared id (nSharedMemoryID in my code). But the size of the segment that has to be passed to the shmget() routine in order to get the shmid seems senseless because I have no idea of exactly how much memory was actually allocated (because of the page size issues) so I cannot be certain it is the same.
  (2) does the segment size that I use in the 2nd process have to be the same as the size of the segment used to initially create the segment in the 1st process? I have tried to specify it as 0 but I still get errors. 
  (3) likewise, do the permissions have to be the same? that is, if the shared segment was created with read/write for user/group/world, can the 2nd process just use read for user? (same user for both processes).
  (4) and why does shmget() fail with the "No such file or directory" error when the file "/dev/null" obviously exists for both processes?  I am assuming that the 1st process does not put some kind of a lock on that node because that would be senseless.</p>
  
  <p>Thanks for any help anyone can give. I have been struggling with this for hours--which means I am probably doing something really stupid and will ultimately embarrass myself when someone points out my error  :-)</p>
  
  <p>thanks,
  -Andres</p>