title: I am receiving "shmat: permission denied" when attempting to attach to shared memory. Why?
body: <p>I'm having a little trouble with shared memory, and could use a little guidance if someone could point me in the right direction.</p>
  
  <pre><code>// Allocate Shared Memory
  key_t key = 56789;
  int shmid;
  char* shm_address;
  int* value;
  
  // Reserve the memory
  if (shmid = shmget(key, sizeof(int), IPC_CREAT | 0777) &lt; 0)
  {
      perror("shmget was unsuccessful");
      exit(1);
  }   
  else
  {
      printf("\nMemory created successfully:%d\n", shmid);
  }
  
  // Attach to memory address
  if ( (shm_address = shmat(shmid, NULL, 0)) == (char *)-1 )
  {
  
      perror("shmat was unsuccessful");
      exit(1);
  
  }
  else
  {
      printf ("shared memory attached at address %p\n", shm_address);
  }
  </code></pre>
  
  <p>I then do some process management, call <code>shmdt(shm_address)</code>, and finally cleanup with <code>shmctl</code>. But I never reach that part of the code.</p>
  
  <p>I get this as output:</p>
  
  <pre><code>Memory created successfully:0
  
  shmat was unsuccessful: Permission denied
  </code></pre>
  
  <p>I just don't understand why <code>shmat</code> is failing to attach? When I call the ipcs command after execution, my memory is allocated, so I'm fairly confident that <code>shmget</code> is working. Could anyone point me in the right direction? Thanks.</p>