title: System V semget creates semaphore but returns 0
body: <p>I'm playing with System V semaphores on an Ubuntu 14.04 server. Whenever I make a call to <code>semget()</code>, either to create or access an existing semaphore, a value of zero is returned. Per the man pages of <code>semget</code>, "If  successful,  the return value will be the semaphore set identifier (a nonnegative integer), otherwise -1 is returned, with errno indicating the error." </p>
  
  <p>I confirm that the sempahore is in fact created by running the <code>ipcs -s</code> command.</p>
  
  <pre><code>------ Semaphore Arrays --------
  key        semid      owner    perms      nsems     
  0x00000002 720903     myuser   666        1 
  </code></pre>
  
  <p>So in my case, I believe <code>semget()</code> should have returned 720903 instead of 0.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>static void create_sem(void)
  
  {
  int key = 2;
  int nsems = 1;
  int sem_id;
  
      printf("Attempting to create new semaphore set with %d members\n", nsems);
  
      if(sem_id = semget((key_t)key, nsems, IPC_CREAT|IPC_EXCL|0666) == -1) {
          printf("Error creating semaphore\n");
          exit(1);
      }
  
      printf("Successfully created semaphore %d for key %d\n",
          sem_id, key);
  
      if(semctl(sem_id, 0, IPC_RMID, 0) == -1) {
          perror("Error destorying semaphore");
          exit(1);
      }
  }
  </code></pre>
  
  <p>Here's the output</p>
  
  <pre><code>Attempting to create new semaphore set with 1 members
  Successfully created semaphore 0 for key 2
  Error destroying semaphore: Operation not permitted
  </code></pre>
  
  <p>Note, if I hardcode the semid I see by issuing <code>ipcs -s</code>, I can successfully remove the semaphore array by using <code>semctl(720903, 0, IPC_RMID, 0)</code>.</p>
  
  <p>Why is <code>semget()</code> returning 0 instead of the actual semid?</p>