title: C semaphores: sem_wait throwing inexplicable error
body: <p>I'm working on a problem which we have to use semaphores to solve. I have an array which contains two semaphores, <code>gsem</code>, and given certain conditions call <code>sem_wait(&amp;(gsem[me]))</code>, which is supposed to waiting until that particular process is woken up. However, for some reason it gives me the error <code>Bad file descriptor</code>. I looked up <code>sem_wait</code> and the Open Group spec says this is not an error <code>sem_wait</code> can cause. This is making my whole program crazy and I have no idea why this is failing.</p>
  
  <p>EDIT: Offending code, as requested.</p>
  
  <pre><code>120     sem_wait(&amp;mutex);
  121     if (inside[opp] &gt; 0 || waiting[opp] &gt; 0) {
  122         sem_wait(&amp;screen);
  123         printf("%s %u waiting\n", names[me], t);
  124         sem_post(&amp;screen);
  125         waiting[me]++;
  126         sem_post(&amp;mutex);
  127         int hg = sem_wait(&amp;(gsem[me]));
  128         if (hg &lt; 0)
  129             printf("%s\n", strerror(errno));
  130     } 
  </code></pre>
  
  <p>I should note this is a homework assignment for which we are required to use semaphores. The professor calls it the "unisex bathroom". Either men and women can use it, but not simultaneously. <code>inside[opp]</code> is the number of people of the opposite sex in the bathroom. <code>waiting[opp]</code> is the number of the opposite sex waiting to use it. <code>screen</code> is a semaphore which locks access to <code>stdout</code>. The solution is based on a solution to the readers/writers problem given in our textbook which uses passing the baton.</p>
  
  <p>I should also note that we first had to code a solution in Ada and then convert it to C. My Ada solution works, and I translated it verbatim. I'm sure it's some minor syntactical detail. Lastly, I'm working on Snow Leopard, if that helps.</p>