CSCI 3500: Studio 10
Race conditions are a common problem of multi-threaded and parallel
programming. Our goal today is to create a race condition and observe
In this studio, you will:
- Create a shared global variable
- Access this variable from a single thread
- Access this variable from multiple threads
- Demonstrate a race under parallel execution (simultaneous execution)
- Demonstrate a race under sequential execution (concurrent execution)
Please complete the required exercises below, as well as any optional
enrichment exercises that you wish to complete.
As you work through these exercises, please record your answers in a text
file. When finished, submit your work via the git repository.
Make sure that the name of each person who worked on these exercises
is listed in the first answer, and make sure you number each of your responses
so it is easy to match your responses with each exercise.
- As the answer to the first exercise, list the names of the people who
worked together on this studio.
- Create a new program, and create a global variable of type
int and called
race at the top of your program
(in global scope, not in the
main() function). Then
write two functions, called
which increment and decrement the variable
race two million
times each. Each function should perform two million additions or
subtractions by one. Copy and paste these functions.
- What do you think
would happen if you executed these functions directly in
main()? Test your hypothesis by doing so. What happens?
- What do you think will happen if these two functions were executed
simultaneously by two different threads? What specific value do you think
race would have?
- Modify your program so that each function is executed
concurrently by its own
pthread_create(). Make sure that you
create both threads before you wait for either thread.What is the result?
Copy and paste several executions of your program.
- Can you explain the program's behavior? What do you think the maximum
or minimum value could ever be?
- Try changing the number of iterations from two million to one million,
and then from one million to 500,000. Does your program still exhibit this
behavior? Try your program with 1000 iterations and try your program ten or
fifteen times. What about now?
- Could your race condition still exhibit itself if your machine had only
one processor core? Why or why not?
- We can simulate this by running your program and only allowing it to
execute on one processor. Set the number of iterations back to
two million, and then execute the command:
taskset -c 0 ./race_program
This restricts the program
race_program to only execute on
processor zero. What happens?
- Modify your program so that each function performs two hundred million
operations (200,000,000). Run your program on one processor core, what
- Give a reasonable explanation of the behavior in the previous exercise.
Optional Enrichment Exercises
- No optional exercises