Question: Write a program that uses 8 threads to compute the sum of an array of 5 0 0 0 integers. The program should divide the
Write a program that uses threads to compute the sum of an array of integers. The program should divide the set to parts and give the st thread the first integers the nd thread the next integers and so on The sums from these parts should then be added to give a total sum. The program should display this total sum. array elements to threads st thread to summednd thread to sumrd thth The program should read the dataset of a integers from a file. This file name should be given in the command line argc argv In order to aid you in this question some parts of the program have already been provided. The data set of a integers is already provided in a file called inputtxt In this file every integer is written on a single separate line. Skeleton code to start has already been provided in the file projectqsc In this file a function to get the filename from the command line and read the file has already been written. The function readfile gets the filename, opens the file in read mode and copies its contents into an array of integers of size This array has been defined and set as a global variable. A Debugging and testing has also been provided called testSum This function sequentially computes the sum of the integers in the array and displays the total sum. This function is present to test against the answer provided by using the threads. Note : This function is only for testing please do not use this function in the final output of the program. Your job is now to add to this existing code the implementation using pthreads. Output of the program : The program should display the sums computed by the threads and the total sum when these sums are added together. eg of the run of the program and how the output should look like: Before Parallelization,running the original sequential program, output is: Gcc o projectqs projectqsc cse@csedesktop:~$ projectqs input.txt Reading file: input.txt Reading file Complete, integers stored in array. Testing without threads, Sum is : After Parallelization with Pthreads output should be : Thread Sum: Thread Sum: Thread Sum: Thread Sum: Thread Sum: Thread Sum: Thread Sum: Thread Sum: Total Sum is:
This is the starting skeleton code. The function to read a file has already been given. Another Debug function called testSum has been given for testing, to check the value of the sum against the values the threads calculate. This function is purely for testing and debugging only Split the "array" of elements into portions and give each portion to one thread. So Thread will get elements Thread the next and so on In the end when all four threads are done add up their sums to get the final total sum of all elements. Print the sum. #include #include The array that holds the data int array; void testSum int sum; int j; forj ; j ; j sumarrayj; printfTesting without threads, Sum is : d
sum; This function reads a file with integers, an integer is stored in each line.The function stores the integers in the array void readfilechar filename char ch; FILE fp; fp fopenfilename,"r; read mode if fp NULL perrorError while opening the file.
; exitEXITFAILURE; char line ; line size int i; printfReading file: ; fputsfilename,stdout; printf
; while fgets line, sizeof line, fp NULL read a line if i arrayiatoiline; debug code fputs line, stdout ; write the line i; debug code printfi is : d
i; fclosefp; printfReading file Complete, integers stored in array.
; int mainint argc, char argv if argc fprintfstderr"usage: aout
; exit; return ; readfileargv; Debug code for testing only testSum; return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
