Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in C PLEASE INCLUDE CODE In this project, you are asked to write a program which uses three threads to concurrently compute the sum of
in C PLEASE INCLUDE CODE
In this project, you are asked to write a program which uses three threads to concurrently compute the sum of square roots from 1 to n, where n is a multiple of 3 and is specified in command line. The program gets the value n from the command line argument (using argc /argv) and converts it to an integer. Next, it creates two threads using pthread _create0. Then, the main thread computes the sum of square roots from 1 to n/3, while the two child threads compute the sum of the square roots from n/3+1 to 2n/3 and 2n/3+1 to n, respectively, at the same time as main thread's calculation. All three partial sums should be added together to a shared global variable by each thread at the end of execution. The main thread calls pthread_join) to wait for the termination of the two child threads and then prints the result. Below is an example of running your thread program: spirit thr 9999 sum of square roots: 666616.459197 u need to think about the location in program execution to call thread creation / mutex locking tc carefully. Be sure that three threads do calculation concurrently, no excessive mutex locking which creates unnecessary overhead, and is inefficient. Use mutex when necessary to avoid race conditions which cause your program to produce incorrect answers randomly. Programs which fail to do s assume the input is always valid and would not cause overflows if you use standard data types (such as int and doubles). You do not need to handle errors. o will receive partial credit only even if the result is (sometimes) correct. You can In this project, you are asked to write a program which uses three threads to concurrently compute the sum of square roots from 1 to n, where n is a multiple of 3 and is specified in command line. The program gets the value n from the command line argument (using argc /argv) and converts it to an integer. Next, it creates two threads using pthread _create0. Then, the main thread computes the sum of square roots from 1 to n/3, while the two child threads compute the sum of the square roots from n/3+1 to 2n/3 and 2n/3+1 to n, respectively, at the same time as main thread's calculation. All three partial sums should be added together to a shared global variable by each thread at the end of execution. The main thread calls pthread_join) to wait for the termination of the two child threads and then prints the result. Below is an example of running your thread program: spirit thr 9999 sum of square roots: 666616.459197 u need to think about the location in program execution to call thread creation / mutex locking tc carefully. Be sure that three threads do calculation concurrently, no excessive mutex locking which creates unnecessary overhead, and is inefficient. Use mutex when necessary to avoid race conditions which cause your program to produce incorrect answers randomly. Programs which fail to do s assume the input is always valid and would not cause overflows if you use standard data types (such as int and doubles). You do not need to handle errors. o will receive partial credit only even if the result is (sometimes) correct. You canStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started