in Operating System edited by
791 views
3 votes
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.
in Operating System edited by
by
791 views

1 comment

0
0

1 Answer

3 votes
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  
 

2 Comments

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

Related questions