1 This monitor is a model of a database with multiple readers and 2 writers. The high−level goal here is (a) to give a writer exclusive 3 access (a single active writer means there should be no other writers 4 and no readers) while (b) allowing multiple readers. Like the previous 5 example, this one is expressed in pseudocode. 6 7 // assume that these variables are initialized in a constructor 8 state variables: 9 AR = 0; // # active readers 10 AW = 0; // # active writers 11 WR = 0; // # waiting readers 12 WW = 0; // # waiting writers 13 14 Condition okToRead = NIL; 15 Condition okToWrite = NIL; 16 Mutex mutex = FREE; 17 18 Database::read() { 19 startRead(); // first, check self into the system 20 Access Data 21 doneRead(); // check self out of system 22 } 23 24 Database::startRead() { 25 acquire(&mutex); 26 while((AW + WW) > 0){ 27 WR++; 28 wait(&okToRead, &mutex); 29 WR−−; 30 } 31 AR++; 32 release(&mutex); 33 } 34 35 Database::doneRead() { 36 acquire(&mutex); 37 AR−−; 38 if (AR == 0 && WW > 0) { // if no other readers still 39 signal(&okToWrite, &mutex); // active, wake up writer 40 } 41 release(&mutex); 42 } 43 44 Database::write(){ // symmetrical 45 startWrite(); // check in 46 Access Data 47 doneWrite(); // check out 48 } 49 50 Database::startWrite() { 51 acquire(&mutex); 52 while ((AW + AR) > 0) { // check if safe to write. 53 // if any readers or writers, wait 54 WW++; 55 wait(&okToWrite, &mutex); 56 WW−−; 57 } 58 AW++; 59 release(&mutex); 60 } 61 62 Database::doneWrite() { 63 acquire(&mutex); 64 AW−−; 65 if (WW > 0) { 66 signal(&okToWrite, &mutex); // give priority to writers 67 } else if (WR > 0) { 68 broadcast(&okToRead, &mutex); 69 } 70 release(&mutex); 71 } 72 73 NOTE: what is the starvation problem here? 74