The structure of a writer process do { wait(rw_mutex); ... /* writing is perform
ID: 3861581 • Letter: T
Question
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
Explanation / Answer
When R1 is reading data:
mutex = 1
rw_mutex = 0
read_count = 1
When W1 comes, it cannot enter writing state since the rw_mutex is 0 // wait(rw_mutex);
When R2 comes, it does not wait and start reading because
read_count now becomes 2 <<-- read_count++
since read_count is 2, it does not wait for rw_mutex <-- if (read_count == 1) wait(rw_mutex)
and it can start reading //reading section
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.