Starting from:

$30

Sorting Contest using Threads - Prog 5

Sorting Contest using Threads - Prog 5 (10 points)
CECS 325 – System Programming with C++


In program 4 you used pthread_create( ) to create threads. That required a pointer to a struct to pass a parameter list to the function that was used as a parameter in the pthread_create( ) function. There is another way to create threads using a thread class.


This assignment uses threads to perform parallel processing – allowing you to speed up your sort program substantially. This sample program below shows 4 threads. You should use 8 threads in your program.

Your program will read in 1 million unsorted numbers from numbers.txt into an array. Then you will split the array into 8 sections – each section will have 125,000 numbers. You will create 8 threads and pass each section of the array to the thread to sort using the sort algorithm you used in the previous assignment. Once all the threads have returned, you will merge 2 adjacent sections into a sorted super section, then 2 adjacent super sections into another super section, and finally the 2 remaining super sections into one single sorted array. Then print the array to a file. Then you will call the verifySort (that you wrote) to verify the output file is sorted. verifySort( ) will print the number of integers in the file and display if they are sorted or not.
You will run this shell file (sortRace.sh – that you write) and submit a screenshot of the results:

:: start shell file
generate 1000000 100000 999999
time sort numbers.txt > sysSort.out
time mySort numbers.txt mySort.out
:: verifySort shows how many numbers in the file and if they are sorted.
verifySort mySort.out  


What to submit:
1)    Your program source code for the sort (mySort.cpp)
2)    Your program that verifies if the file is sorted (verifySort.cpp)
3)    Your shell (batch) file (sortRace.sh)
4)    Screenshot showing the output of the shell command



Below is a program that creates 4 threads, each of which print out one word from a quote from the Matrix movie: “I know kung fu”. You can examine this program to see how threads work in a C++ program.
    
// filename: matrix2.cpp
// compile line:  %c++ matrix2.cpp -o matrix2 -pthread

#include <string>
#include <thread>  // include the thread library
#include <iostream>
using namespace std;

// this is the function that is called by each thread
void print_message(string quote)
{
    cout << quote<<endl;
}

string quote[4] = {"I","know","kung","fu"};

int main()
{
     // create 2 threads. threads launch a process upon creation
     // pass the function name and all parameters. 
     // In this case, a function name and string
     thread thread0(print_message,quote[0]); 
     thread thread1(print_message,quote[1]); 
     thread thread2(print_message,quote[2]); 
     thread thread3(print_message,quote[3]);

     // force the threads to join back to the main program
     thread0.join();  
     thread1.join();
     thread2.join();
     thread3.join();
 
     return 0;
}

More products