edited by
2,284 views
1 votes
1 votes
int readers_count = 0;
semaphore mutex = 1; // binary semaphore
semaphore db = 1;    // binary semaphore

void reader() {
  while(TRUE) {
 x. down(mutex);    //or  wait(mutex) or P(mutex)
    readers_count = readers_count + 1;
 y. if(readers_count == 1) down(db);
    up(mutex);       // or signal(mutex) or // V(mutex)

    < access DB >
    
    down(mutex);
    readers_count = readers_count - 1;
 p. if(readers_count == 0) up(db);
 q. up(mutex);       //or signal(mutex) or // V(mutex)
  }
}

void writer() {
  while(TRUE) {
 z: down(db);

    < access DB >
    
    up(db);
  }
}

What happens when positions of lines p and q are interchanged ?

  • a. No problem, the solution works fine.
  • b. Multiple readers and writers are allowed in the database at the same time.
  • c. There is possibility of deadlock.
  • d. None of the above.
edited by

2 Answers

0 votes
0 votes
p. if(readers_count == 0) up(db);
 q. up(mutex);  
 1. Before it, we donot have writer. We bring mutex up in code. Take an example. Say rc=0 ; Last reader say R3 is 
leaving it makes rc=0. Now still db is not allowed. We have up mutuex. So a reader r1 enters and down mutex check 
rc ==1 then tries to down db but it cannot and deadlock occurs.

 

0 votes
0 votes

the point of attention was there is violation of mutual exclusive excess of reader_count in end part of reader’s process code which will cause the deadlock in system but how? try yourself !

edited by

Related questions

0 votes
0 votes
1 answer
3