From 42e9c786b6e8c000c3b72ec20312a0174db6d998 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 15 Nov 2017 15:14:40 -0500 Subject: [PATCH] Implement full multithreaded reading of files --- Main.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 92 insertions(+), 11 deletions(-) diff --git a/Main.c b/Main.c index 132e9b8..3ef2129 100644 --- a/Main.c +++ b/Main.c @@ -47,13 +47,18 @@ // Variables. -typedef struct{ +typedef struct { char* user_name; char* password; char* blood_type; char* domain_name; int db_index; } data_struct; +typedef struct { + int array_count; + data_struct* data_array; +} thread_return_struct; + int dir_file_counter; char* absolute_path; pthread_t* thread_array; @@ -63,6 +68,9 @@ pthread_t* thread_array; int change_directory(); void open_folder(); void* thread_read_file(); // Reads given file and reorganizes data. +data_struct* sort_data_array(); +data_struct* merge_sort(); +data_struct* merge_array(); /** @@ -72,7 +80,9 @@ void* thread_read_file(); // Reads given file and reorganizes data. int main(int argc, char* argv[]) { int index; int return_int; - char* return_string; + int struct_number; + data_struct* data_array; + thread_return_struct* return_struct; // Check for valid args. if (argc < 2) { @@ -96,9 +106,21 @@ int main(int argc, char* argv[]) { // Iterate through all threads and grab returned value. for (index = 0; index < dir_file_counter; index++) { - pthread_join(thread_array[index], (void**) &return_string); - printf("Thread returned: %s", return_string); - free(return_string); + pthread_join(thread_array[index], (void**) &return_struct); + data_array = return_struct->data_array; + for (struct_number = 0; struct_number < return_struct->array_count; struct_number++) { + printf("User: %s ", data_array[struct_number].user_name); + printf("Password: %s ", data_array[struct_number].password); + printf("Blood Type: %s ", data_array[struct_number].blood_type); + printf("Domain: %s ", data_array[struct_number].domain_name); + printf("DB Index: %d \n", data_array[struct_number].db_index); + free(data_array[struct_number].user_name); + free(data_array[struct_number].password); + free(data_array[struct_number].blood_type); + free(data_array[struct_number].domain_name); + } + free(data_array); + free(return_struct); } free(absolute_path); @@ -255,19 +277,78 @@ void open_folder() { * Uses thread to read file value. */ void* thread_read_file(void* file_location) { - FILE* read_file; - char* line_buffer = calloc(BUFFER_SIZE, sizeof(char*)); int result_int; + int struct_max; + int struct_number; + char* line_buffer = calloc(BUFFER_SIZE, sizeof(char*)); + char* token; + char* save_pointer; + FILE* read_file; + data_struct* data_array; + thread_return_struct* return_struct; + struct_max = 1024; + struct_number = -1; + data_array = calloc(struct_max, sizeof(data_struct)); + + // Open file. read_file = fopen(file_location, "r"); - fgets(line_buffer, BUFFER_SIZE, read_file); + if (read_file == NULL) { + err_sys("Failed to open file at %s", file_location); + } free(file_location); + + // Read file line by line and assign values to struct. + while(fgets(line_buffer, BUFFER_SIZE, read_file) != NULL) { + line_buffer = remove_newline(line_buffer); + struct_number++; + + // Check that there is still more space in data_array. + if (struct_number >= struct_max) { + struct_max = struct_max * 2; + data_array = realloc(data_array, struct_max); + } + + // Actually parse line to array. + + // First get username. + token = strtok_r(line_buffer, ",", &save_pointer); + data_array[struct_number].user_name = copy_string(token); + // printf("User: %s ", data_array[struct_number].user_name); + + // Get password. + token = strtok_r(NULL, ",", &save_pointer); + data_array[struct_number].password = copy_string(token); + // printf("Password: %s ", data_array[struct_number].password); + + // Get blood type. + token = strtok_r(NULL, ",", &save_pointer); + data_array[struct_number].blood_type = copy_string(token); + // printf("Blood Type: %s ", data_array[struct_number].blood_type); + + // Get domain name. + token = strtok_r(NULL, ",", &save_pointer); + data_array[struct_number].domain_name = copy_string(token); + // printf("Domain: %s ", data_array[struct_number].domain_name); + + // Get db index. + token = strtok_r(NULL, ",", &save_pointer); + data_array[struct_number].db_index = atoi(token); + // printf("DB Index: %d \n", data_array[struct_number].db_index); + } + + // data_array = merge_sort(data_array, 0, struct_number, struct_number); + + return_struct = calloc(1, sizeof(thread_return_struct)); + return_struct->array_count = struct_number; + return_struct->data_array = data_array; + + // Close file and exit out. result_int = fclose(read_file); if (result_int != 0) { err_msg("Failed to close file properly."); } - printf("I'ma child and I found dis: %s", line_buffer); - - pthread_exit(line_buffer); + free(line_buffer); + pthread_exit((void*) return_struct); } -- GitLab