There are three kinds of threads: canoe, paddle and shipper. Canoe and paddle threads produce canoes and paddels respect
Posted: Thu May 26, 2022 9:46 am
There are three kinds of threads: canoe, paddle and shipper.
Canoe and paddle threads
produce canoes and paddels respectively that need to be assembled
to create a sale
package. Once two paddles and one canoe are available, the shipper
thread consumes two
paddles and one canoe to produce a sale package.
You do not want extra canoes or paddles. Therefore, you must
guarantee that the
producing threads (canoe and paddle) will wait to produce more
elements until after a
sale package is created.
In other words:
• If a canoe thread produces a canoe when no paddles are present,
it has to wait for
two paddles to be produced.
• If a paddle thread produces two paddles when no canoe is present,
it has to wait
for a canoe to be produced.
It takes a random amount of time, between 2 and 5 seconds, to
create each paddle or
canoe. You may find sleep() and rand() useful functions for you to
use.
Write synchronization code for paddle and canoe producers, as well
as the shipper,
that enforces these constraints. Note that:
• There is one paddle thread and one canoe thread.
• There is a shipper thread that consumes two paddles and one canoe
to produce a
sale package.
When an element is created you are to output what was produced. For
example,
We have a paddle.
We have a canoe.
When you have two paddles and one canoe, they are consumed to form
a sales package.
For example,
We now have a shipment!
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
Canoe and paddle threads
produce canoes and paddels respectively that need to be assembled
to create a sale
package. Once two paddles and one canoe are available, the shipper
thread consumes two
paddles and one canoe to produce a sale package.
You do not want extra canoes or paddles. Therefore, you must
guarantee that the
producing threads (canoe and paddle) will wait to produce more
elements until after a
sale package is created.
In other words:
• If a canoe thread produces a canoe when no paddles are present,
it has to wait for
two paddles to be produced.
• If a paddle thread produces two paddles when no canoe is present,
it has to wait
for a canoe to be produced.
It takes a random amount of time, between 2 and 5 seconds, to
create each paddle or
canoe. You may find sleep() and rand() useful functions for you to
use.
Write synchronization code for paddle and canoe producers, as well
as the shipper,
that enforces these constraints. Note that:
• There is one paddle thread and one canoe thread.
• There is a shipper thread that consumes two paddles and one canoe
to produce a
sale package.
When an element is created you are to output what was produced. For
example,
We have a paddle.
We have a canoe.
When you have two paddles and one canoe, they are consumed to form
a sales package.
For example,
We now have a shipment!
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