Attachment 1 27
Attachment 1 27
1. Assignment Overview
2. Important Note
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:
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
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).
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
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.