2

I got the code from the following link. https://www.youtube.com/watch?v=l6zkaJFjUbM[17:00]

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <semaphore.h>

#define THREAD_NUM 2

pthread_mutex_t mutexBuffer;

sem_t semEmty;
sem_t semFull;

int buffer[10];
int count = 0;

void* producer(void* args) {
  int x, s, p;
  while(1) {
    //Produce
    x = rand() % 100;
    
    //Add to the buffer
    s = sem_wait(&semEmty); //decrements semEmty, blocks when it reaches zero
    pthread_mutex_lock(&mutexBuffer);  
    buffer[count] = x;
    count++;   
    pthread_mutex_unlock(&mutexBuffer);
    p = sem_post(&semFull); //increments semFull

    //for debugging purpose
    if (count > 9) printf("---> count: %d, s: %d, p: %d\n", count, s, p); 
  }
  return NULL;
}
 
void* consumer(void* args) {
   int y, s, p;
   while (1)
   {  
      //Remove from the buffer
      s = sem_wait(&semFull); //decrements semFull, blocks when it reaches zero
      pthread_mutex_lock(&mutexBuffer);
      y = buffer[count - 1];
      count--;
      pthread_mutex_unlock(&mutexBuffer);
      p = sem_post(&semEmty); //increments semEmty
       
      //Consume
      printf("Got %d; count: %d, s: %d, p: %d\n", y, count, s, p);
   }
  
  return NULL;
}

int main(int argc, char* argv[]) {
  srand(time(NULL));
  pthread_t th[THREAD_NUM];
  pthread_mutex_init(&mutexBuffer, NULL);
  sem_init(&semEmty, 0, 10); //initially 10 empty slots
  sem_init(&semFull, 0, 0);  //initially 0 filled slot
  int i;
  for(i=0; i<THREAD_NUM; i++) {
    if (i % 2 == 0) {
      if (pthread_create(&th[i], NULL, &producer, NULL) != 0) {
        perror("Failed to create producer thread");
      }
    } else {
       if (pthread_create(&th[i], NULL, &consumer, NULL) != 0) {
         perror("Failed to create consumer thread");
       }
    }
  }

  for (i = 0; i < THREAD_NUM; i++) {
    if (pthread_join(th[i], NULL) != 0) {
      perror("Failed to join thread");
    }
  }
  sem_destroy(&semEmty);
  sem_destroy(&semFull);
  pthread_mutex_destroy(&mutexBuffer);  
  return 0;
}

As I run the code, I notice that the 'count' variable exceeds the upper bound value 10, and goes lower than the lower bound value 0. Eventually this leads to segmentation fault.

I compile the code in MAC, and get the "'sem_XXX' is deprecated ..." WARNING messages.

Not sure if this contributes to the erratic behaviour of the program.

I wonder why the sem_wait and sem_post are not doing their jobs.

You may also notice that I do not use sleep() function.

The followings are the last few lines that lead to the segmentation fault.

Got 32767; count: -33, s: 0, p: 0
Got 1586857744; count: -34, s: 0, p: 0
Got 1; count: -35, s: 0, p: 0
Got 54410882; count: -36, s: 0, p: 0
Got 1; count: -37, s: 0, p: 0
Got 26; count: -36, s: 0, p: 0
Segmentation fault: 11

Notice the output may change from time to time as you run the program.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
Tony
  • 111
  • 5

0 Answers0