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;

.

}

Code for task 1.1:

***********************************************************************************************************************

#include #include #include #include

#define NUMTHREADS 4 // not sure if this is command line argument.

int SharedVariable = 0; pthread_mutex_t lock; pthread_barrier_t barr;

void threadFunc(void arg) {

int threadId = *((int*)arg); int i;

// 20 is the number given in example for(i = 0; i< 20; i++) { // macro for compilation #ifdef PTHREAD_SYNC pthread_mutex_lock(&lock); #endif // incrementing shared variable printf("thread %d sees value %d ", threadId, SharedVariable); SharedVariable++;

#ifdef PTHREAD_SYNC pthread_mutex_unlock(&lock); #endif

}

// barrier so that all thread will read the shared variable value and display the final value. pthread_barrier_wait(&barr); printf("Thread %d sees final value %d ", threadId,SharedVariable); return NULL;

}

int main() {

int i,err; // NUMTHREADS is 4 as in the example. // The question mentions nothing about the command line argument. // if it is number of threads or number of iterations. pthread_t threads[NUMTHREADS]; int threadIds[NUMTHREADS]={0};

// initialixing mutex. pthread_mutex_init(&lock, NULL); // initialixing barrier. pthread_barrier_init(&barr, NULL, NUMTHREADS);

for(i = 0; i < NUMTHREADS; i++) {

// giving thread ids. threadIds[i]=i;

// creating threads err = pthread_create(&threads[i], NULL, threadFunc, (void*)&threadIds[i]); if(err){ printf("ERROR creating THREADS, return code %d ",err); exit(-1); } }

// waiting for all the threads to finish. for(i = 0; i < NUMTHREADS; i++) { pthread_join(threads[i], NULL); }

return 0; }

***************************************************************************************************************************************

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

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions

Question

What is Centrifugation?

Answered: 1 week ago

Question

To find integral of ?a 2 - x 2

Answered: 1 week ago

Question

To find integral of e 3x sin4x

Answered: 1 week ago

Question

To find the integral of 3x/(x - 1)(x - 2)(x - 3)

Answered: 1 week ago

Question

What are Fatty acids?

Answered: 1 week ago