Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A file is shared between several reader and writer threads. Design a monitor to

ID: 3603403 • Letter: A

Question

A file is shared between several reader and writer threads. Design a monitor to control the access of the file by the different threads so that the following constraints are satisfied:i) at most one writer can be active on the file at a particular time. (ii) When a writer is writing to the file, no reader can read from the file. (iii More than one reader can be reading from the file simultaneously. (iv) When a writer is waiting to write, no more new reader should be allowed to read. (v) When a writer is writing and some other writer is waiting to write, then the writer is given more preference over a reader waiting to read. The general structure of each reader and writer thread is shown in the following: Monitor FileControl // Definition of the monitor class to be filled in by you FileControl fc; // An instance of the monitor Writer Thread: while (True)( Reader Thread: while (True) f fc.WriterEntry(): Write (file) fc.WriterExit(); fc.ReaderEntry() Read (file); fc.ReaderExit(); Fill in the pseudo-code for the monitor FileControl as shown above.

Explanation / Answer

The complete pseudocode for the monitor FileControl is given bellow. The explaination is given as the comment within the code itself.

Monitor FileControl {
    /* Shared Data of the Monitor */
    condition okToRead, okToWrite;
    int readerCount = 0; // number of readers currently reading the file simultaneously
    boolean busy = false; // busy is true means one writer is writing to the file
  
    /* Oprtaions of the Monitor:ReaderEntry(),ReaderExit(), WriterEntry() and WriterExit() */
    procedure ReaderEntry()
    {
        if (busy)           // if already writing, block the reader.
            okToRead.wait();
        readerCount++;         // incrementthe readerCount.
        okToRead.signal();     // Once one reader can start, all readers can start simultaneously.
    }

    procedure ReaderExit()
    {
        readerCount-- ;  
        if(okToWrite.Queue)    // When a Reader exit, Writers are given more preference over Reader to enter.
           okToWrite.signal();
        else
           okToRead.signal();
    }


procedure WriterEntry()
{
    if ( busy || readerCount != 0 )
       OKtoWrite.wait();
    busy = true;
}

procedure WriterExit()
{
    busy = false;
    If (okToWrite.Queue)   // When a Writer exit, anotherWriters are given more preference over Reader to enter.
      okToWrite.signal();
    else
      okToRead.signal();
   }
}

Since more than 1 reader can read simultaneously (point iii), we need the readerCount variable. Since only 1 writer can write at a time (points i and ii), this can be handled by a boolen variable busy.

The priority of writter over reader(points iv and v) is given by the code

If (okToWrite.Queue)  
      okToWrite.signal();
else
      okToRead.signal();

NB: Hope the comments in the code helps you to understand the FileControl monitor. Let me know if anything else is needed from my side. Please provide Feedback to help you better.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote