title: producer consumer using posix
body: <p>I have a Standard Producer Consumer problem for bounded buffer.Whenever i give unequal number of producer or consumer the Program does not terminate.
  I have limited the number of insertion or deletion to 50 
  I would like to know why the above problem occurs and ways to fix it</p>
  
  <pre><code> #include&lt;stdio.h&gt;
   #include&lt;pthread.h&gt;  //Header for creating Posix Threads;
   #include&lt;semaphore.h&gt; 
   #include&lt;stdlib.h&gt;
   #define MAX 20
  
   typedef struct shared_buffer
    {
       int arr[20];
       int i; 
    }buffer;   
  
    buffer b;         //Fixed Length buffer shared memory
  
  sem_t full,empty;    //Counting semaphores full-&gt;no of slots filled  empty -&gt;no of slots        empty
  pthread_mutex_t mutex; //mutual exclusion for critcal section
  int flag=1,cnt=0;      
  void * producer(void * arg)   //Producer threads method
  {
    int index;   //thread Id
    index=(int)arg;
  
    while(flag)
     {
  
       sem_wait(&amp;empty);        //will check if slot available and decrement empty count
       pthread_mutex_lock(&amp;mutex);   //acquiring lock on process
       b.arr[b.i]=rand()%100;        //critcal section
       printf("\n Process %d Produced :%d",index,b.arr[b.i]);
       b.i++;
           cnt++;                //critcal section ends
       pthread_mutex_unlock(&amp;mutex); //realeasing lock 
       sem_post(&amp;full);              //increment full count 
           usleep(rand()%100);           //sleep for random time
  
     }
   }
  
   void * consumer(void * arg)
   {
    int index;
    index=(int)arg;
    while(flag)
    {   
       sem_wait(&amp;full);                  //will check if buffer is not empty
       pthread_mutex_lock(&amp;mutex); 
       b.i--;                           //critical section
       printf("\n Process %d consumed :%d",index,b.arr[b.i]);
       cnt++;                      //critical section ends
       pthread_mutex_unlock(&amp;mutex);  //release lock
       sem_post(&amp;empty);             //increment count of empty slots
       usleep(rand()%100);
    }
   }
  
  
  int main(int argc,char * argv[])
  {
     pthread_t Pid,Cid;
     int P,C,index,size;
     b.i=0;
   if(argc&lt;4)
   {
      printf("Error.Usage : ./filename.out &lt;no of producer&gt; &lt;no of consumer&gt; &lt;Buffer     Size(&lt;=15)&gt; \n");
     exit(0);
   }
    P=atoi(argv[1]);
    C=atoi(argv[2]);
    size=atoi(argv[3]);
    sem_init(&amp;full,0,0);              //number of slots filled is 0
    sem_init(&amp;empty,0,size);         //number of empty slots is buffer size
    pthread_mutex_init(&amp;mutex,NULL);
  
    for (index=0;index&lt;C;index++)     //creating C number of consumer
    {
       pthread_create(&amp;Cid,NULL,consumer,index);
    }
    for (index=0;index&lt;P;index++)    //creating P number of producer
    {
      pthread_create(&amp;Pid,NULL,producer,index);
    }
  while(cnt&lt;=50)                    //maximum 50 operations allowed
     usleep(200);
     flag=0;
     printf("phew!Successful");
     pthread_exit(NULL);
     return 1;
  </code></pre>
  
  <p>}</p>