Reader/Writer Problem with Writer Priority:

Using general semaphores, create a solution to the reader/writer problem such that writers have priority over readers. When writers have priority, the following rules exist:

Your BACI program should consist of the following three procedures:

  1. Coordinator: This procedure should be executed before any readers or writers are executed, and should be used to initialize the semaphores and any other global variables that require initialization.
  2. Reader(int reader): Multiple copies of this procedure may be executed from the cobegin/coend block; reader is the id of this particular reader.
  3. Writer(int writer): Multiple copies of this procedure may also be executed from the cobegin/coend block; writer is the id of this particular writer.

Use two global declarations to declare the number of readers/writers in the system:

const int Rnumber= 5; //number of readers//
const int Wnumber= 3; //number of writers//
If the above number of readers/writers are defined, then main() will be:
       main() {
         Coordinator;
         cobegin {
             Reader(0);
             Reader(1);
             Reader(2);
             Reader(3);
             Reader(Rnumber-1);
             Writer(0);
             Writer(1);
             Writer(Wnumber-1);
         //add-delete lines when the number of readers or writers change.
         //NOTE: BACI can only handle about 10 threads at a time
         }
       }

In both the Reader and Writer procedures, during the actual reading/writing phase of the shared file, have a for loop of length three that prints to stdout "... reading is performed by process I ..." (or "... writing is performed by process J ..."), where I/J is the id value of the reader/writer. In addition, both the Reader and Writer procedures should print a message to stdout just before they perform a P() or V() operation on any of the semaphores. In the message, each thread must identify itself as a reader or a writer, the id of the reader/writer, the name of the semaphore variable upon which it is operating, and the value of the semaphore variable at that time.

Written deliverables:

  1. A listing of your code (*.lst).
  2. The results from executing your program with the following number of readers/writers:
    1. Rnumber= 5 and Wnumber= 3
    2. Rnumber= 6 and Wnumber= 1
    3. Rnumber= 1 and Wnumber= 6
  3. A brief explanation of how your solution gives writer priority.