You are on page 1of 2

Javier Gonzalez

CST 334 - Operating Systems


06 April 2021

Lab 5 Write-up

Step 1: When I run threadHello_step1, I get this output:

10 threads are created and iterations up to 10 are passed to i. Unfortunately, I do not


get the same result if I run the program multiple times. I am getting duplicate iterations
(i) for every run. Below is an example of another output that I received:
Due to a race condition (data race), if you run some demanding process, it is possible
that you will get the wrong result and a different outcome across multiple different runs.
Instead of nice deterministic output, you’ll be met with an indeterministic output. This
leads to unexpected results.

The function go() has a parameter arg passed to it and these variables are per thread.
The compiler stores these variables in its own stack in the same address space.

The main() has a local variable i which is in a shared state and is stored in the main()
stack.

There is an obvious bug in step 1. Because go() is outside of the main(), the i values
need to be allocated to the heap. When go() tries to access i values in the stack, it is
unable to. The i values have to be dynamically allocated in the heap using malloc().
Once the values of i’s have been dynamically allocated to the heap, the program will run
correctly as shown in Step 2:

You might also like