CSCI4630 - Fall 2003 - Assignment 2
Due Wednesday, 10/22/03



The point of this project is to demonstrate inter-process communication and synchronization, except that we will be working with threads instead of processes.

You will be writing a game to generate Jumbles for the user to solve.  A typical run of the game is shown to the right, where the user's input is shown in italics, and the shell's command prompt looks like "% ".

Globals:
string jumbles[5]      <should hold 5 scrambled words>

Threads:
The main thread should welcome the user to the game, check for a "times" file called JumbleTimes (in the directory containing the program) and create a file by that name if it doesn't already exist, containing the lines:

   best = 0
   worst = 0
   average = 0
   times

The main thread should then launch a scramble thread which will serve as a producer of jumbled words.  It does this by reading sequentially from a file containing 5-letter words, randomly scrambling them (however you want) and filling the shared jumbles[] array with these scrambled strings.  Whenever the main thread needs these scrambles, it acts as a consumer and reads from the shared jumbles[] buffer.  Use semaphores, as in the classical producer-consumer solution, to synchronize these threads' access to the buffer.

Main also starts a stats thread which reads the times from the JumbleTimes file and maintains the values of best, worst and average inside that file.  This thread should be started only once, and stopped only when the main thread exits (that is, the player quits).  Use semaphores to have this thread wake up when it needs to add a new time to the file, and sleep when it is done.
Sample Run


% jumble

Welcome to Jumble!

Unscramble:  DEARTH
> thread
That took you 3.2 seconds.
Play Again:  y

Unscramble:  SECORPS
> process
That took you 11.8 seconds,

Play Again:  y


Unscramble:  ANUT
> aunt
Nope!  Not what I'm thinking...
> tuna
That took you 7.6 seconds.

Play Again: 
x
Your times are 3.2, 7.6 and 11.8

Play Again: 
s
Best = 3.2, Worst = 11.8, Average = 7.5

Play Again:  y

Unscramble:  PURSY
> syrup
That took 6.8 seconds.

Play Again:  y


Unscramble:  UNXIL
>

...
Notes
You will probably need to introduce some additional global variables. 

The JumbleTimes file should be opened when the stats thread needs to read it and write it, but should otherwise stay closed.  This way if the program crashes, there is a high probability of the file maintaining its integrity.  Within the file, best should contain the fastest (least) time in the file, worst should contain the slowest, and average should contain the average.

When main asks the user if he wants to play again, the valid entries are:
   'y' whcih means "yes"
   'n' which means "no," and the program quits
   't' which means "display times," and
   's' which means "display statistics."

Helpful Links
Solaris Threads and Realtime Library Functions
Example using gettimeofday() function