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

1. Here are the codes for the reader-writer The structure of a writer process do

ID: 3861451 • Letter: 1

Question

1.       Here are the codes for the reader-writer

The structure of a writer process

do {
   wait(rw_mutex);

      ...
   /* writing is performed */

      ...

   signal(rw_mutex);

} while (true);

The structure of a reader process

do {
   wait(mutex);
   read count++;
   if (read_count == 1)

      wait(rw_mutex);

   signal(mutex);

   ...
   /* reading is performed */

   ...

   wait(mutex);
   read count--;
   if (read_count == 0)

      signal(rw_mutex);

   signal(mutex);

} while (true);

Shared Data

Data set

Semaphore rw_mutex initialized to 1

Semaphore mutex initialized to 1

Integer read_count initialized to 0

Assume: currently there is a Reader process R1 that is reading the data; firstly a Writer process W1 comes and then another Reader process R2 comes; what W1 and R1 will do? Please explain your answer based on the code

Answer:

Explanation / Answer

Hi,

This is classic example of reader writer problem of concurrency. We can see that there is a shared data structure,

a rw_mutex and mutex which are used by writers and readers.

Writer uses rw_mustex and reader uses mutex to place locks while working on shared data strutcure.

Current scenario :

1.) Reader R1 reading data => readCount is 1, rw_mutex is on wait and reader is holding lock on mutex

2.) Writer W1 comes => it tries to hold rw_mutex by calling wait(rw_mutex) but Reader R1 is holding rw_mutex so process W1 goes for wait for rw_mutex semaphore.

3.) Reader R2 comes and it will again try for reading semaphore to be released but R1 but R1 is still holding it so R2 will also wait.

This will result in deadlock as process are waitingin in cyclic fashion for the locks to be released.