Thursday 30 June 2016

Starvation Solution for Reader Writer Problem using Semaphore

Starvation Solution for Reader Writer Problem using Semaphore

Description: The solution of the Readers-Writers problem usually given may starve the writers. So, following is a starvation-free solution for Reader Writer problem using semaphore.


semaphore rw_mutex=1;
semaphore mutex=1;
semaphore turn=1;     //new added line
int read_count=0;

//Writer Process

do{
   wait(turn);        //new added line
   wait(rw_mutex);
   signal(turn);      //new added line
  
   /* Writing is Performed */
  
   signal(rw_mutex);
}while(true);


//Reader Process

do{
   wait(turn);        //new added line
   wait(mutex);
  
   read_count++;
   if(read_count==1)
      wait(rw_mutex);
   signal(turn);      //new added line
  
   signal(mutex);
  
   /* Reading is Performed */
  
   wait(mutex);
  
   read_count--;
   if(read_count==0)
      signal(rw_mutex);
  
   signal(mutex);

}while(true);


No comments

Post a Comment

Recent Posts