A mutex lock is meant to be taken and released, always in that order, by each task that uses the shared resource it protects. In general, semaphores should not be used to enforce mutual exclusion. The correct use of a semaphore is to signaling from one task to another, i.e., a task either signal or wait, not both.
In this assignment you will solve the rendezvous problem using semaphores. This problem clearly demonstrates the type of synchronization that can be provided by semaphores.
In the terminal, navigate to the
module-4/mandatory directory. Use make to compile the program.
The executable will be named
rendezvous and placed in the
bin sub directory. Run the program from the terminal.
Run the programs several times and study the source code. Think about the following questions.
We now want to make the two threads have a rendezvous after each iteration, i.e., the two threads A and B should perform their iterations in lockstep. Lockstep means that they both first perform iteration 0, then iteration 1, then iteration 2, etc. For each iteration the order between A and B should not be restricted.
The following is an example of a valid trace of execution: A0, B0, B1, A1, B2 A2. The following are examples of invalid traces of execution: A0, A1, B0, B1 and A0, B0, B1, B2.
Implement the desired behaviour using two semaphores.
Compile and run the program.
$ make $ ./bin/rendezvous
Change the threads sleeping durations.