You are on page 1of 1

Modèle du producteur/consommateur

#define BUFFER_SIZE 4
int head = 0, tail = 0 ;

sem_t empty_places = BUFFER_SIZE,


nb_messages = 0 ;

Producer Consumer
tail
// Produce a value // Consume a value
while (1) shared_buffer while (1)
{ {
value = rand()%100 ; sem_wait(&nb_messages) ;
sem_wait(&empty_places) ;
shared_buffer[tail] = value ; value = shared_buffer[head] ;
tail = (tail + 1)%BUFFER_SIZE ; Printf("%d", value);
sem_post(&nb_messages) ; head = (head + 1)%BUFFER_SIZE ;
head sem_post(&empty_places) ;
}
}

You might also like