Posts

Showing posts from May, 2018

Producer Consumer problem using mutex

  #include<stdio.h> #include<pthread.h> #include<stdlib.h>   // Max buffer size user for producing #define buff_sz 10   // buffer index would be the current index volatile int buff_idx = -1; // buffer used to produce item volatile char *buff = NULL;;   // Initialization and declaration of muetxt and condition variables pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t buff_not_full = PTHREAD_COND_INITIALIZER; pthread_cond_t buff_not_empty = PTHREAD_COND_INITIALIZER;   // Consumer thread void *Consumer() {          while (1)          {                  // Taking lock to avoid race condition                  pthread_mutex_ lock(&m...