Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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_2

Step: 3

blur-text-image_3

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

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions