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:
Milk producers
Cheese producers
Cheeseburger producer
Milk producers
int Buffermilk; shared by all producers
Cheese producers
int Buffercheese; shared by all producers
cheeseburger producer
Program DEscriPtion
You must create threads in your program: milk producer threads, cheese producer threads, and 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: cheeseburger Rightarrow slices of cheese Rightarrow bottles of milk.
Pass the ID of each producer and the number of items assigned to each thread: IDs of milk producers cheese producers
pthreadcreate&tid, NULL, runner, void args;
Threads are created with: pthreadcreate&tid, NULL, runner, void args;
The Main program waits for thread completion using pthreadjoin&tid, NULL;
Program Input
Program asks: "How many burgers do you want?"
Creates milkproducer threads:
Arguments: producer ID number of items to produce
Produce a bottle of milk and put it in the refrigerator buffermilk: size
The buffer access by milkproducer, cheeseproducer is protected by a mutex lock use a semaphore to implement mutex
Can produce one item per buffer access
indicates no item; and represent items produced by the producer IDs and respectively
Use semaphores to synchronize milk production three and consumption by cheeseproducer
The producer waits if the buffer is full use a semaphore and carefully set an initial value
Creates cheeseproducer threads:
Arguments: producer ID 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 "buffermilk", produce a slice of cheese
Replace the milk ID in the buffermilk with
The cheese slice is represented by a digit positive integer
Three milk bottles are from producers and if the current cheese producer ID is then is the produced item
The cheesebuffer is protected by a mutex lock implemented by a semaphore. Only one thread can access the buffer at a time.
The cheeseproducer is a consumer of milk and a producer
Create cheeseburgerproducer threads:
Arguments: number of items to produce
Cheeseburgerproducer 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
eg is made with two slices of cheese and
The main thread waits for the termination of threads three milk producers, two cheese producers, one cheeseburger producer using pthreadjoin operation
Output
Printed cheeseburger IDs, egmade with two cheese slices and
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
