Question: Part 2: Pthread Mutex Observation (5 points) A file named pthread-data-sharing-mutex-os-call.cpp has been provided to you in the same project. Compile the program and execute

Part 2: Pthread Mutex Observation (5 points)

A file named pthread-data-sharing-mutex-os-call.cpp has been provided to you in the same project.

Compile the program and execute it several times, at least 10. Make sure to pay close attention to the output that the program produces.

#include

#include

#include

#define TOTAL_THREADS 4

int count;

pthread_mutex_t the_mutex; // phread mutex variable

void* myFunction(void* arg)

{

int actual_arg = *((int*) arg);

for(unsigned int i = 0; i

// TODO:

// Use a Pthread mutex to control

// access to the critical region.

// Beginning of the critical region

count++;

std::cout

// Random wait - This code is just to ensure that the threads

// show data sharing problems

int max = rand() % 100000;

for (int x = 0; x

// End of random wait code

// End of the critical region

}

pthread_exit(NULL);

}

int main()

{

int rc[TOTAL_THREADS];

pthread_t ids[TOTAL_THREADS];

int args[TOTAL_THREADS];

// TODO: Initialize the pthread mutex here

count = 0;

for(unsigned int i = 0; i

args[i] = i;

rc[i] = pthread_create(&ids[i], NULL, myFunction, (void*) &args[i]);

}

for(unsigned int i = 0; i

pthread_join(ids[i], NULL);

}

std::cout

pthread_exit(NULL);

}

Create a Word or Google Docs document.

In this document, answer the following questions about the programs behavior:

What does it do?

What output does it produce?

Examine the program code carefully. Is the program functioning correctly?

If you do not think that the program is working correctly, describe why?

Include one screenshot of the programs output. Your choice.

Part 3: Pthread Mutex Implementation (10 points)

Modify the pthread-data-sharing-mutex-os-call.cpp program to apply a Pthread mutex solution, i.e., you will use Linux system calls to control access to the critical region. Refer to the prep materials for background info (section 2.3.6 in the textbook) and also to the Linux manual for the names and parameters of the functions. See https://linux.die.net/man/3/pthread_mutex_init The necessary changes will be very small, i.e., not a lot of code is needed.

Build and execute the updated program several times. Expected Output: Your program should produce output similar to the following (Note: the order of threads may be different in your case):

Thread #0 count 1 Thread #0 count 2 Thread #0 count 3 Thread #0 count 4 Thread #0 count 5 Thread #0 count 6 Thread #0 count 7 Thread #0 count 8 Thread #0 count 9 Thread #0 count 10 Thread #3 count 11 Thread #3 count 12 Thread #3 count 13 Thread #3 count 14 Thread #3 count 15 Thread #3 count 16 Thread #3 count 17 Thread #3 count 18 Thread #3 count 19 Thread #3 count 20 Thread #2 count 21 Thread #1 count 22 Thread #1 count 23 Thread #1 count 24 Thread #1 count 25 Thread #1 count 26 Thread #1 count 27 Thread #1 count 28 Thread #1 count 29 Thread #1 count 30 Thread #1 count 31 Thread #2 count 32 Thread #2 count 33 Thread #2 count 34 Thread #2 count 35 Thread #2 count 36 Thread #2 count 37 Thread #2 count 38 Thread #2 count 39 Thread #2 count 40 Final count 40

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!