Producer Consumer


System
  •  
  •  
  •  
  •  
  •  
  •  
Buffer
State
  • Producer
  • Consumer
  • Mutex
  • Empty
  • Fill
What's going on?
Producer Active
void *producer() {
    sem_wait(&empty);
    sem_wait(&mutex);
    produce();
    sem_post(&mutex);
    sem_post(&fill);
    cut_hair(); 
}
Consumer Active
void *consumer() {
    sem_wait(&fill);
    sem_wait(&mutex);
    consume();
    sem_post(&mutex);
    sem_post(&empty);
}
producer