From 375c72f0534f5cb27100225780744d96ba543140 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Thu, 16 Apr 2020 23:59:00 -0400 Subject: [PATCH] Implement initial threading logic --- documents/references.md | 11 ++++++++++ main.c | 48 ++++++++++++++++++++++++++++++++++++++++- makefile | 2 +- readme.md | 2 +- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/documents/references.md b/documents/references.md index 8b05480..71982ef 100755 --- a/documents/references.md +++ b/documents/references.md @@ -7,6 +7,17 @@ Includes anything from stack overflow links to notes about logic from previous w ## References +### Pthreads Reference +<https://www.cs.cmu.edu/afs/cs/academic/class/15492-f07/www/pthreads.html> +<https://www.geeksforgeeks.org/thread-functions-in-c-c/> + +### C Sleep +<https://stackoverflow.com/a/10923084> + +### Copy Variable to New Memory Location +This helps ensure variable values passed to threads are thread-safe and not shared. +<https://stackoverflow.com/a/25489311> + ### Makefile * Purpose of ".PHONY" - <https://stackoverflow.com/a/2145605> * Have one command execute another - <https://stackoverflow.com/a/48552668> diff --git a/main.c b/main.c index a2dfc2e..4b94e33 100755 --- a/main.c +++ b/main.c @@ -1,13 +1,15 @@ /** - * + * Uses pthreads to implement the "Dining Philosophers" problem. */ // Import headers. +#include <pthread.h> #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> // Constant Defines. @@ -17,6 +19,7 @@ // Method Declaration. +void* start_thread(); /** @@ -26,5 +29,48 @@ int main(int argc, char* argv[]) { printf("Starting program.\n"); + // Create variables. + pthread_t* thread_array = calloc(10, sizeof(pthread_t)); + int* thread_results = calloc(10, sizeof(int)); + int index = 0; + + // Create threads. + while (index < 10) { + // Create new "thread_number" int from index and save to new memory location. Then create thread. + int* thread_number = calloc(1, sizeof(int)); + *thread_number = index + 1; + pthread_create(&thread_array[index], NULL, start_thread, (void*) thread_number); + index += 1; + } + + // Join threads and handle results. + index = 0; + while (index < 10) { + // Get and save return value. Free original pointer for int that was passed into threads. + void* ret_val = NULL; + pthread_join(thread_array[index], &ret_val); + thread_results[index] = *(int*) ret_val; + free(ret_val); + printf("Got thread #%d results.\n", thread_results[index]); + index += 1; + } + + // Free variables. + free(thread_array); + free(thread_results); + printf("\n\nTerminating program.\n"); } + + +/** + * Function threads call on start. + */ +void* start_thread(void* thread_num) { + printf("Started thread #%d.\n", *(int*)thread_num); + + sleep(1); + + printf("Exiting thread #%d.\n", *(int*)thread_num); + pthread_exit(thread_num); +} diff --git a/makefile b/makefile index 9180187..4837313 100755 --- a/makefile +++ b/makefile @@ -11,7 +11,7 @@ all: compile run clean # Compile program. compile: - gcc -Wall -Wpedantic -std=c99 *.c + gcc -Wall -Wpedantic -std=c99 *.c -lpthread # Compile and run program. diff --git a/readme.md b/readme.md index a9fb3bf..fef1a98 100755 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ ## Description -A program using pthreads to do the "Dining Philosophers" problem. +A program using pthreads to implement the "Dining Philosophers" problem. ## Running Program -- GitLab