Question: 2: Simple Threads Programming with Proper Synchronization- IN C LANGUAGE Modify your program by introducing pthread mutex variables, so that accesses to the shared variable
2: Simple Threads Programming with Proper Synchronization- IN C LANGUAGE
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.,
for(num = 0; num < 20; num++) {
#ifdef PTHREAD _SYNC
/* put your synchronization-related code here */
#endif
val = SharedVariable;
printf("*** thread %d sees value %d ", which, val);
SharedVariable = val + 1;
.......
}
Create a Makefile to run the code
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
