Question: Problem Statement: Implement a program with the following requirements: - 3 Milk producers - 2 Cheese producers - 1 Cheeseburger producer 3 Milk producers int

Problem Statement: Implement a program with the following requirements:
-3 Milk producers
-2 Cheese producers
-1 Cheeseburger producer
3 Milk producers
int Buffer_milk[9]=\{0,\}; // shared by all producers
2 Cheese producers
int Buffer_cheese[4]=\{0,\}; // shared by all producers
1 cheeseburger producer
2. Program DEscriPtion
- You must create 6 threads in your program: 3 milk producer threads, 2 cheese producer threads, and 1 cheeseburger producer thread.
- When your program is started, it will read a nonnegative integer (num. of cheeseburgers.).
- Then, your program calculates the required numbers of items for each thread. (see the previous slide):
- Example: 1 cheeseburger \(\Rightarrow 2\) slices of cheese \(\Rightarrow>6\) bottles of milk.
- Pass the ID of each producer and the number of items assigned to each thread: IDs of milk producers \((1,2,3)\), cheese producers \((4,5)\)
- pthread_create(\&tid, NULL, runner, (void*) args);
- Threads are created with: pthread_create(\&tid, NULL, runner, (void*) args);
- The Main program waits for thread completion using pthread_join(\&tid, NULL);
3. Program Input
- Program asks: "How many burgers do you want?"
- Creates 3 milk_producer threads:
- Arguments: producer ID (1,2,3), number of items to produce
- Produce a bottle of milk and put it in the refrigerator (buffer_milk: size 9)
- The buffer access (by milk_producer, cheese_producer) is protected by a mutex lock (use a semaphore to implement mutex)
* Can produce one item per buffer access
*0 indicates no item; 1,2, and 3 represent items produced by the producer IDs 1,2, and 3, respectively
- Use semaphores to synchronize milk production (three) and consumption (by cheese_producer)
- The producer waits if the buffer is full (use a semaphore and carefully set an initial value)
- Creates 2 cheese_producer threads:
- Arguments: producer ID \((4,5)\), number of items to produce
- If there are less than three milk bottles, the thread waits (use semaphore)
- If there are three (or more) available milk bottles in the "buffer_milk", produce a slice of cheese
* Replace the milk ID in the buffer_milk with 0
* The cheese slice is represented by a 4-digit positive integer
* Three milk bottles are from producers \(2,1,3\), and if the current cheese producer ID is 4, then 2134 is the produced item
* The cheese_buffer is protected by a mutex lock implemented by a semaphore. (Only one thread can access the buffer at a time.)
- The cheese_producer is a consumer (of milk) and a producer
- Create 1 cheeseburger_producer threads:
- Arguments: number of items to produce
- Cheeseburger_producer is the consumer of cheese slices
- If there are less than two slices of cheese, it waits (use semaphore)
- If there are two or more cheese slices, produce a cheeseburger
- Print the cheeseburger ID: concatenation of two cheese slice IDs
* e.g.213431125 is made with two slices of cheese 2134 and 1125.
- The main thread waits for the termination of threads (three milk producers, two cheese producers, one cheeseburger producer) using pthread_join() operation
4. Output
- Printed cheeseburger IDs, e.g.,213431125(made with two cheese slices 2134 and 1125).
Problem Statement: Implement a program with the

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!