0% found this document useful (0 votes)
8 views6 pages

Attachment 1 27

The assignment requires the design and implementation of a C program that sorts integers using multithreading, dividing the list into two sub-lists for separate sorting threads and merging the results. Students must adhere to strict academic integrity guidelines, ensuring their work is original and properly cited. Additional requirements include specific input/output file formats, the use of a sorting algorithm, and the creation of a readme file with compilation instructions.

Uploaded by

Hanzy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Attachment 1 27

The assignment requires the design and implementation of a C program that sorts integers using multithreading, dividing the list into two sub-lists for separate sorting threads and merging the results. Students must adhere to strict academic integrity guidelines, ensuring their work is original and properly cited. Additional requirements include specific input/output file formats, the use of a sorting algorithm, and the creation of a readme file with compilation instructions.

Uploaded by

Hanzy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Multithreaded Sorting

1. Assignment Overview

In this assignment, you need to design and implement a C program that


utilizes multithreading to accomplish the task of integer sorting.

2. Important Note

There is a zero-tolerance policy on academic offenses such as


plagiarism or inappropriate collaboration. By submitting your solutionfor this
assignment, you acknowledge that the code submitted is your own work.
You alsoagree that your code may be submitted to a plagiarism detection
software (such as MOSS) that may have servers located outside Canada.
Please note that:
1) The assignments are individual assignments. You can discuss the problems
with your friends/classmates, but you need to write your program
by yourself. There should not be much similarity between your
program and others’ programs.
2) When you refer to some online resources to complete your program,
you need to understand the mechanism, then write your own code.
Your code should not be similar to the online resources. In addition,
you should cite the sources via comments in your program.
3) You cannot use Al-driven tools to produce work for evaluations. Your program should
not be highly similar to others’ programs.

3. Detailed Requirements

1) Overview: In this assignment, you need to design and implement a C program that
utilizes multithreading to accomplish the task of integer sorting. Specifically, a
list of integers should be divided into two sub-lists of integersby your program.
The size of the sub-lists should be the same (note that when the total
number of integersis odd, the integer in the middle of the list should only
be included one of the sub-lists). Then two separate threads (which are
called “sorting threads”) are created. Each of them sorts a sub-list using
a sorting algorithm of your choice. Finally, the sorted sub-lists are
merged into a single sorted list by a third thread (which is called
“merging thread”).

Because global data are shared across all threads, perhaps the easiest way
to store the initial list of integersis to create a global array. Each
sorting thread could work on one half of this array. A second global array

1
(whose size is the same as that of the unsorted integer array) could alsobe
established. The merging thread could then merge the sorted sub-lists into this
second array. Graphically, the operation flow of your C program could
be illustrated using the following figure:

2) Parameter Passing:As mentioned previously, the parent thread in your C


program needs to create two worker threads (i.e. the sorting threads) to
complete the sorting operation.
When the worker threads are created, the parent thread needs to pass two
unsorted sub-lists of integersto the worker threads. If you choose to use a
global array to store the initial list of integers, then the parent only
needs to pass the index number of the starting/ending element of the sub-lists
(instead of the sub-lists themselves) to the worker threads. To pass the
index numbers, the easiest approach is to use “struct”. For example, a
struct to pass the starting and ending index number can be found below:
/* structure for passing data to threads */
typedef struct
{
int starting_index;
int ending_index;
} parameters;

Then the parent thread could create worker threads using a strategy
similar to the following approach (assuming that N1 and N2 are the starting and
ending index number; tid is the variable corresponding to thread identification;
sorter() is the function for the worker threads):

2
/* create worker threads */
parameters *data = (parameters *) malloc(sizeof(parameters));
data->starting_index= N1;
data->ending_index = N2;
pthread_create(&tid, NULL, sorter, data);
Note that, for the merging thread, the required parameters can be passed
in a similar manner.

3) Program Structure: The overall structure of your program should look


like the following skeleton:

4) Additional Requirements: Your program should also satisfy the following


requirements:
a) Sorting Algorithm: You can use any sorting algorithm to sort a sub-list of
integers. However, in order to make marking easy, you need
to add a comment at the beginning of sorter(), which clearly states
the name of the sorting algorithm used in your program. Here
is a list of potential sorting algorithms:
https://en.wikipedia.org/wiki/Sorting_algorithm
Note that:
a. You cannot reuse your previous code to implement the
sorting algorithm.
b. The GNU C library provides a sorting function, qsort(). You can use
this function to sort integers if you prefer. If you choose to use this
function, in the comment at the beginning of sorter(),

3
please state “The sorting function qsort() in glibc is used
to sort integers”. Here are two webpages on qsort():
https://www.tutorialspoint.com/c_standard_library/c_function_qsort.htm
https://man7.org/linux/man-pages/man3/qsort.3.html
b) Sorting Result: Your program should sort the initial list of integersin
ascending order (i.e. from the smallest integer to the largest integer).

c) Merging Algorithm: You should implement an efficient method to merge two


sorted sub-lists. The following webpage (specifically,the section titled
“Merging two lists”) includes the pseudo code of an example
method. You can use this method or a different method. However,
you SHOULD NOT merge the sorted sub-lists by combining the sub-lists
of integersinto a set of integersand thereafter applying a sorting
algorithm to the set of integers(because creatingmultiple sorting
threads is designed to use parallel processing to speed up sorting).
https://en.wikipedia.org/wiki/Merge_algorithm#Merging_two_lists
d) Input and Output Files:
a. Input file: The initial list of integers are provided via a file named
“IntegerList.txt”.
i. In this file, the integersare separated by commas.
ii. There is NO new line character (i.e. \n) at the end of the list.
iii. The integersin the list are positive. The range of the integers
is [1, 999].
iv. There are at least 2 integersin the list. And there are
at most 500 integers in the list. Note that each integer has a
unique value.
v. Your program should assume that this file is located in the same
directory as your program.
vi. You should create the “IntegerList.txt” just for test yourself.
b. Output file: The sorted list of integers should be placed in a
newly-created file named “SortedIntegerList.txt”. This file should
be located in the same directory as your program. The
integers in this file should be separated by commas.
c. There are different methods to scan a file and get the integers
from it. One of the methods consists of the following steps:

i. Use fgets() to read the content of the file and store the
content using a string. Here are two useful links
about fgets():
https://www.tutorialspoint.com/cprogramming/c_file_io.htm
https://man7.org/linux/man-pages/man3/fgets.3p.html

4
ii. Use strtok() to separate the string into sub-strings, each
sub-string corresponds to an integer. Here is a
tutorial:
http://www.cplusplus.com/reference/cstring/strtok/
iii. Use atoi() to convert each sub-string into an integer.
e) Total Number of Integers: When the total number of integersis even,
each of the sublists includes half of the integers. When the total
number of integersis odd, the integer in the middle of the list should
only be included in one of the sub-lists.
f) Total Number of Threads: For simplicity, there should be four
threads in your program in total. These threads include the parent
thread (i.e. the thread corresponding to main()), two sorting threads, and
one merging thread.
g) Your program must use the GNU C library (i.e. glibc) to implement
the required features. Note that GNU C library (i.e. glibc) is the
GNU Project implementation of C standard library (i.e. libc). GNU C
library implements the entire C standard library. In addition, it
includes additional non-standard extensions, such as the header files
of “sys/wait.h” and “sys/types.h”. Here are a few links about libc and
glibc:
a. C Reference: https://en.cppreference.com/w/
b. Functions in libc:
https://www.ibm.com/docs/en/i/7.3?topic=extensions-standard-c-
libraryfunctions-table-by-name
c. Header files in libc: https://en.cppreference.com/w/c/header
d. Use of C libraries:
http://www.crasseux.com/books/ctutorial/Libraries.html#Libraries
e. glibc Reference:
https://sourceware.org/glibc/manual/latest/html_mono/libc.html
f. Function and Macro Index in glibc:
https://sourceware.org/glibc/manual/latest/html_mono/libc.html#Functi
o n-Index
g. Linux man pages for glibc: https://man7.org/linux/man-
pages/dir_all_alphabetic.html

5) Readme File: You need to complete a readme file named “Readme.txt”,


which includes the instructions that the TA could use to compile.

4. Grading Criteria

The TA will use your submitted zip file to evaluate your assignment. The full
grade is 16 points. Your submission will be evaluated from the following

5
perspectives. The specific rubric used by the marker is included in a
separate file.

a) “Readme.txt” with the correct compilation/execution instructions is provided


[1 Point]
b) main():
a. Retrieve the initial list of integersfrom “IntegerList.txt” [2 Points]
b. Create the first sorting thread [1 Point]
c. Create the second sorting thread [1 Point]
d. Wait for sorting threads to finish [1 Point]
e. Create the merge thread [1 Point]
f. Wait for the merge thread to finish [1 Point]
g. Output the sorted array: The program creates a new file
named “SortedIntegerList.txt”, which includes the sorted list of
integersand is located
in the same directory as your program. The integersare separated
using commas. [1 Point]
c) sorter(): It sorts the integersthat are passed to the function. There is a
comment at the beginning of sorter(), which clearly indicates the
name of the sorting algorithm implemented in your program. [3 Points]
d) merger(): It merges two lists of sorted integers into a sorted list.
[3 Points]
e) Proper programming format/style (e.g. release the memory that is
dynamically allocated when it is not needed any more; proper
comments; proper indentation/variable names, etc.). [1 Point]

Please note that when “Readme.txt” is not provided or “Readme.txt” does


not include the compilation/execution instructions, your submission will be
compiled using the standard command gcc -o work work.c (or your
filename) -lpthread, and you will receive a zero grade if your program
cannot be successfully compiled.

You might also like