• edited by
1,414 views
3 3 votes
Consider the below code for synchronizing the classical readers and writers.

int rc = 0; 
int c = 0; 
Semaphore mutex =1; 
Semaphore db =1;
void Reader(void)
{ 
    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 Answer

3 3 votes
Answer B
 
​​​
int rc = 0;
int c = 0;
Semaphore mutex =1;
Semaphore db =1;
void Reader(void)
{
   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  
 
Position:
Show:

Related questions

1 1 vote
0 0 answers
1.1k
1.1k views
snaily16 asked Jan 12, 2019
1,091 views
Fails to Guarantee mutual exclusion Guarantee mutual exclusion and prevents deadlockFails to prevent deadlockFails to guarantee mutual exclusion and fails to prevent dead...
2 2 votes
1 1 answer
818
818 views
charul asked Jan 24, 2018
818 views
does this satisfy progress , if no then why?
3 3 votes
1 1 answer
1.7k
1.7k views
Rishabh Gupta 2 asked Jan 23, 2018
1,740 views
Consider two processes P1 and P2 accessing shared variable ‘x’ and ‘y’ protected by two binary semaphores Sx and Sy respectively both initialized to 1. ‘wait’ and ‘signal...
1 1 vote
0 0 answers
1.2k
1.2k views