======================================================================
CS 25200 SYSTEMS PROGRAMMING Summer 2018
Project #4
======================================================================
Name: _____________________________________
======================================================================
Part 0: Introduction and Setup
----------------------------------------------------------------------
Login to a CS department machine (a lab machine or data.cs.purdue.edu),
navigate to your preferred directory, and run
$ cd
$ cd cs252
$ git clone /homes/cs252/Summer2018/repos/$USER/lab4-src.git
$ cd lab4-src
======================================================================
Part 1: Producer/consumer Problem
----------------------------------------------------------------------
Using the pthreads API, implement producer/consumer communication through a
bounded buffer. The solution to the bounded buffer problem was described in
class. Test your solution with a multi-character buffer and with multiple
producers and consumers. Of course, with multiple producers or consumers,
the output display will be gobbledygook. However, note that a correct
solution will not produce arbitrary output! Make sure your solution can be
run using the command line
./part1 [numproducers numconsumers]
The output should be in the form.
Thread X produced H
Thread Z consumed W
X and Z in this example are numbers, ranging from 0 to
numproducers/numcomsumers.
The string to be used is "Hello, world!", i.e. the producers will be
producing characters from this string. Once a producer has inserted all
characters from the string, it should terminate. Once a consumer has
consumed strlen(producer_string) characters it should also terminate.
You should define the following functions:
void *producer(void *ptr);
void *consumer(void *ptr);
Your main() function should use pthread_create() to spawn the appropriate
number of producer and consumer threads.
There are many ways to implement this correctly. You can use semaphores,
mutexes, and/or condition variables. It is entirely up to you.
Each producer should be numbered 0 to N. The same goes for consumers.
======================================================================
Part 2:
----------------------------------------------------------------------
The atmosphere on the planet Coruscant consists primarily of the elements
Nitrogen and Oxygen and of their compound Nitrogen diOxide (N02). Trace
quantities of Ozone (O3) have also been discovered. There are 2 primary
chemical reactions that occur in the atmosphere:
1) N + N -> N2
2) O + O -> O2
In addition, the following two chemical reactions take place
a) N2 + 2O2 -> 2NO2
b) 3O2 -> 2O3
The scientists of the Rebel Alliance have asked us to help out with the
terraforming of the planet Sullust. They require a program that:
a) Injects atoms of Nitrogen and Oxygen into the atmosphere
b) Monitors the formation of the N2, O2, N02 and O3 molecules
c) Prints out messages at every stage
In order to solve this problem, you may think of each atom as a separate
thread. Everytime a Nitrogen atom is created, you will have to print out
the message "An atom of nitrogen was created." Similarly, every time an
atom of Oxygen is created, print out the message "An atom of oxygen was
created".
Every time a molecule of either Nitrogen or Oxygen is created, print out
the message "Two atoms of nitrogen (or oxygen) combined to produce one
molecule of N2 (or O2)".
NO2 and O3 are created when enough molecules of the requisite compounds are
present. Remember that the formation of NO2 and O3 are molecular and not
atomic operations. Thus, from equation (a), you need one molecule of
nitrogen and 2 molecules of oxygen to produce 2 molecules of NO2.
Whenever reaction (a) takes place, print out the message "One molecule of
N2 and two molecules of O2 combined to produce two molecules of NO2".
If, instead, Ozone was created, print out the message "Three molecules of
O2 combined to produce two molecules of O3".
Here is one method of implementing the solution...
1. Each N atom invokes a procedure called nReady when it's ready to
"react" and prints out the appropriate message (see above).
2. Similarly, each O atom invokes oReady when it's ready to react.
Again, print out messages.
3. A procedure called makeNitrogen is called when a molecule of
Nitrogen is formed. The corresponding procedure for Oxygen is called
makeOxygen.
There are a number of ways of proceeding from this point on. You may think
of makeNitrogen and makeOxygen as being both producers and consumers (since
they "consume" 2 atoms to "produce" a molecule of N2/O2 and they then
"consume" one molecule of N2/ two molecules of O2 to "produce" of NO2).
Another procedure called makeNO2 is then the "consumer" for NO2. You may
similarly have a procedure for O3 called makeOzone.
The trick is to synchronize appropriately between all of these procedures.
Feel free to use semaphores or mutexes and condition variables to effect
the synchronization.
======================================================================
Part 6: Turn in your files
----------------------------------------------------------------------
Just like the previous labs, every time you run make, your files are pushed
to a git repository. So make sure you do not change the provided Makefile.
A submission without a git repository will not be graded. This lab is due
Sunday 07/22/2018 at 11:58pm.