Reader Writer


Database
  •  
  •  
  •  
  •  
  •  
Action on Database
State
  • Writer
  • Readers
  • Mutex
  • Waiting
  • Reader Count
What's going on?
Writer Active
void *writer() {
  sem_wait(&database);
  writeDatabase();
  sem_post(&database);
}
Reader Active
void *reader() {
  sem_wait(&mutex);
  readerCount++;
  if (readerCount ⚌⚌ 1) {      
    sem_wait(&database);
  }
  sem_post(&mutex);
  read_data();
  sem_wait(&mutex);
  readerCount--;
  if(readerCount==0){
      sem_post(&database);
  }
  sem_post(&mutex);
}