From 77940d3c412dc129b6331ea2f715e824a1cefba4 Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Fri, 17 Apr 2020 02:09:06 -0400
Subject: [PATCH] Implement deadlock and starvation versions of program

---
 deadlock.c   | 13 +++++++------
 starvation.c |  6 ++++--
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/deadlock.c b/deadlock.c
index b369e63..e4f7064 100644
--- a/deadlock.c
+++ b/deadlock.c
@@ -145,10 +145,12 @@ void* start_thread(void* thread_num) {
         if (fork_held_by[left_fork] == *(int*) thread_num && fork_held_by[right_fork] == *(int*) thread_num) {
             bites_taken += 1;
             sleep(0.0001);
-        }
 
-        // Attempt to release forks.
-        release_forks(*(int*) thread_num, left_fork, right_fork);
+            // Bite taken. Attempt to release forks.
+            release_forks(*(int*) thread_num, left_fork, right_fork);
+        } else {
+            sleep(0.5);
+        }
 
         if (bites_taken > last_output[*(int*) thread_num] && (bites_taken % 100) == 0) {
             printf("Philosopher #%d has taken %d bites. Resting.\n", *(int*) thread_num, bites_taken);
@@ -186,9 +188,8 @@ void get_forks(int thread_num, int left_fork, int right_fork) {
 
     // Check if philosopher got both forks.
     if (fork_held_by[left_fork] != thread_num || fork_held_by[right_fork] != thread_num) {
-        // Failed to get one or more forks. Release if any were picked up.
-        // printf("Philosopher #%d could not get both forks. Releasing.\n", thread_num);
-        release_forks(thread_num, left_fork, right_fork);
+        // Failed to get one or more forks.
+        printf("Philosopher #%d could not get both forks.\n", thread_num);
     }
 }
 
diff --git a/starvation.c b/starvation.c
index bef7057..b8485da 100644
--- a/starvation.c
+++ b/starvation.c
@@ -147,8 +147,7 @@ void* start_thread(void* thread_num) {
             sleep(0.0001);
         }
 
-        // Attempt to release forks.
-        release_forks(*(int*) thread_num, left_fork, right_fork);
+
 
         if (bites_taken > last_output[*(int*) thread_num] && (bites_taken % 100) == 0) {
             printf("Philosopher #%d has taken %d bites. Resting.\n", *(int*) thread_num, bites_taken);
@@ -158,6 +157,9 @@ void* start_thread(void* thread_num) {
 
     }
 
+    // Attempt to release forks.
+    release_forks(*(int*) thread_num, left_fork, right_fork);
+
     printf("Philosopher #%d is full.\n", *(int*) thread_num);
     thread_ready[*(int*) thread_num] = false;
     pthread_exit(thread_num);
-- 
GitLab