Your solution is to be written in C
There are three kinds of threads: c1, p1 and s1. C1 and p1 threads produce c1 and p1 respectively that need to be assembled to create a sale package. Once two p1 and one c1 are available, the s1 thread consumes two p1 and one c1 to produce a sale package.
You do not want extra c1 or p1. Therefore, you must guarantee that the producing threads (c1 and p1) will wait to produce more elements until after a sale package is created.
In other words:
• If a c1 thread produces a c1 when no p1 are present, it has to wait for two p1 to be produced.
• If a p1 thread produces two p1 when no c1 is present, it has to wait for a c1 to be produced.
It takes a random amount of time, between 2 and 5 seconds, to create each p1 or c1. You may find sleep() and rand() useful functions for you to use.
Write synchronization code for p1 and c1 producers, as well as the shipper, that enforces these constraints. Note that:
• There is one p1 thread and one c1 thread.
• There is a s1 thread that consumes two p1 and one c1 to produce a sale package.
When an element is created you are to output what was produced. For example,
We have a p1.
We have a c1.
When you have two p1 and one c1, they are consumed to form a sales package. For example,
We now have a s1!
Your program will run for n seconds such that your threads will not start a new sales package after that time, where n is specified on the command line (ie. 100 seconds):
./a.out 100
Your solution is to be written in C There are three kinds of threads: c1, p1 and s1. C1 and p1 threads produce c1 and p1
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am