Question: #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
#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
// Beginning of the critical region count++; std::cout
// End of the critical region // TODO: // Relinquish access to the Pthread mutex // since critical region is complete.
// Random wait - This code is just to ensure that the threads // show data sharing problems int max = rand() % 100000; for (int x = 0; x
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
-
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.
NMX999 Thread Thread count = 2 Thread count = 3 Thread #8 count Thread #0 count Thread #8 count = 6 Thread #8 count = 7 Thread #8 count Thread #8 count Thread #8 count = Thread #3 Count = 11 Thread #3 count Thread *3 count Thread #3 count Thread 23 count = Thread 13 count Thread #3 count 17 Thread #3 Count = 18 Thread X3 count = 19 Thread #3 count = 20 Thread #2 count - 21 Thread X1 Count = 22 Thread #1 count = 23 Thread #1 count = 24 Thread #1 count25 Thread #1 count = 26 Thread X1 count = 27 Thread #1 count = 28 Thread #1 count 29 Thread #1 count = 32 Thread N1 count = 31 Thread #2 court = 32 Thread #2 Court 33 Threac 2 count = 34 Thread 2 Court = 35 Thread #2 count = 36 Thread #2 count Threac 2 Count = 38 Thread 2 Court = 39 Thread count Final count 40
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
