Lab 2 - Introduction to Concurrency

This lab will give you a introduction in the basic concepts of concurrency. You will create two processes (these are called tasks in Tornado) which will both operate on shared memory. More specifically, these two tasks will concurrently call the function increment(), which will handle incrementing a shared variable. Each task will call increment 10,000 times, therefore the final value of the shared variable should be 20,000.

In order to implement this correctly, only one task should be allowed to update the shared variable at a time. This idea is known as mutual exclusion. The region of code which requires mutual exclusion is known as a critical region. If mutual exclusion is not provided to a critical region, the outcome of the program can be incorrect. Note that the output might not always be incorrect because of the exact scheduling of the two tasks. This idea makes it difficult to prove the correctness of any concurrent program.

To provide mutual exclusion to the critical region (the increment function), you will need to use binary semaphores. These were used in the tutorial of lab 1 and can be found in the help files of Tornado. You will need the functions semBCreate, semGive, and semTake. For more information on semaphores, see the help files or look in your text book.

In this lab, you will need to:

1. Create a new project in Tornado.
2. Download the source file source.cpp and insert it into the project.
3. Make any modifications necessary to the increment function in order to provide mutual exclusion.
4. Run the program and make sure the output is 20,000. Note that even if the output is 20,000 the program might still be incorrect.
5. Explain in the lab report why your solution is correct.
6. A common mistake in providing mutual exclusion is to use a flag to signify whether or not there is a process executing in the critical region. Whenever a process wants to enter the critical region, it checks to see if the flag is set. If it is, it will simply wait until the flag is not set (i.e. while(flag);). When a process enters the critical region, the flag is set. When a process leaves the critical region, the flag is reset. Explain why this does not gaurantee mutual exclusion. Also, discuss any other problems with this solution that you can think of.

Additional materials

  • Hardware procedure
  • config.h