diff --git a/documents/references.md b/documents/references.md index 8b054807061a8fa541d2e4829a01ba6530da5639..71982ef819fab3a1133eb959a816d6081f0a2ecd 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 a2dfc2e180b0ebb24a6088050726ea6028d0ecdd..4b94e3380ddef0864a620171991f78a0aff921c5 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 9180187073988fb0207d20c01e09f134aa056c40..4837313fc06cf1b1329d77e24e3134635b856afb 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 a9fb3bf5ba1a7f16bea4081f81d59bc8e19be7c1..fef1a9888ef77a9c669d02eef1f7addb998722cc 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