Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

mutex_switch.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

mutex_switch.c

image text in transcribed

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include  #include  #include  /* compile with gcc mutex_switch.c -o mutex_switch */ #define COUNT_TO 100000 #define MAX_CORES 12 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; long long i = 0; void *start_counting(void *arg) { for (;;) { // acquire lock pthread_mutex_lock(&mutex); // check if all COUNT_TO has been arrived at if (i >= COUNT_TO) { pthread_mutex_unlock(&mutex); return NULL; } ++i; // release lock pthread_mutex_unlock(&mutex); printf("i = %lld ", i); } } int main(void) { int i = 0; // create a thread group the size of MAX_CORES pthread_t *thread_group = malloc(sizeof(pthread_t) * MAX_CORES); // start all threads to begin work for (i = 0; i  

no_mutex.c

image text in transcribed

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include  #include  #include  /* compile with gcc no_mutex.c -o no_mutex */ #define COUNT_TO 100000 #define MAX_CORES 12 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; long long i = 0; void *start_counting(void *arg) { for (;;) { // acquire lock //pthread_mutex_lock(&mutex); // check if all COUNT_TO has been arrived at if (i >= COUNT_TO) { //pthread_mutex_unlock(&mutex); return NULL; } ++i; // release lock //pthread_mutex_unlock(&mutex); printf("i = %lld ", i); } } int main(void) { int i = 0; // create a thread group the size of MAX_CORES pthread_t *thread_group = malloc(sizeof(pthread_t) * MAX_CORES); // start all threads to begin work for (i = 0; i  

a. What is the difference between no_mutex.c and mutex_switch.c?

b. Is there a possibility that no_mutex and mutex_switch might behave differently? If so, why? If not, why not?

6 1 #include 2 #include 3 #include 4 5/* compile with gcc mutex_switch.c -o mutex_switch */ 7 #define COUNT_TO 188808 8 #define MAX_CORES 12 18 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 11 long long i = 0; 12 13 void *start_counting(void *arg) { 14 for (i) { 15 // acquire Lock 16 pthread_mutex_lock (&mutex), 17 // check if aLL COUNT_TO has been arrived at 19 if (i >= COUNT_TO) { 20 pthread_mutex_unlock(&mutex); return NULL; 22 } 18 21 24 ++i; // release Lock 27 pthread_mutex_unlock(Smutex); 28 29 printf("i = x11d ", 1); 30 } 31 } 32 33 int main(void) { 34 int i = B; 35 36 // create a thread group the size of MAX_CORES 37 pthread_t *thread_group = malloc(sizeof(pthread_t) *MAX_CORES); 38 39 // start all threads to begin work 40 for (i = @; i 2 #include 3 #include 4 5/* compile with gec no_mutex.c -o no_mutex */ 7 #define COUNT_TO 100008 8 #define MAX_CORES 12 10 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 11 long long i = 0; 12 13 void *start_counting (void *arg) { 14 for (;;) { 15 // acquire Lock 16 1/pthread_mutex_Lock (&mutex); 17 18 // check if aLL COUNT_TO has been arrived at 19 if (i >= COUNT_TO) { 29 1/pthread_mutex_unlock (&mutex); 21 return NULL; 22 } 23 24 +i; 25 26 // release Lock 27 //pthread_mutex_unlock(mutex); 28 29 printf("i = xild ", 1); 30 } 31 } 32 33 int main(void) { 34 int i = B; 35 36 // create a thread group the size of MAX_CORES 37 pthread_t *thread_group = malloc(sizeof(pthread_t) *MAX_CORES); 38 39 // start all threads to begin work 40 for (i = w; i

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

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started