title: Semaphore not working without sem_getvalue?
body: <p>I'm really confused as to why this is not working, and since most the examples regarding semaphores are multithreaded and not multiprocessed I'm kind of stumped. My code does not work unless I put this above the sem_wait.</p>
  
  <pre><code>while (1)
      {
          int x;
          sem_getvalue(sem1, &amp;x);
          if (x&gt;0) break;
          sleep(1);
      }
  </code></pre>
  
  <p>Essentially making a semaphore myself... It's like the sem_wait only checks the value one time and then just sits forever. I have two producers and one consumer. Here is how it is structured:</p>
  
  <pre><code>Producer 1:
    sem_waits for sem1 (initially set to 1)
    sem_posts to sem2
  Producer 2:
    sem_waits for sem2 (initially set to 0)
    sem_posts to sem1
  </code></pre>
  
  <p>Without that snippet producer 2 never runs. And with that snippet in producer 2 but not 1, producer 1 runs, then 2, and then stop. Both need that snippet to do their every other thing..</p>
  
  <p>I feel like my problem is here:</p>
  
  <pre><code>// sem open must be used to be used by multiple processes?
  sem_t *sem1 = sem_open("producer", O_CREAT, 0644, 1);
  sem_t *sem2 = sem_open("producer2", O_CREAT, 0644, 0);
  sem_t *consumer_full = sem_open("cfull", O_CREAT, 0644, 0);
  sem_t *consumer_empty = sem_open("cempty", O_CREAT, 0644, 0);
  
  // sem_open remains in memory even after program exit.
  // initialize to correct value to be sure
  
  sem_init(sem1,0,1);
  sem_init(sem2,0,0);
  sem_init(consumer_full,0,0);
  sem_init(consumer_empty,0,0);
  </code></pre>
  
  <p>The sem_init changes the semaphores so they could only be shared by storing in shared memory? But how can I initialize a sem_open semaphore without designing some arbitrary do while loop.</p>