Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 2: Pthread Mutex Observation (5 points) A file named pthread-data-sharing-m

ID: 3750951 • Letter: P

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 it several times, at least 10. Make sure to pay close attention to the output that the program produces.

#include <iostream>

#include <pthread.h>

#include <stdlib.h>

#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 < 10; ++i) {

  

// TODO:

// Use a Pthread mutex to control

// access to the critical region.

  

  

// Beginning of the critical region

  

count++;

std::cout << "Thread #" << actual_arg << " count = " << count << std::endl;

  

  

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

// show data sharing problems

int max = rand() % 100000;

  

for (int x = 0; x < max; 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 < TOTAL_THREADS; ++i) {

args[i] = i;

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

}

  

for(unsigned int i = 0; i < TOTAL_THREADS; ++i) {

pthread_join(ids[i], NULL);

}

  

std::cout << "Final count = " << count << std::endl;

pthread_exit(NULL);

}

Create a Word or Google Docs document.

In this document, answer the following questions about the program’s 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 program’s 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.

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

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition_cond = PTHREAD_COND_INITIALIZER;

void *funcCnt1();
void *funcCnt2();
int count = 0;
#define COUNT_DONE 20
#define COUNT_HALT1 6
#define COUNT_HALT2 12

int main()
{
pthread_t thread1, thread2;

pthread_create( &thread1, NULL, &funcCnt1, NULL);
pthread_create( &thread2, NULL, &funcCnt2, NULL);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);

exit(0);
}

void *funcCnt1()
{
for(;;)
{
pthread_mutex_lock( &condition_mutex );
while( count >= COUNT_HALT1 && count <= COUNT_HALT2 )
{
pthread_cond_wait( &condition_cond, &condition_mutex );
}
pthread_mutex_unlock( &condition_mutex );

pthread_mutex_lock( &count_mutex );
count++;
printf("Counter value funcCnt1: %d ",count);
pthread_mutex_unlock( &count_mutex );

if(count >= COUNT_DONE) return(NULL);
}
}

void *funcCnt2()
{
for(;;)
{
pthread_mutex_lock( &condition_mutex );
if( count < COUNT_HALT1 || count > COUNT_HALT2 )
{
pthread_cond_signal( &condition_cond );
}
pthread_mutex_unlock( &condition_mutex );

pthread_mutex_lock( &count_mutex );
count++;
printf("Thread count: %d ",count);
pthread_mutex_unlock( &count_mutex );

if(count >= COUNT_DONE) return(NULL);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote