Semaphores

Please work out the pre-lab problems before your next lab section. The GSI/IAs will go over these problems during the lab section.

Implement your solutions using the semaphores that are provided by the Project 1 thread library.

[pre-lab] 1. Synchronization with semaphores

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

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

[pre-lab] 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.

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:

[in-lab] 2. RAII

RAII (Resource Acquisition Is Initialization) is a programming idiom that is commonly used to acquire/release mutexes in concurrent programs. The basic idea is to create a class (often called a lock guard) whose constructor acquires the mutex and whose destructor releases the mutex. The program can then define the lock guard as a local variable when it needs to acquire the lock, then let the compiler automatically call the destructor (and release the lock) whenever that local variable leaves scope. This idiom makes it easy to release the mutex, regardless of how the program leaves the scope (e.g., return statement, exception). You will find this idiom handy when you write Project 2 and 4.

Modify your Project 1 disk scheduler to use RAII. Remember that you can create a new scope anywhere in your program, even if it doesn't correspond nicely to a function, if-statement, or loop.

Think about what resources are acquired and released in Project 2, and consider using RAII to acquire/release these resources.