EECS 371 - MP 5: Readers and Writers

Due: Tuesday April 24, 2001 at 11:59 pm

This assignment will simulate the readers and writers (reader priority) problem using UNIX System V IPC Semaphores and Shared Memory. Be sure to look at the code given in chapter six of Silbershatz and Galvin to help you with this assignment. There will be 5 memory elements (stored in shared memory) that the code can either read or write. Each memory element can have multiple reads occurring simultaneously, but each write operation must occur mutually exclusive of any other read or write operation. Writing to one of the these memory elements must not effect the reading or writing to any of the other four memory elements. That is, your program must be able to write to different memory elements simultaneously.

For the assignment, you will need to have your program accept command line arguments. The command line arguments will be a list of file names. The number of files names given will specify the number of processes that will be executing the readers and writers problem. Each file name will contain a sequence of commands that will control how a process will read and write the shared memory. Each line will only contain a single command. The first non-white-space character on the line will be the command character. The command characters can be in either upper or lower case. The commands are as follows:

Each filename given on the command line argument must be unique. This is because two processes cannot read from the same file at the same time on a UNIX system. If a filename is given multiple times, an error message should be given and the entire program exited. This check is best done by the parent/original process as soon as the program starts. After this, the parent should create the needed IPC elements, fork off the number of child processes equal to the number of files given on the command line, wait the the child process to finish and finally remove the IPC elements from the system. Since this program will have all IPC elements being used by processes that were created by the process that created the IPC elements, your program should use the value of IPC_PRIVATE for the key value. The five shared memory elements are to be initialized to the value of zero.

Each child/reader-writer process will begin by opening the proper file to read commands from. If the file does not open properly, print out some error message and exit that child process. After the file has been opened, the process will process the commands given in the file. If a command is unknown print out an error message and ignore that line in the file. The process is to continue processing commands until the end of the file is reached. The following are some sample data files:

  1. Data File
  2. Data File
  3. Data File
  4. Data File
  5. Data File

Both the read and write commands have there own entry sections, critical sections and exit sections (refer to the Silbershatz and Galvin text for the descriptions of these three sections). Immediately prior to executing the code for the entry section, print out a message that will list:

  1. the action to be performed (reading or writing)
  2. the shared memory element to be accessed (1 through 5)
  3. the process performing the operation (you decide how to uniquely identify each process)
  4. the value being written (for the write operation only)
Immediately after the execution of the code for the exit section, print out a message that will list:
  1. the action performed (reading or writing)
  2. the shared memory element accessed (1 through 5)
  3. the process performing the operation (you decide how to uniquely identify each process)
  4. the value that was read (for the read operation only)
Immediately after the accessing of the shared memory element in the critical section and BEFORE executing the code for the exit section, your program must sleep() for one second. This will simulate the time needed to perform that operation and allow us to see the blocking of processes actually occurring.

It is suggested that you write the program in steps. First have the program only create one reader-writer process. Then have the program create two reader-writer process. Finally have the program create N reader-writer process (where N is the number of filenames given on the command line). This should help reduce the possibility of creating a program that runs out of control and produces more children than can be allowed on the system; effectively crashing the system. Avoiding this with keep you from being the target of the wrath of EECS computer support. I also suggest that you become familiar with the following UNIX commands:

The assignment is expected to be the result of individual work. You will submit the project electronically using the UNIX turnin command with the project name of mp5 as shown below, where <files> is a list of all files that you wish to submit. Refer to the man page on the system for more information about the turnin command.

            $> turnin -c eecs371 -p mp5 <files>
All of your programs must be written in a good programming style. This is to include in-line comments, a file header, function headers, blank lines, indentation, meaningful variable names, readable output, etc. The first two lines of your file must comments stating how you compiled your program on the EECS department's UNIX machines and how you ran your program. You must also submit a makefile with your program that will properly compile your program.