From 823365ea38ab1f14f484ae2ad522f92bace65e1c Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Wed, 15 Nov 2017 10:04:51 -0500
Subject: [PATCH] Implement basic thread creation and return

---
 .gitignore                                    |  1 +
 .../{ => ClassExamples}/BakingAndEating.c     |  0
 .../PthreadExitWithReturnData.c               |  0
 .../{ => ClassExamples}/StringThreadEx1.c     |  0
 Examples/{ => ClassExamples}/ThreadEx1.c      |  0
 Examples/{ => ClassExamples}/ThreadEx2.c      |  0
 Examples/{ => ClassExamples}/ThreadEx3.c      |  0
 Examples/MyExamples/PassSingleLine.c          | 89 +++++++++++++++++++
 Main.c                                        | 43 ++++++++-
 makefile                                      |  2 +
 10 files changed, 133 insertions(+), 2 deletions(-)
 create mode 100644 .gitignore
 rename Examples/{ => ClassExamples}/BakingAndEating.c (100%)
 rename Examples/{ => ClassExamples}/PthreadExitWithReturnData.c (100%)
 rename Examples/{ => ClassExamples}/StringThreadEx1.c (100%)
 rename Examples/{ => ClassExamples}/ThreadEx1.c (100%)
 rename Examples/{ => ClassExamples}/ThreadEx2.c (100%)
 rename Examples/{ => ClassExamples}/ThreadEx3.c (100%)
 create mode 100644 Examples/MyExamples/PassSingleLine.c
 create mode 100644 makefile

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7e785c1
--- /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 0000000..039086c
--- /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 19d8a53..ca6b80f 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 0000000..365f25e
--- /dev/null
+++ b/makefile
@@ -0,0 +1,2 @@
+all:
+	gcc -Wall -Wpedantic -std=c99 *.c -g -o coolSort -pthread
-- 
GitLab