Question: 1: Simple Multi-thread Programming without Synchronization - Weight: 40% First, you need to write a program using the Pthread library that forks a number of
1: Simple Multi-thread Programming without Synchronization - Weight: 40%
First, you need to write a program using the Pthread library that forks a number of threads each executes the loop in the SimpleThread function below. The number of threads is a command-line parameter of your program. All the threads modify a shared variable SharedVariable and display its value within and after the loop.
Your program must validate the command line parameter to make sure that it is a number, not arbitrary garbage. Your program must be able to run properly with any reasonable number of threads (e.g., 200). Try your program with the command line parameter set to 1, 2, 3, 4, and 5. Analyze and explain the results. Put your explanation in your project report.
2: Simple Threads Programming with Proper Synchronization- Weight: 40%
Modify your program by introducing pthread mutex variables, so that accesses to the shared variable are properly synchronized. Try your synchronized version with the command line parameter set to 1, 2, 3, 4, and 5. Accesses to the shared variables are properly synchronized if (i) each iteration of the loop in SimpleThread() increments the variable by exactly one and (ii) each thread sees the same final value. It is necessary to use a pthread barrier [2] in order to allow all threads to wait for the last to exit the loop.
You must surround all of your synchronization-related changes with preprocessor commands so that we can easily compile and get the version of your program developed in Step 1. E.g.,
One acceptable output of your program is (assuming 4 threads):
*** thread 0 sees value 0
*** thread 0 sees value 1
*** thread 0 sees value 2
*** thread 0 sees value 3
*** thread 0 sees value 4
*** thread 1 sees value 5
*** thread 1 sees value 6
*** thread 1 sees value 7
*** thread 1 sees value 8
*** thread 1 sees value 9
*** thread 2 sees value 10
*** thread 2 sees value 11
*** thread 2 sees value 12
*** thread 3 sees value 13
*** thread 3 sees value 14
*** thread 3 sees value 15
*** thread 3 sees value 16
*** thread 3 sees value 17
*** thread 2 sees value 18
*** thread 2 sees value 19
......
Thread 0 sees final value 80
Thread 2 sees final value 80
Thread 1 sees final value 80
Thread 3 sees final value 80
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
