Consider a factory with 30 machines Mi (1 <= i <= 30) making hot dogs and another 25 machines Pj (1 <= j <= 25) packing hot dogs. The hot dogs will be first made by the making machines and then sent to a hot dog pool [Note 1]. The packing machines will take hot dogs from the pool for packing. The manager of the factory specifies the number of hot dogs need to be done, N [Note 2]. There is a log file containing records for logging when a hot dog is packed. When all the hot dogs are done, the manager will append a summary to the log file.
Tasks
Using Pthreads and mutex, design and implement a multithreaded C++ program under Linux that works as follows:
Each making machine is modeled with a making thread (created by the master thread).
A making machine starts with checking the remaining number of hot dogs to be made.
The making machine makes one hot dog at a time.
The making machine takes 4 to 6 seconds [Note 3] to make one hot dog.
After a hot dog is made, the making machine takes 1 to 2 seconds to send the hot dog together with its machine id, i, to the hot dog pool.
Once the making machine finishes sending the hot dog, it can start to make the next hot dog.
When the required number of hot dogs is made, the making machine stops
Each packing machine is modeled with a packing thread (created by the master thread).
A packing machine starts with checking if there is any hot dog in the pool.
If there are hot dogs in the pool, the packing machine takes 1 to 2 seconds to take a hot dog from the pool, one at a time.
After taking a hot dog from the pool, the packing machine takes 1 to 2 seconds to pack the hot dog.
After packing the hot dog, the packing machine writes a record to the log file in form of < hot_dog_id, i, j >, where hot_dog_id is a serial number of the packed hot dog (counting from 1 to N), i is the making machine id and j is the packing machine id.
The packing machine takes 1 to 2 seconds to write a record to the log file.
Once the packing machine finishes writing the record, it can start to pack the next hot dog.
When all the hot dogs are packed, the packing machine stops.
A master thread is created to model the manager of the factory.
The manager specifies the number of hot dogs to be done, N.
The manager initiates 30 making and 25 packing machines with machine id i and j respectively, where 1 <= i <= 30 and 1 <= j <= 25.
The manager has to wait until all machines stop, i.e., all hot dogs are made and packed and all records are written in the log file.
After all machines stop, the manager reads the log file and appends the following summary to the log file:
The number of hot dogs made and packed by each machine, in form of < machine type (M or P), machine id (i or j), number of hot dogs made or packed >
The total number of hot dogs made and packed, in form of < total number of hot dogs made, total number of hot dogs packed>
Notes:
1. There is NO specific insertion/removal order for the hot dog pool. If you need to enforce a certain order, specify this assumption clearly as a comment in your program.
2. The number of hot dogs need to be done should be an input value to your program
3. To simulate the time taken for a thread to finish a step, you can make the thread sleep for a random period of time. For example, sleep(rand()%3+4) can be used to simulate the time taken for a making machine to make a hot dog, ranging from 4 to 6 seconds (http://linux.die.net/man/3/sleep, http://www.tutorialspoint.com/cplusplus/cpp_numbers.htm).
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.