Discussion questions: semaphores

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

1. Synchronization with semaphores

Redo the restroom problem from the previous discussion using semaphores. To ensure some fairness and prevent starvation, your solution shall use the following policy. If women (men) are in the restroom, then newly arriving women (men) can keep going in as long as men (women) are not waiting; else new arrivals must wait. When women (men) leave the restroom, all waiting men (women) are admitted. Write the following procedures:

Hint: This is similar to implementing reader/writer locks with semaphores.

2. Barrier Synchronization

Some applications are divided into phases and have a rule that no process may proceed into the next phase until all processes are ready to proceed to the next phase (Tanenbaum, Section 2.3.9 "Barriers").

A running race is to take place. There are N runners and one official starter of the race. The starter waits until all of the runners arrive at the starting line. Once all the runners have arrived, the starter releases all the runners to start the race. Write the following procedures using semaphores to simulate this situation: