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

Consider this monitor solution to the readers/writers problem: monitor Readers-W

ID: 3680174 • Letter: C

Question

Consider this monitor solution to the readers/writers problem:

               monitor Readers-Writers {

                   int read_cnt = 0, writing = 0;

                   condition OK_to_read, OK_to_write;

                   start_read() {

                           if (writing )

                                   OK_to_read.wait;

                           read_cnt++;

                           OK_to_read.signal;

                   }

                   finish_read() {

                           read_cnt--;

                           if (read_cnt == 0)

                                   Ok_to_write.signal;

                   }

                   start_write() {

                           if ((read_cnt != 0) || writing)

                                   OK_to_write.wait;

                           writing = 1;

                   }

                   finish_write() {

                           writing = 0;

                           if (!empty(OK_to_read))

                                   OK_to_read.signal;

                           else

                                   OK_to_write.signal;

                   }

               }

               reader:

                   …..

                   start_read();

                   /* reading here */

                   finish_read();

                   …..

               writer:

                   …..

                   start_write();

                   /* writing here */

                   finish_write();

                   …..

Assume the first request is a writer W1. While W1 is executing/writing, the following requests arrive in the given order: W2, R1, R2, W3, R3, W4.

(1) In which order will these requests be processed? Which groups of readers will be reading concurrently?

(2) Assume that two new readers R4 and R5 arrive while W2 is writing. For each of the two new readers, indicate when it will be processed. (i.e. following which write request)

Explanation / Answer

1)
(R1, R2, R3), W2, W3, W4

The parentheses indicate that R1 through R3 will all be executing concurrently; then W2 enters; followed by W3, and finally W4

2)
Both R4 and R5 will be processed between W2 and W