Part 3: Thread utilities (20 pts) Thread helper functions and structures. Create
ID: 3721026 • Letter: P
Question
Part 3: Thread utilities
(20 pts) Thread helper functions and structures. Create the following structure using C programming language:
JobArguments This structure will contain all the necessary instances to process the les by a single thread
to insert words to the dictionary. This structure will have the following members:
1. hash table: A pointer to the hash table holding the words to create the dictionary.
2. lepaths: A pointer to an array of strings (hint: use double pointers).
3. start index: Index of the rst le to process from lepaths array.
4. end index: Index of the last le to process from lepaths array.
The start index and end index are indices of the lepaths array.
Implement the following helper functions:
BuildDictionaryFromFiles : This function will be executed by every thread. This function will iterate
the les from the lepaths member of the JobArguments structure in the range indicated by start index and
end index. For every le to process, the thread will open the le and identify the words in it; words in the
les are separated by a single space and a single line in the text le will not have more than 512 characters.
For this function I recommend using the fscanf or getline functions to read text lines from the le or strtok
function to get the words in a text line. The thread will insert words into the hash table.
1. Name: BuildDictionaryFromFiles
2. Return: A generic pointer.
3. Input: A generic pointer to a JobArguments instance.
For the sake of clarity, the pseudo-code for this function is the following:
for i=start_index ... end_index do
file = OpenFile(filepaths[i])
For every word in file, insert it into the hash table.
Close file
end
CreateOutputFile : This function will create a le containing the content of the dictionary.
1. Name: CreateOutputFile
2. Return: Nothing
3. Input Argument 1: A pointer to the hash table.
4. Input Argument 2: A string with the output lepath.
ProcessFiles : This function will create and launch the threads.
1. Name: ProcessFiles
2. Return: A pointer to the hash table storing the unique words.
3. Input Argument 1: An integer indicating the number of threads to use.
4. Input Argument 2: An array of strings.
5. Input Argument 3: An integer indicating the total number of les to process.
6. Note: The number of threads has to be less than or equal to the total number of les to process.
The pseudo-code for this function is the following:
ProcessFiles(number of threads, array of strings, num. of files to process):
number of files per thread = number of files to process / num. of threads
thread_array;
for i = 1 ... number of threads do
Create a JobArguments instance
thread_array[i] = Launch a thread using the BuildDictionaryFromFiles
function and the created JobArguments instance.
end
Wait for the threads to finish
To test your implementation of the thread utilities, use the test thread utils.c.
Explanation / Answer
A)
#include <stdio.h>
typedef struct Element
{
int key;
int value;
} Element;
typedef struct HashTable
{
Element *table[13];
} HashTable;
HashTable* createHashTable()
{
HashTable *Raking = malloc(sizeof(HashTable));
int i;
for (i = 0; i < 13; i++) {
Raking->table[i] = NULL;
}
return Raking;
}
void insertElement(HashTable *Raking, int key, int value) {
int h = hashFunction(key);
while(Raking->table[h] != NULL) {
if(Raking->table[h]->key == key) {
Raking->table[h]->value = value;
break;
}
h = (h + 1) % 13;
}
if(Raking->table[h] == NULL) {
Element *newElement = (Element*) malloc(sizeof(Element));
newElement->key = key;
newElement->value = value;
Raking->table[h] = newElement;
}
}
int main()
{
HashTable * Ranking = createHashTable();
}