reshown by
1,417 views
1 votes
1 votes

Why in reader section we are locking(I highlighted the locking) read_count-- and if section?

If we don't use locking what problems might arise?

I know that we should use locking whenever we access shared variable but in this particular case I don't see any problem if we don't use locking(highlighted).

Please explain what I am missing

reshown by

2 Answers

2 votes
2 votes
Let us consider that read_count = 7, means there are 7 readers processes are there in the critical section and they want to leave it.

In assembly language, $\textbf{read_count --}$ would be implemented as

1. READ temp, read_count

2. SUB temp, 1

3. STORE read_count, temp

Consider that 3 processes want to leave the critical section at the same time and when $\textbf{wait(mutex)}$ statement is not there then all leaving processes read statement 1. and wait preempt for short duration, then all 3 decrement temp value and store it to read_count and at the end read_count will be 6 instead of 4. Which means readers are still present in the critical section even though they are not. So in that case writer may not even get the chance to enter into the critical section or there are no readers in the critical section but still read_Count will show some reader processes are there in the critical section.
0 votes
0 votes
In that we are accessing a shared variable i.e. read_count, so we need to ensure Mutual exclusion during its access.

Why if we don't? Then we will have race condition and final value of read_count will depend on sequence probably of access leading to inconsistency.

Related questions