title: Producer Consumer program using semaphores and pthreads
body: <p>I have written a code for producer-consumer problem.But I am not getting the output.There is no compilation error,but warning in my program.I am confused.Trying very hard.But can't get it.Please tell me what is wrong in my program.What will be the correct program.I am getting frustrated.Please help guys.
  Here is the code-</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;/usr/include/semaphore.h&gt;
  
  #define BUFF_SIZE   5           /* total number of slots */
  #define NP          3           /* total number of producers */
  #define NC          3           /* total number of consumers */
  #define NITERS      4           /* number of items produced/consumed */
  
  typedef struct {
      int buf[BUFF_SIZE];   /* shared var */
      int in;               /* buf[in%BUFF_SIZE] is the first empty slot */
      int out;              /* buf[out%BUFF_SIZE] is the first full slot */
      sem_t full;           /* keep track of the number of full spots */
      sem_t empty;          /* keep track of the number of empty spots */
      sem_t mutex;          /* enforce mutual exclusion to shared data */
  } sbuf_t;
  
  sbuf_t shared;
  
  
  void *Producer(void *arg) {
      int i, item, index;
  
      index = (int) arg;
  
  
      for (i = 0; i &lt; NITERS; i++) {
          /* Produce item */
          item = i;
  
          /* Prepare to write item to buf */
  
          /* If there are no empty slots, wait */
          sem_wait(&amp;shared.empty);
          /* If another thread uses the buffer, wait */
          sem_wait(&amp;shared.mutex);
          shared.buf[shared.in] = item;
          shared.in = (shared.in+1)%BUFF_SIZE;
          printf("[P%d] Producing %d ...\n", index, item); fflush(stdout);
          /* Release the buffer */
          sem_post(&amp;shared.mutex);
          /* Increment the number of full slots */
          sem_post(&amp;shared.full);
  
          /* Interleave  producer and consumer execution */
          if (i % 2 == 1) sleep(1);
      }
      return NULL;
  }
  
  void *Consumer(void *arg) {
      int i, item, index;
  
      index = (int) arg;
      for (i = NITERS; i &gt; 0; i--) {
        sem_wait(&amp;shared.full);
        sem_wait(&amp;shared.mutex);
        item = i;
        item = shared.buf[shared.out];
        shared.out = (shared.out + 1) % BUFF_SIZE;
        printf("[C%d] Consuming  %d ...\n", index, item); fflush(stdout);
        /* Release the buffer */
        sem_post(&amp;shared.mutex);
        /* Increment the number of full slots */
        sem_post(&amp;shared.empty);
  
        /* Interleave  producer and consumer execution */
        if (i % 2 == 1) sleep(1);
      }
      return NULL;
  }
  
  int main() {
      pthread_t idP, idC;
      int index;
  
      sem_init(&amp;shared.full, 0, 0);
      sem_init(&amp;shared.empty, 0, BUFF_SIZE);
      pthread_mutex_init(&amp;shared.mutex, NULL);
      for (index = 0; index &lt; NP; index++) {
         /* Create a new producer */
         pthread_create(&amp;idP, NULL, Producer, (void*)index);
      }
      /*create a new Consumer*/
      for (index = 0;index &lt; NC;index++) {
          pthread_create(&amp;idC, NULL, Consumer, (void*)index);
      }
      pthread_exit(NULL);
  }
  </code></pre>