Question: Task 1.1: Simple Multi-threaded Programming without Synchronization First, you need to write a C program using the Pthread library to spawn a number of threads

Task 1.1: Simple Multi-threaded Programming without Synchronization

First, you need to write a C program using the Pthread library to spawn a number of threads each of which executes the loop in the SimpleThread function below. The number of threads to create is a command line parameter passed to your program. All the threads modify a shared variable SharedVariable and display its value within and after the loop.

int SharedVariable = 0;

void SimpleThread(int which) { int num, val = 0;

for(num = 0; num < 20; num++) { if (random() > RAND_MAX / 2) usleep(10);

val = SharedVariable;

printf("*** thread %d sees value %d ", which, val); SharedVariable = val + 1;

}

val = SharedVariable;

printf("Thread %d sees final value %d ", which, val);

}

Your program must validate the command line parameter to make sure that it is a number.

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, 5, 10, and 50. Analyze and explain the results.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!