Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task 1.2: Simple Threads Programming with Proper Synchronization- Modify your program by introducing pthread mutex variables, so that accesses to the shared variable are properly

Task 1.2: Simple Threads Programming with Proper Synchronization-

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, 5, 10, 100 and 200.

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 I can easily compile (with the gcc preprocessor option Dmacro[=defn]) and get the version of your program developed in Task 1.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;

.

}

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions