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. Note: Mutex initialization can be done in two ways:  Using a mutex initialization function (https://linux.die.net/man/3/pthread_mutex_init) which is more powerful if you need to set up your mutex in special ways. Using the mutex initialization macro introduced in the lecture material, which initializes a mutex with default settings [sufficient for the purposes of this assignment].   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 and all iterations for a given thread need not be together):   Take a screenshot of a sample output and upload the picture as part of your assignment submission. ----------------------- Code: #include #include #include #define TOTAL_THREADS 2 int count; int turn; // Shared variable, indicates // whose turn it is to execute bool interested[TOTAL_THREADS]; // Shared variable, indicates // processes interested in executing // The thread_id will be either 0 or 1 void enter_region(int thread_id) { int other; // ID of the other thread other = 1 - thread_id; // The oposite of thread_id // TODO: Add the code to indicate the // thread's interest in executing. // TODO: Indicate the thread's turn to execute next // TODO: Busy wait until it is the thread's turn to execute } void leave_region(int thread_id) { // TODO: Add the code to set the flag // indicating that the thread has // exited the critical region. } void* myFunction(void* arg) { int thread_id = *((int*) arg); for(unsigned int i = 0; i < 10; ++i) { // TODO: // Make sure that the thread waits for its turn // before it enters the critical region. // // HINT: You need one function call // Beginning of the critical region count++; std::cout << "Thread #" << thread_id << " count = " << count << std::endl; // End of the critical region // TODO: // Make sure that the other thread gets a turn // // HINT: You need one function call   // Random wait - This code is just to ensure that the threads // show data sharing problems int max = rand() % 1000000; for (int x = 0; x < max; x++); // End of random wait code } pthread_exit(NULL); } // HINT: It is not necessary to make any changes in main() int main() { int rc[TOTAL_THREADS]; pthread_t ids[TOTAL_THREADS]; int args[TOTAL_THREADS]; 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); }

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter7: File Handling And Applications
Section: Chapter Questions
Problem 14RQ
icon
Related questions
Question
  1. 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.

Note: Mutex initialization can be done in two ways: 

  • Using a mutex initialization function (https://linux.die.net/man/3/pthread_mutex_init) which is more powerful if you need to set up your mutex in special ways.
  • Using the mutex initialization macro introduced in the lecture material, which initializes a mutex with default settings [sufficient for the purposes of this assignment].

 

The necessary changes will be very small, i.e., not a lot of code is needed.

  1. 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 and all iterations for a given thread need not be together):

 

Take a screenshot of a sample output and upload the picture as part of your assignment submission.

-----------------------

Code:

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

#define TOTAL_THREADS 2

int count;
int turn; // Shared variable, indicates
// whose turn it is to execute

bool interested[TOTAL_THREADS]; // Shared variable, indicates
// processes interested in executing

// The thread_id will be either 0 or 1
void enter_region(int thread_id)
{
int other; // ID of the other thread

other = 1 - thread_id; // The oposite of thread_id


// TODO: Add the code to indicate the
// thread's interest in executing.


// TODO: Indicate the thread's turn to execute next


// TODO: Busy wait until it is the thread's turn to execute


}


void leave_region(int thread_id)
{
// TODO: Add the code to set the flag
// indicating that the thread has
// exited the critical region.


}


void* myFunction(void* arg)
{
int thread_id = *((int*) arg);

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

// TODO:
// Make sure that the thread waits for its turn
// before it enters the critical region.
//
// HINT: You need one function call


// Beginning of the critical region

count++;
std::cout << "Thread #" << thread_id << " count = " << count << std::endl;

// End of the critical region


// TODO:
// Make sure that the other thread gets a turn
//
// HINT: You need one function call

 


// Random wait - This code is just to ensure that the threads
// show data sharing problems
int max = rand() % 1000000;

for (int x = 0; x < max; x++);

// End of random wait code
}

pthread_exit(NULL);
}


// HINT: It is not necessary to make any changes in main()
int main()
{
int rc[TOTAL_THREADS];
pthread_t ids[TOTAL_THREADS];
int args[TOTAL_THREADS];

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);
}

OUTPUT IN SCREENSHOT

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
Transcribed Image Text: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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Np Ms Office 365/Excel 2016 I Ntermed
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage