# COMP1521 18s1 Exam Q9 A. i) The `mx' semaphore is there to ensure that only one reader at a time executes the code in between the `wait(mx)' and `signal(mx)' calls. So only the first reader correctly waits for the writer to finish (if needed) and only after the last reader has finished reading can the writer start writing. ii) The `w' semaphore is to ensure that only one process at a time is writing (readers can't read here) and if the resource is being read by at least 1 reader, the writers cannot write. B. i) sem_init(mx, 0, 1); ii) sem_post(w); C. The writer could be pre-empted after the `signal(w)' call, so `w' has value 1. If there are multiple readers, the first reader will decrement the `w' semaphore and start reading. It is pre-empted and another reader starts reading. That reader is pre-empted and another reader starts and so on. If the reader processes have a higher priority than the writer, the scheduler will likely choose to run the reader processes thus starving the writer of the shared resource.