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.

int SharedVariable = 0;

void SimpleThread(int which) {

int num, val;

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, 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.

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!