> released: 02/24, 11:00 > due: 03/02, 23:59 > > Answer the following questions. > Submit your answers to Canvas assignments. There is an entry for this homework. > > > 1. Read "Basic Threads Programming: Standards and Strategy" by Mike Dahlin. > (https://naizhengtan.github.io/22spring/notes/programming-with-threads.pdf) > > Write down the six rules: (6 points) > > 1. > 2. > 3. > 4. > 5. > 6. > [answer: (order doesn't matter) 1. Always do things the same way 2. Always use monitors (condition variables + locks) 3. Always hold lock when operating on a condition variable 4. Always grab lock at beginning of procedure and release it right before return 5. Always use "while" instead "if". 6. (Almost) never sleep() ] > > > 2. Database Reader-Writer problem > > Read handout week6b panel 2 and 3 (starting from line 106) and answer questions below. > (https://naizhengtan.github.io/22spring/notes/handout_w06b.pdf) > > > 2.a Answer the question at line 178. > Explain the starvation case in 1-2 sentences. (2 points) > [answer: A constant stream of writers will prevent readers acquiring the mutex, a starvation. [note: see line 131; for a reader, it will wait if there are either active writers (AW) or waiting writers (WW). Thus, writers will not starve because of readers.] ] > > > 2.b Read the handout panel 3 and answer the question B at line 223 below. (2 points) > [note: write pseudocode like the handout; use comments to explain if necessary.] > [answer: sharedlock lock; Database::read() { AcuiqreShared(&lock); // read database ReleaseShared(&lock); } Database::write() { AcuiqreExclusive(&lock); // write database ReleaseExclusive(&lock); } ]