diff --git a/deadlock.c b/deadlock.c index b369e63f1a0142757cccfe9452b985c116de82af..e4f706444cef3f314b70114382aac84b78481bc1 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 bef7057babae14051fc0a73aa52ad1250e1b8f3a..b8485da2078476aeb89ba72d2404ab5d01607a95 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);