title: pthreads + semaphores, why is this not executing properly?
body: <p>This is an assignment I'm working on. It must use semaphores, not mutex.</p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  #include &lt;pthread.h&gt; 
  #include &lt;assert.h&gt; 
  #include &lt;unistd.h&gt; 
  #include &lt;semaphore.h&gt; 
  #include &lt;fcntl.h&gt;
  sem_t *ab, *ac, *ad, *de, *ce, *bf, *ef; 
  
  void *a(void *arg) {
      printf("Entering A...\n");
      sleep(1);
      printf("Exiting A...\n");
      assert(sem_post(ab)==0);
      assert(sem_post(ac)==0);
      assert(sem_post(ad)==0);
      pthread_exit((void *)99);
  }
  
  void *b(void *arg) {
      assert(sem_wait(ab)==0);
      printf("Entering B...\n");
      sleep(1);
      printf("Exiting B...\n");
      assert(sem_post(bf)==0);
      pthread_exit((void *)99);
  }
  
  void *c(void *arg) {
      assert(sem_wait(ac)==0);
      printf("Entering C...\n");
      sleep(1);
      printf("Exiting C...\n");
      assert(sem_post(ce)==0);
      pthread_exit((void *)99);
  }
  
  void *d(void *arg) {
      assert(sem_wait(ad)==0);
      printf("Entering D...\n");
      sleep(1);
      printf("Exiting D...\n");
      assert(sem_post(de)==0);
      pthread_exit((void *)99);
  }
  
  void *e(void *arg) {
      assert(sem_wait(ce)==0);
      assert(sem_wait(de)==0);
      printf("Entering E...\n");
      sleep(1);
      printf("Exiting E...\n");
      assert(sem_post(ef)==0);
      pthread_exit((void *)99);
  }
  
  void *f(void *arg) {
      assert(sem_wait(bf)==0);
      assert(sem_wait(ef)==0);
      printf("Entering F...\n");
      sleep(1);
      printf("Exiting F...\n");
      pthread_exit((void *)99);
  }
  
  
  int main() { 
      pthread_t _a, _b, _c, _d, _e, _f;
      int r1, r2, r3, r4, r5, r6;
  
      ab=sem_open("foobar", O_CREAT, 0700, 0);
      ac=sem_open("foobar", O_CREAT, 0700, 0);
      ad=sem_open("foobar", O_CREAT, 0700, 0);
      ce=sem_open("foobar", O_CREAT, 0700, 0);
      de=sem_open("foobar", O_CREAT, 0700, 0);
      ef=sem_open("foobar", O_CREAT, 0700, 0);
      bf=sem_open("foobar", O_CREAT, 0700, 0);
  
      /*sem_init(ab,0,1);
      sem_init(ac,0,1);
      sem_init(ad,0,1);
      sem_init(ce,0,1);
      sem_init(de,0,1);
      sem_init(ef,0,1);
      sem_init(bf,0,1);*/
  
      assert(pthread_create(&amp;_a, NULL, a, &amp;r1) == 0);
      assert(pthread_create(&amp;_b, NULL, b, &amp;r2) == 0);
      assert(pthread_create(&amp;_c, NULL, c, &amp;r3) == 0);
      assert(pthread_create(&amp;_d, NULL, d, &amp;r4) == 0);
      assert(pthread_create(&amp;_e, NULL, e, &amp;r5) == 0);
      assert(pthread_create(&amp;_f, NULL, f, &amp;r6) == 0);
  
      assert(pthread_join(_a, NULL) == 0);
      assert(pthread_join(_b, NULL) == 0);
      assert(pthread_join(_c, NULL) == 0);    
      assert(pthread_join(_d, NULL) == 0);
      assert(pthread_join(_e, NULL) == 0);
      assert(pthread_join(_f, NULL) == 0);
  
      assert( sem_close(ab)==0 ); 
      assert( sem_close(ac)==0 ); 
      assert( sem_close(ad)==0 ); 
      assert( sem_close(ce)==0 );
      assert( sem_close(de)==0 ); 
      assert( sem_close(bf)==0 );
      assert( sem_close(ef)==0 ); 
  
      return 0; 
  }
  </code></pre>
  
  <p>It's pretty simple but for some reason it's not executing in the right order. The output is far from consistent but always incorrect. Here is one sample output:</p>
  
  <p>Entering A...<br>
  Entering B...  &lt;----sem_post(ab) has not even been called yet<br>
  Exiting A...<br>
  Entering C...<br>
  Entering D...<br>
  Exiting B...<br>
  Exiting D...<br>
  Exiting C...<br>
  Entering E...<br>
  Entering F...<br>
  Exiting F...<br>
  Exiting E...  </p>
  
  <p>It should be following this diagram:</p>
  
  <p><img src="https://i.stack.imgur.com/wCAXE.jpg" width="275"></p>
  
  <p>Any help with this will be greatly appreciated, but it's an assignment so don't start telling me to do it a completely different way and don't give the answer straight up, just point me in the right direction.</p>