The bounded buffer using semaphores in C
<p>The following is the code I have so far. The program uses a buffer shared between two processes, a producer and a consumer. The producer reads characters from a file and places them in the buffer. The consumer reads each item from the buffer and prints the characters onto the screen. I run the program but it seems like it has run into an infinite loop or something that keeps it from terminating. Nothing gets printed to the screen and I don't see whats causing this.</p>

<pre><code>#define _REENTRANT
#include &lt;fcntl.h&gt;
#include &lt;pthread.h&gt;
#include &lt;semaphore.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/ipc.h&gt;
#include &lt;sys/shm.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/wait.h&gt;
typedef int bufferItem;
#define BUFFER_SIZE 5

pthread_mutex_t mutex;
sem_t full;
sem_t empty;
int counter;
bufferItem buffer[BUFFER_SIZE];

//--------------------------------------------
// insert function used by producer thread
//--------------------------------------------
void insertItem(bufferItem item) {

  if (counter &lt; BUFFER_SIZE) {
    buffer[counter] = item;
    counter++;
    return;
  } else
    printf("Error inserting item");
}

//--------------------------------------------
// remove function used by consumer thread
//---------------------------------------------
bufferItem removeItem() {

  if (counter &gt; 0) {
    bufferItem itemRem;
    itemRem = buffer[counter - 1];
    counter--;
    return itemRem;
  } else
    return -1;
}

//---------------------------------------------
// producer prototype
//---------------------------------------------
void *thread1() {

  char newChar;
  FILE *fp;
  fp = fopen("mytest.dat", "r");

  while (1 &amp;&amp; fscanf(fp, "%c", &amp;newChar) != EOF) {

    // produce an item
    bufferItem currentChar = newChar;

    sem_wait(&amp;empty);
    pthread_mutex_lock(&amp;mutex);

    insertItem(currentChar);

    pthread_mutex_unlock(&amp;mutex);
    sem_post(&amp;full);
  }
  close(fp);
}

//---------------------------------------------
// consumer thread prototype
//----------------------------------------------
void *thread2() {
  while (1) {
    sem_wait(&amp;full);
    pthread_mutex_lock(&amp;mutex);

    bufferItem itemPrint = removeItem();

    pthread_mutex_unlock(&amp;mutex);
    sem_post(&amp;empty);
    printf("%d", itemPrint);

    sleep(1);
  }
}

//-------------------------------------------
// MAIN
//-------------------------------------------
main() {

  int r = 0;
  int i;
  sem_t sem1;
  int shmid;              /* shared memory ID */
  pthread_t tid1[1];      /* process id for thread 1 */
  pthread_t tid2[1];      /* process id for thread 2 */
  pthread_attr_t attr[1]; /* attribute pointer array */
  pthread_mutex_init(&amp;mutex, NULL);
  sem_init(&amp;full, 0, 0);
  sem_init(&amp;empty, 0, BUFFER_SIZE);
  counter = 0;

  fflush(stdout);
  /* Required to schedule thread independently.*/
  pthread_attr_init(&amp;attr[0]);
  pthread_attr_setscope(&amp;attr[0], PTHREAD_SCOPE_SYSTEM);
  /* end to schedule thread independently */

  /* Create the threads */
  pthread_create(&amp;tid1[0], &amp;attr[0], &amp;thread1, NULL);
  pthread_create(&amp;tid2[0], &amp;attr[0], &amp;thread2, NULL);

  /* Wait for the threads to finish */
  pthread_join(tid1[0], NULL);
  pthread_join(tid2[0], NULL);

  printf("------------------------------------------------\n");
  printf("\t\t  End of simulation\n");

  exit(0);
}
</code></pre>