548 views
Consider the below code for synchronizing the classical readers and writers.

int rc = 0;
int c = 0;
Semaphore mutex =1;
Semaphore db =1;
{
L1: down (mutex):
if (c ! = 0 && rc! = 0)
{
up (mutex);
goto L1;
}
else
{
rc = rc + 1;
up (mutex);
if (rc = = 1) down (db);
D.B //database
down (mutex);
rc = rc - 1;
if (rc = = 0) up (db);
up (mutex);
}
}
void Writer(void)
{
while(true)
{
c = c+1;
down(db);
D.B
up(db);
c = c-1;
}
}

Consider the below statements. Which of the following is true?
a) No problem, the solution is working fine.
b) It is possible for both reader and writer accessing the database at the same time.
c) more than one writers can access the database at the same time.
d) None of these.

### 1 comment

​​​
int rc = 0;
int c = 0;
Semaphore mutex =1;
Semaphore db =1;
{
1. L1: down (mutex):
2.
if (c ! = 0 && rc! = 0)
3. {
4. up (mutex);
5.  goto L1;
6 }
7. else
8.  {
9. rc = rc + 1;
10.  up (mutex);
11.  if (rc = = 1)
12.            down (db);
13.  D.B //database
14.     down (mutex);
15.     rc = rc - 1;
16.      if (rc = = 0) up (db);
17.  up (mutex);
18.   }
19.}
void Writer(void)
{
while(true)
{
c = c+1;
down(db);
D.B
up(db);
c = c-1;
}
}
At line 9 process P1 increment rc by one rc=rc+1=0+1=1 then up (mutex )
suppose here P1 preempt then another process P2 come and make rc=1+1=2 here rc=2 so skip 11and 12 line  and enter into CS
P2 skip 11 line so writers also enter into CS with P2
Hence break mutual exclusion

please share book name where it is mentioned that  preemption is possible while considering the case of mutual exclusion
thanx for this

1 vote