Question: #include #include #include #include / / Milk and cheese buffer sizes #define MILK _ BUFFER _ SIZE 9 #define CHEESE _ BUFFER _ SIZE 4

#include
#include
#include
#include
//Milk and cheese buffer sizes
#define MILK_BUFFER_SIZE 9
#define CHEESE_BUFFER_SIZE 4
//Milk and cheese buffer initialization from zero
int milk_buffer[MILK_BUFFER_SIZE]={0};
int cheese_buffer[CHEESE_BUFFER_SIZE]={0};
//Semaphores and mutexes
sem_t milk_sem;
sem_t cheese_sem;
pthread_mutex_t milk_mutex;
pthread_mutex_t cheese_mutex;
int number_of_burgers;
//Milk producer thread function
void* milk_producer(void* arg){
int id =*(int*)arg; //Producer ID
int produced =0;
while (produced < MILK_BUFFER_SIZE /3){
sem_wait(&milk_sem); //Wait for available space in the buffer
pthread_mutex_lock(&milk_mutex);
//Find an empty slot in the buffer to produce buffer
for (int i =0; i < MILK_BUFFER_SIZE; i++){
if (milk_buffer[i]==0){
milk_buffer[i]= id; //Fill the space with producer ID
printf("Milk producer %d produced at position %d
", id, i);
produced++;
break;
}
}
pthread_mutex_unlock(&milk_mutex);
sem_post(&milk_sem); //Signal that a milk bottle has been produced
}
return NULL;
}
//Cheese producer thread function
void* cheese_producer(void* arg){
int id =*(int*)arg;
int produced =0;
while(produced < CHEESE_BUFFER_SIZE /2){
pthread_mutex_lock(&cheese_mutex);
//Count and track available milk bottles
int milk_ids[3]={0};
int milk_index =0;
// Fetch three milk bottles
for (int i =0; i < MILK_BUFFER_SIZE && milk_index <3; i++){
if (milk_buffer[i]!=0){
milk_ids[milk_index++]= milk_buffer[i];
milk_buffer[i]=0; // Consume the milk bottle
}
}
if (milk_index ==3){
int cheese_id = milk_ids[0]*1000+ milk_ids[1]*100+ milk_ids[2]*10+ id;
printf("Cheese producer %d produced cheese %d
", id, cheese_id);
// Add the cheese slice to the cheese buffer
for (int j =0; j < CHEESE_BUFFER_SIZE; j++){
if (cheese_buffer[j]==0){
cheese_buffer[j]= cheese_id;
break;
}
}
}
pthread_mutex_unlock(&cheese_mutex);
sem_post(&cheese_sem); //signal that a cheese slice has been produced
}
return NULL;
}
//Cheeseburger producer thread function
void* cheeseburger_producer(void* arg){
int num_burgers =*(int*)arg; //Number of cheeseburgers to produce
for (int i =0; i < num_burgers; i++){
sem_wait(&cheese_sem);
pthread_mutex_lock(&cheese_mutex);
int cheese_count =0;
int cheese_ids[2]={0};
for (int j =0; j < CHEESE_BUFFER_SIZE; j++){
if (cheese_buffer[j]!=0) cheese_count++;
}
// Find 2 available cheese slices
for (int j =0; j < CHEESE_BUFFER_SIZE && cheese_count <2; j++){
if (cheese_buffer[j]!=0){
cheese_ids[cheese_count++]= cheese_buffer[j];
cheese_buffer[j]=0; // Consume cheese slice
}
}
if (cheese_count ==2){
printf("

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!