Select the option that best describes this completed code. C…
Select the option that best describes this completed code. Create shared mutex lock and cond1 & cond2 condition variable and initialize//COMPLETION BEFORE EVEN PRINTpthread_mutex_lock(&mutex);while (counter %2 != 0) pthread_cond_wait(&cond1,&mutex);//COMPLETION AFTER EVEN PRINTpthread_cond_signal(&cond2);pthread_mutex_unlock(&mutex);//COMPLETION BEFORE ODD PRINTpthread_mutex_lock(&mutex);while (counter %2 == 0) pthread_cond_wait(&cond2,&mutex);//COMPLETION AFTER ODD PRINTpthread_cond_signal(&cond1);pthread_mutex_unlock(&mutex);
Read DetailsFour threads are started to work together to take turns prin…
Four threads are started to work together to take turns printing out each integer once in sequential order from 1 up to some maximum. Two of the threads are executing printEven() and the other two threads are executing printOdd(). The following code is a partial implementation of the functions the threads are executing and the shared variables between them. int MAX = 1000; int counter = 1; //the next number to be printed //two threads execute this function void* printEven(void* arg) { for (int i = 0; i < MAX; i++) { // COMPLETION BEFORE EVEN PRINT printf("Even: %d\n", counter); counter++; // COMPLETION AFTER EVEN PRINT } return NULL; } //two threads execute this function void* printOdd(void* arg) { for (int i = 0; i < MAX; i++) { // COMPLETION BEFORE ODD PRINT printf("Odd: %d\n", counter); counter++; // COMPLETION AFTER ODD PRINT } return NULL; }
Read DetailsSuppose there are N workers concurrently running the followi…
Suppose there are N workers concurrently running the following worker function with arbitrary op_code parameters. As we can see, there are two different semaphores r0 and r1 declared in the following code. They are respectively initialized with value R0 and R1. N, R0, and R1 are all positive integers. do_task_A() and do_task_B() functions won’t get stuck. 1 sem_t r0; // Initialized with value R02 sem_t r1; // Initialized with value R134 void *worker(void *op_code) {5 switch (*(int *)op_code) {6 case 0:7 sem_wait(&r0);8 sem_wait(&r1);910 do_task_A();1112 sem_post(&r1);13 sem_post(&r0);14 case 1:15 sem_wait(&r1);16 sem_wait(&r0);1718 do_task_B();1920 sem_post(&r0);21 sem_post(&r1);22 }23 }
Read Details