CIS 307: Homework 4: Using Threads and Locks

Given March 27, due April 7 at 7pm.

This homework is a simple continuation of Homework 3. Now within the RAND_PROC process we will have two threads. One thread [we will call it the main thread] will interact with the user by displaying the menu and carrying out the user's request. The other thread [we will call it the access thread] will do the read and update operations on the shared store. The two threads interact because they share some data, the statistics data. You will use a mutex lock to make sure that access to the statistics data in the two threads takes place in mutual exclusion. In the statistics data area you will also use a variable "terminate" to inform the access thread that it is time to terminate. Another change from homework 3 is that now a user can also ask to see the current content of the log file. Here is a possible structure for your program:

   MAIN PROGRAM (and main thread):
     Initialization;
     Set terminate to false;
     Create lock mutex;
     Create the access thread to executes the MOO function;
     loop
       Display Menu;
       if user wants the statistics then
          lock mutex;
            print out statistics;
          unlock mutex;
       else if user wants to see the log then
          print out the log file;
       else
          lock mutex;
            Set terminate to true;
          unlock mutex;
          Wait for the access thread to be done (use pthread_join);  
          Clean up and exit;
     forever;

   FUNCTION MOO (executed by access thread):
     initialization including access to shared memory;
     loop
       create command;
       start execution of command;
       lock mutex;
         if terminate then
            break out of the loop;
         update statistics;
       unlock mutex;
       sleep;
       complete execution of command;
       lock mutex;
         if terminate then
            break out of the loop;
         update statistics;
       unlock mutex;
       sleep;
     forever;
     unlock mutex;
     Clean up (including shared memory segment);
     exit (use pthread_exit);