in Operating System edited by
2,283 views
1 vote
1 vote
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.
in Operating System edited by
by
2.3k views

4 Comments

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;
 q. up(mutex); //or signal(mutex) or // V(mutex) //Here buffer is empty and ready to go with next instruction, but still reader can stay inside, so writer cannot execute, only reader can go.
 p. if(readers_count == 0) up(db);//reader can reside inside the program, still showing reader=0, which is never possible. Even here writer is getting signal to execute. But not getting V(mutex). Means all writer getting simulteneous signal.
   }
}

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

    < access DB >
    
    up(db);
  }
}

1)Reader is empty or not, mutex is giving signal to next instruction.

2) Writer is getting signal, even if there are any reader is present in the process.

3) Writer is not getting mutex signal.

All these three conditions are causing deadlock

0
0

@Arjun Sir, Actually, I found some questions based on the following manipulations,

  • swapping the following lines
    x.down(mutex);    //or  wait(mutex) or P(mutex)
      readers_count = readers_count + 1;
  • swapping the following lines
    y. if(readers_count == 1) down(db);
        up(mutex);       // or signal(mutex) or // V(mutex)
    
  • and
        down(mutex);
        readers_count = readers_count - 1;

 I just tried one more variation. 

0
0
@DD plz help me. I have wasted 1 hour and am still not able to understand how it’s violating the condition. It looks like a standard Reader writer problem implementation.

https://www.tutorialspoint.com/readers-writers-problem#:~:text=The%20mutex%20semaphore%20ensures%20mutual,cannot%20access%20the%20object%20anymore.
0
0

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

1 comment

also reader and writer can enter in C.S at same time.
0
0