(Last revision 10/14/2001)
CS 3750 First C++ Concurrent Programming Assignment (prog
#1)
1. Look over the contents of the directory "ThreadInfo" in our
web space. Read the file HiHo.cpp in that directory. It
illustrates everything you need to know about using function
calls from the pthreads package on the Sun Ultras, and about
using the queuing semaphores implemented by the sem.cpp and sem.h
files. Make copies of sem.cpp and sem.h for your own use. (We
will go over this in class.)
2. Use the queuing semaphores to implement a "cheerleader
pyramid" program that spawns 15 cheerleaders (threads), assigns
each of them a position in the pyramid, and causes them each to
execute a C++ function that writes a message to standard output
when they take their position in the pyramid.
Just as a real pyramid must be built from the ground up, so must
be the pyramid created by the threads in your program. The
threads will have serial numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, and 15. The pyramid they are trying to build
looks like this:
01
02 03
04 05 06
07 08 09 10
11 12 13 14 15
Thread #01 cannot join the pyramid until threads #02 and #03 are
in position. Thread #02 cannot join until threads #04 and #05
are in position. Similarly, #03 must join after #05 and #06.
In general if a thread X is not part of the base, X must wait for
it's supporting threads to get into position before X can get
into position.
It will be the job of your program to synchronize the activities
of the threads and to see to it that the messages the threads
write come out in an order consistent with the constraints above.
The synchronization that accomplishes the above must be done by
employing the semaphore data types implemented by the files
sem.cpp and sem.h. You don't get credit if you don't use
semaphores to do this.
In *addition* to the constraints mentioned above, imposed by the
shape of the pyramid, there is also a need to enforce mutually
exclusive access to standard output. The constraints above are
not enough to guarantee that (say) thread #4 will not try to
write its message concurrently with #6. This could cause output
from #4 and #6 to be written to the screen in a jumbled
interleaving.
We want the message of each thread to be written atomically, on
its own separate line of the output. This is really a separate
problem. I will solve this one for you by telling you exactly
what to do. Let's cover that topic in class, since there are
some details I don't want to go into here.
All critical section problems you encounter must be solved in
such a way that the mutual exclusion, progress, and bounded
waiting conditions are all guaranteed. All synchronizations done
must guarantee freedom from all forms of indefinite postponement.
The program must be written so that there is a "mother" task that
creates the 15 cheerleader threads. The cheerleaders must notify
the mother after they have finished their message, and then exit.
The mother must make sure that all the cheerleaders have finished
with their messages before she exits.
3. Figure out a protocol that allows the mother and threads to
cooperate to do the job indicated above. Once you have decided
on the protocol, write it down in pseudo-code form and e-mail it
to me by the first due date. This design should be in the form
of two or more procedure listings, showing what the threads and
the mother process will be doing while the program is running.
By the second due date, send a complete, compilable, source
listing with adequate comments (The bottom line on the question
of documentation is that I need to be able to read and understand
your program!) Also, place copies of test scripts in a separate
file, and e-mail that to me too.
(Beware: since some errors in concurrent programs are
timing-dependent, you will need to think of some novel ways to
test your program. The file HiHo.c illustrates this. Ask for
more details in class.) All you have to send me is the main
module, pyramid.cpp, and I will compile it on a Sun Ultra with my
own copies of sem.h and sem.cpp. Your program should work for me
if it worked for you. For this assignment, you may work in teams
of two (2) people. If you work in a team, make sure that both
names are in your comments!
More Discussion of the Assignment
When you think about doing this assignment, imagine that the
mother thread and each cheerleader thread will run on a separate
CPU of a tightly-coupled multiprocessor. Of course, our Sun
Ultra computing systems are not tightly-coupled multiprocessors.
Nevertheless, the point of this exercise is for you to write a
program that will work on *any* computing system on which
multiple processes can execute concurrently while sharing
variables.
A tightly-coupled multiprocessing system can be diagrammed this
way:
CPU CPU CPU CPU CPU CPU CPU MEMORY
| | | | | | | |
------------------------------------------------------------ BUS
| | | | | | | | |
CPU CPU CPU CPU CPU CPU CPU CPU CPU
Here we have a RAM memory being shared over a common bus by
several CPU's. It is possible that the different CPU's have
radically different speeds. Speed can be affected by the
inherent power of the CPU, or by the current load on the CPU.
Besides that, it is impossible to tell which CPU will be assigned
to which thread. And also, each time we run the program, the
assignment can be different.
In your program, the mother process must create all the
cheerleaders without delay immediately after the program starts
running. The mother must do nothing to direct the creation of
the pyramid. In general it is a bad idea to create "traffic cop"
threads, because the traffic cop too often becomes a bottleneck.
When every action has to be approved by a master, events can only
happen as fast as the master can approve them. This would defeat
the purpose of parallel processing.
The only interactions a cheerleader is allowed to have with its
mother, after its creation, is that each cheerleader will somehow
notify the mother after it has positioned itself in the pyramid
and written its message to standard output. This is done so that
the mother can know when to exit.
The cheerleaders must cooperate among themselves, using
semaphores for synchronization, to create the pyramid. As to the
decision of when a cheerleader X moves into position, only X and
the cheerleaders that will be supporting X are allowed to
participate in the making of that decision.
The general rules for submitting assignments by e-mail are:
- Always send me e-mail as plain text in the main message
body. Never send me attachments.
- When you send a program source file, you also send
a script
of your tests. Combine the source file and the script
into one
shell archive (shar) file.
- I will tell you what subject line to use with each
message, and I need you to use exactly the
subject lines I give you. (I get hundreds of e-mail
messages at a time and your subject line allows me to
sort messages.)
- Send each submission before midnight on the due date.
Here is the list of things you have to turn in:
- On the first due date e-mail a
psuedo-code description of the algorithm for your
solution. Use this subject line:
CS3750,prog1,pcode
- On the second due date e-mail a
shar file containing:
- the completed program source code
- one or more script(s) documenting thoughtful,
adequate, intelligent testing
Use this subject line: CS3750,prog1,final.
Note that there are no spaces in the subject lines given. It is
important that you do not insert any spaces. My e-mail address
is:
john@ishi.csustan.edu.
DUE DATES:
For due dates, see
the class schedule.