diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..7e785c1f48765708b6d244eac5093e255e4ebb03 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +coolSort diff --git a/Examples/BakingAndEating.c b/Examples/ClassExamples/BakingAndEating.c similarity index 100% rename from Examples/BakingAndEating.c rename to Examples/ClassExamples/BakingAndEating.c diff --git a/Examples/PthreadExitWithReturnData.c b/Examples/ClassExamples/PthreadExitWithReturnData.c similarity index 100% rename from Examples/PthreadExitWithReturnData.c rename to Examples/ClassExamples/PthreadExitWithReturnData.c diff --git a/Examples/StringThreadEx1.c b/Examples/ClassExamples/StringThreadEx1.c similarity index 100% rename from Examples/StringThreadEx1.c rename to Examples/ClassExamples/StringThreadEx1.c diff --git a/Examples/ThreadEx1.c b/Examples/ClassExamples/ThreadEx1.c similarity index 100% rename from Examples/ThreadEx1.c rename to Examples/ClassExamples/ThreadEx1.c diff --git a/Examples/ThreadEx2.c b/Examples/ClassExamples/ThreadEx2.c similarity index 100% rename from Examples/ThreadEx2.c rename to Examples/ClassExamples/ThreadEx2.c diff --git a/Examples/ThreadEx3.c b/Examples/ClassExamples/ThreadEx3.c similarity index 100% rename from Examples/ThreadEx3.c rename to Examples/ClassExamples/ThreadEx3.c diff --git a/Examples/MyExamples/PassSingleLine.c b/Examples/MyExamples/PassSingleLine.c new file mode 100644 index 0000000000000000000000000000000000000000..039086ce2527913c36c67fca452c3e6e9adf2abb --- /dev/null +++ b/Examples/MyExamples/PassSingleLine.c @@ -0,0 +1,89 @@ +/** + * Brandon Rodriguez + * CS 3240 + * 11-15-17 + * a4 (Assignment 5) + */ + + +/** + * Description: + * Testing of thread implementation to solve a problem. + * This is a very basic example of joining with data passing. + * + * This creates a single thread which opens up the first file in the "small-data" folder. + * It grabs the first line in this file, reads it from thread, then returns line to main + * and reads again. + */ + + +/** + * Known Issues: + * + */ + + +// Import headers. +#include <ctype.h> +#include <fcntl.h> +#include <pthread.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "apue.h" + + +// Define Vars. +#define BUFFER_SIZE 4096 + + +// Variables. +typedef struct{ + char* user_name; + char* password; + char* blood_type; + char* domain_name; + int db_index; +} data_struct; + + +// Method Declaration. +void* ThreadReadFile(); // Reads given file and reorganizes data. + + +/** + * Program's main. + * Initializes and runs program. + */ +int main(int argc, char* argv[]) { + pthread_t thread; + + char* file_location = "Data/small-data/0"; + char* return_string; + + pthread_create(&thread, NULL, ThreadReadFile, (void*) file_location); + pthread_join(thread, (void**) &return_string); + printf("Thread returned:\n%s", return_string); + free(return_string); +} + + +/** + * Uses thread to read file value. + */ +void* ThreadReadFile(void* file_location) { + FILE* read_file; + char* line_buffer = calloc(BUFFER_SIZE, sizeof(char*)); + int result_int; + + read_file = fopen(file_location, "r"); + fgets(line_buffer, BUFFER_SIZE, read_file); + result_int = fclose(read_file); + if (result_int != 0) { + err_sys("Failed to close file properly."); + } + + printf("I'ma child and I found dis: \n%s\n", line_buffer); + + pthread_exit(line_buffer); +} diff --git a/Main.c b/Main.c index 19d8a53ed7f34eb2b4dd65acd917d068e9297e41..ca6b80f00b612210931bb519f2d5128d14ebff3d 100644 --- a/Main.c +++ b/Main.c @@ -28,21 +28,31 @@ // Import headers. +#include <ctype.h> +#include <fcntl.h> +#include <pthread.h> #include <stdio.h> #include <string.h> #include <stdlib.h> -#include <ctype.h> #include "apue.h" // Define Vars. -//#define ??? +#define BUFFER_SIZE 4096 // Variables. +typedef struct{ + char* user_name; + char* password; + char* blood_type; + char* domain_name; + int db_index; +} data_struct; // Method Declaration. +void* ThreadReadFile(); // Reads given file and reorganizes data. /** @@ -50,5 +60,34 @@ * Initializes and runs program. */ int main(int argc, char* argv[]) { + pthread_t thread; + + char* file_location = "Data/small-data/0"; + char* return_string; + + pthread_create(&thread, NULL, ThreadReadFile, (void*) file_location); + pthread_join(thread, (void**) &return_string); + printf("Thread returned:\n%s", return_string); + free(return_string); +} + + +/** + * Uses thread to read file value. + */ +void* ThreadReadFile(void* file_location) { + FILE* read_file; + char* line_buffer = calloc(BUFFER_SIZE, sizeof(char*)); + int result_int; + + read_file = fopen(file_location, "r"); + fgets(line_buffer, BUFFER_SIZE, read_file); + result_int = fclose(read_file); + if (result_int != 0) { + err_sys("Failed to close file properly."); + } + + printf("I'ma child and I found dis: \n%s\n", line_buffer); + pthread_exit(line_buffer); } diff --git a/makefile b/makefile new file mode 100644 index 0000000000000000000000000000000000000000..365f25e916f438bcd98809f3631ceaf684e5ce9b --- /dev/null +++ b/makefile @@ -0,0 +1,2 @@ +all: + gcc -Wall -Wpedantic -std=c99 *.c -g -o coolSort -pthread