Discussion questions: monitors

Please work out these problems before your next discussion section. The GSIs will go over these problems during the discussion section.

1. Restroom access

U-M decides to save money on the construction of the new CSE building by building only unisex restrooms (as opposed to redundant men's and women's rooms on each floor). For modesty's sake, however, they impose the rule that only people of one gender may occupy a given restroom at the same time.

Your task is to write a program which models access to a restroom, using Mesa monitor primitives.

Write the following procedures: woman_wants_to_enter(), man_wants_to_enter(), woman_leaves(), man_leaves().

Use the monitor primitives lock(), unlock(), signal(), wait(), and broadcast() in these functions to control access to the restroom. You may assume there is no limit on the number of people of the same gender who may occupy the restroom at a given time.

Next, modify your solution in order to prevent starvation. In other words, a person should not have to wait indefinitely to enter, assuming that each person already in the restroom stays there for some finite time period. Also, guarantee alternating priorities if both men and women are waiting for a newly emptied restroom.

2. Locks

Assume n threads are accessing m independent shared objects (e.g., shared variables). How many locks are required to provide maximum concurrency within the threads? Does more concurrency imply better performance?

3. File access

Several threads are accessing a file. Each thread has a a unique priority number. The file can be accessed simultaneously by several threads, subject to the following constraint: the sum of all unique priority numbers of the threads currently accessing the file must be less than n.

Show how to use monitors to coordinate access to the file.