Provide two programming examples in which multithreading provides better performance than a single-threaded solution.
Read moreTable of Contents
Textbook Solutions for Operating System Concepts
Question
Consider a multicore system and a multithreaded program writtenusing the many-to-many threading model. Let the number of user-levelthreads in the program be greater than the number of processing coresin the system. Discuss the performance implications of the followingscenarios.a. The number of kernel threads allocated to the program is less thanthe number of processing cores.b. The number of kernel threads allocated to the program is equal tothe number of processing cores.c. The number of kernel threads allocated to the program is greaterthan the number of processing cores but less than the number ofuser-level threads. #include #include #include int value = 0;void *runner(void *param); /* the thread */int main(int argc, char *argv[]){pid t pid;pthread t tid;pthread attr t attr;pid = fork();if (pid == 0) { /* child process */pthread attr init(&attr);pthread create(&tid,&attr,runner,NULL);pthread join(tid,NULL);printf("CHILD: value = %d",value); /* LINE C */}else if (pid > 0) { /* parent process */wait(NULL);printf("PARENT: value = %d",value); /* LINE P */}}void *runner(void *param) {value = 5;pthread exit(0);}F
Solution
The first step in solving 4 problem number 18 trying to solve the problem we have to refer to the textbook question: Consider a multicore system and a multithreaded program writtenusing the many-to-many threading model. Let the number of user-levelthreads in the program be greater than the number of processing coresin the system. Discuss the performance implications of the followingscenarios.a. The number of kernel threads allocated to the program is less thanthe number of processing cores.b. The number of kernel threads allocated to the program is equal tothe number of processing cores.c. The number of kernel threads allocated to the program is greaterthan the number of processing cores but less than the number ofuser-level threads. #include #include #include int value = 0;void *runner(void *param); /* the thread */int main(int argc, char *argv[]){pid t pid;pthread t tid;pthread attr t attr;pid = fork();if (pid == 0) { /* child process */pthread attr init(&attr);pthread create(&tid,&attr,runner,NULL);pthread join(tid,NULL);printf("CHILD: value = %d",value); /* LINE C */}else if (pid > 0) { /* parent process */wait(NULL);printf("PARENT: value = %d",value); /* LINE P */}}void *runner(void *param) {value = 5;pthread exit(0);}F
From the textbook chapter Threads you will find a few key concepts needed to solve this.
Visible to paid subscribers only
Step 3 of 7)Visible to paid subscribers only
full solution