(Last revision 10/22/2013)
CS 3750 First C++ Concurrent Programming Assignment (prog
#1)
- Look over the contents of the directory "ThreadInfo" in our
web space. Read the file HiHo.cpp.html in that directory. It
illustrates everything you need to know about using function
calls from the pthreads package on the CS Lab Macs, 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.)
- Starting with the skeleton program called pyramid.cpp,
use 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
write 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 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10,
11, 12, 13, and 14. The pyramid they are trying to build
looks like this:
00
01 02
03 04 05
06 07 08 09
10 11 12 13 14
Thread #00 cannot join the pyramid until threads #01 and #02 are
in position. Thread #01 cannot join until threads #03 and #04
are in position. Similarly, #02 must join after #04 and #05.
In general if a thread X is not part of the base, X must wait for
its supporting threads to get into position before X can get
into position.
It will be your job to add code to the 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 ANY credit unless you achieve
the results with semaphores.
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 #03 will not try to
write its message concurrently with #05. This could cause output
from #03 and #05 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 solved that one for you in the skeleton code.
Let's discuss that further in class.
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 cheerleader group
must somehow notify the mother after all the cheerleaders
have finished writing all their messages.
The mother has to make sure all the cheerleaders have finished
with their messages before she exits.
- 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. You should send me a copy of the
pyramid.cpp skeleton program with your pseudo code inserted
in the appropriate places - where the "real code" will go
later.
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 CS Lab Mac 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, but you don't have to if you don't want to.
If you work in a team, make sure that both names are in your comments!
If you don't understand something, ask me questions. Don't expect "magic
enlightenment."
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. The program may or may
not actually use multiple cores or CPUs. 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 CPUs. It is possible that the different CPU's have
radically different effective 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 of threads to CPUs 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.
I gave you a good skeleton program. Just add code in the places indicated,
so that the program starts working correctly. Don't rewrite the program
in other ways.
The only interactions ANY cheerleader is allowed to have with its
mother, after its creation, is that a cheerleader is allowed to
notify the mother after the cheerleader has positioned itself in the pyramid
and written its message to standard output.
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.