(Latest Revision: 11/17/99)

11/17/99: Moved back due dates to Nov 30 and Dec 07.

CS 3750 Second Concurrent Programming Assignment


Due Tuesday, November 30 -- psuedo-code description of the algorithm for your solution.

Due Tuesday, December 07 (in two separate e-mail messages -- please no "enclosures" or attachments.") -- the completed program source code in one e-mail message -- one or more script(s) documenting thoughtful, adequate, intelligent testing

Step 1

Read the file concStripped.c. It shows how you can spawn threads in a mach task and use semaphores to synchronize their activities. The file also illustrates the use of a random number generator.

After studying concStripped.c, examine conc.c. This file is functionally nearly identical to concStripped.c. The differences are firstly that conc.c is set up to write many messages to the screen, secondly that conc.c has some (commented-out) code that can be used to delay threads, and thirdly that conc.c has (commented-out) code that changes an operation that may be implemented atomically into one that will not be atomic (unless the compiler is too determined to "optimize").

All these things are useful for testing the code.

The messages provide a sort of (no pun intended) running commentary that allows the programmer to run the program and then figure out what each thread did, and when it did it relative to the time lines of the other threads.

The other coding variations affect timing characteristics thus helping to search for latent, time sensitive errors.

Optional Reading

After looking at conc.c you may want to examine sem.h and sem.strp, which implement counting semaphores that behave precisely according to the specifications for counting semaphores in your text book. The file sem.c is another version of sem.strp that contains code for printing out more helpful "commentary" messages.

You may also look at files machThreads and C-ThreadsFunctions to learn more about mach threads and the cthreads package of function calls. The cthreads package is used to implement semaphores in sem.c and sem.h. There is a current POSIX standard called pthreads used on unix and windows platforms for thread programming. It differs very little from the cthreads application programmer interface (API).

Step 2.

Do the concurrent programming problem described in the problem description.

For synchronization of concurrent threads, use only the counting semaphores implemented by sem.h and sem.c The one exception is that, just as conc.c does, you are allowed to use the mutex_t called stdoutLock to lock stdout for printing messages.

In particular, there must be no spinlocks, no busy waiting of any kind used in your program, with the exception of whatever (hidden) busy waiting might be involved in the implementation of the semapore and mutex_t operations.

When I say that the semaphores must be used to do all the "synchronization" of concurrent threads, I mean any critical section problem or any protocol that controls the chronological order in which threads interleave their actions. If that's not sufficiently clear, ask me about it, because your grade will count heavily on this issue.

Incidentally, all that you have to do for this assignment is write a main program. Let's say the name of your program is packets.c. To interface this program with other modules, please place the following statements at the beginning of packets.c:
#include <stdio.h>
#include <cthreads.h>
#include "sem.h"

extern sim_semaphore create_sim_sem() ;
extern void wait_sem() ;
extern void signal_sem() ;

     /* Set this to 1 if you want lots of "debug printf's" coming
        from the code in sem.c */
int  checking ;          

      /* Use this flag to turn on checking in your main program file. */
int  my_checking ;          

mutex_t stdoutLock ;    /*  for locking stdout. */


Then compile packets.c with the sem.c module by typing the following on the command line:
cc sem.c  packets.c
If you do this, all you have to send me is the main module, packets.c, and I will compile it on a NeXT with my own copies of sem.h and sem.c in the same directory, so it will work for me if it worked for you.

For this assignment, you may work in teams of up to two (2) people. If you work in a team, make sure that both names are in your header comment!

Step 3.

E-mail 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!) Send it to john@ishi.csustan.edu by the due date. I will compile and run the program as a part of the grading procedure.

Your source file should contain lots of extra code like the code conc.c has for printing messages and changing timing. Your program has to do a good job of being self-testing. Provide flags like the one called checking in conc.c that I can use to turn your messages and other testing features on and off. Keep separate the flag that turns on checking in sem.c from the flags that turn on checking in your program.