Question: OverviewIn this assignment you will learn and practice developing a multithreaded application using both Java and C with Pthreads. So you will submit two programs!The
OverviewIn this assignment you will learn and practice developing a multithreaded application usingboth Java and Cwith Pthreads. So you will submit two programs!The application you are asked to implement is from our textbook SGG namelyMultithreaded Sorting Application with an addition of computing average of the given values.Here is the description of it for convenience:Write a multithreaded sorting program consisting of three threads: main, sortingavg, mergingavg. They work as follows:Main thread randomly generates an array of double values with a given size say N and divides it into two smaller arrays of equal size. It then passes each subarray to sortingavg thread creates two copies of the same thread and waits until these two subarrays are sorted and the average of the values in each array are computed. It then calls mergingavg thread by passing the already sorted two subarrays, and the averages of the first and second subarrays as parameters.Sortingavg thread sorts the given subarray using insertion sort or selection sort one is enough and you need to implement either one and computes the average of the values in the given array. So this thread returns sorted subarray and its average.Mergingavg thread takes both sorted subarrays and the their averages as parameters. It then merges them into a single sorted array in a linear time so don't call sorting function again! You can simply have a loop and take the smallest from the sorted arrays until they are all merged! and simply find the overall average by taking the average of the given two averages this will work because we assume both subarrays have the same size!Your program should take take an integer say N from the command line you can assume that we will always give an even number This number N represents the size of the array that needs to be sorted. Accordingly, your main thread should create an array of N double values and randomly select the values from the range of Then sort them using multithreading and compute the average of these values as described above. While doing these, we like to also measure how long it takes to finish this task. For the comparison purposes, you are also asked to simply call your sort function to sort the whole array and compute average without multithreading and measure how long it takes basically the main thread simply calls the sortingavg thread for the whole arrayHere is how your program should be executed with a sample output:prog or in case of Java java progj Sorting is done in ms when two threads are usedSorting is done in ms when one thread is usedThe numbers and here are just an example! Your actual numbers will be different and depend on the runs. I have some more discussion at the end of this documentYou need to figure out how to implement this program in Java and C Pthreads You need to be careful about how to pass parameters in C and Java. For example, in C because global data are shared across all threads, perhaps the easiest way is to create a global array and just pass index valuesranges as parameters. Each sorting thread will work on one half of this array. A second global array of the same size will also be created so that the merging thread will then merge the two subarrays into this second array. This programming project will require passing parameters to each of the sorting threads. In particular, it will be necessary to identify the starting index from which each thread is to begin sorting. You need to deal with similar issues in Java as well. In both case please do not use readily available sort functionsmethods you need to implement a simple one insertion or selection sort by yourself!Part In this part you will write your program in C with Pthreads.Part In this part you will write your program in Java.Part Test both programs with different values of N and see how they perform.Make a table of your results and include it in your REPORT.txtComment on your results under two threads vs no threading or using different languages C or Javahere is the high level idea!mainn will be given as command line argumentstruct timespec tsbegin, tsend;double elapsed; create array A n double values and randomly generate these values also create arrays B and C with the same size of A Afirsthalf and AsecondHalf with the half size of A ONE THREAD CASE copy A into BclockgettimeCLOCKMONOTONIC, &tsbegin; create thB sortThreadavg to sort B and compute its average implement selection or insertion sort On return sorted array and overallAvg join thBclockgettimeCLOCKMONOTONIC, &tsend; elapsed tsend.tvsec tsbegin.tvsec; elapsed tsend.tvnsec tsbegin.tvnsec; Sorting by ONE thread is done in elapsed ms Print average and at least first values of the the sorted array TWO THREADS CASE copy A into Afirsthalf and AsecondHalfclockgettimeCLOCKMONOTONIC, &tsbegin; create thA sortThreadavg to sort Afirsthalf and compute AfirstAvg use selectioninsertion sort Oncreate thA sortThread to sort Asecondhalf and compute AsecondAvg use selectioninsertion sort Onjoin thAjoin thAcreate thM mergeThreadavg by paasing Afirsthalf, Asecondhalf, AfirstAvg, AfirstAvg make sure this just merges the sorted values form two arrays while keeping them sorted. On dont' copy these arrays and then call sort! which will be like sorting the whole thing! and simply computes overalAvg AfirstAvg AfirstAvg return mergedsorted array and overallAvg join thMclockgettimeCLOCKMONOTONIC, &tsend; elapsed tsend.tvsec tsbegin.tvsec; elapsed tsend.tvnsec tsbegin.tvnsec; Sorting by TWO threads is done in elapsed ms Print overallAvg and at least first values of the the sorted arrayWhat should be the execution time ratio between two thread vs single threads?In theory, a single thread using selectioninsertion sort takes On right? So if you divide the array into two parts, then sorting the first half takes On and similarly sorting the second half takes the same merging takes OnIf two parts are sorted by different CPU cores in parallel using kernel level threads, then in theory the execution time of two threads would be around of single thread, right? if only a single CPU is used with user level threads then the ratio could be around But then there are a lot of other issues!. For example, switching between threads context switching cost Also consider paging: in case of single thread dealing with a large array may cause more page faults etc If you first execute single thread and then two threads in the same program, single thread case may be busy with getting all the pages and cause more page fault; but then two threads may not result any page fault and use the existing data and cause less page fault. If you see wildly different execution times, try two threads first, then run single thread to see if there is any difference! Moreover, measuring execution time is not an easy job. Sometimes due to how clock is updated, you may get interesting execution times. That is why actually people run the same experiment several times and then take their averages...In summary, it is possible to get wild execution times beyond the expected theoretical ratios. But if is too wild, it is better to first make sure your code is working correctly. Also verify that the sorted array is actually sorted at the end in both cases. Then report the execution times that you got and try to explain what you see and howwhy they might be different than the theoretical expectations.
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
