From e874ad94a31a8259b8eb7ad2865767c081bcd279 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sun, 3 Jan 2021 14:04:22 +0100 Subject: Fixing philo_one/philo_two dying by sleeping more when checking the death, Fixing timestamp in the wrong order in philo_three --- philo_one/src/event.c | 12 ++++++------ philo_one/src/main.c | 3 ++- philo_one/src/philo_one.h | 3 ++- philo_one/src/routine.c | 4 ++-- 4 files changed, 12 insertions(+), 10 deletions(-) (limited to 'philo_one/src') diff --git a/philo_one/src/event.c b/philo_one/src/event.c index 2a0c302..4abfa67 100644 --- a/philo_one/src/event.c +++ b/philo_one/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/02 10:49:08 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 13:56:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ void event_take_fork(t_philo *arg, pthread_mutex_t *fork) pthread_mutex_lock(&arg->conf->mutex_stdout); if (philo_finished(arg->conf)) return ; - philo_put(arg->id, EVENT_FORK); + philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); pthread_mutex_unlock(&arg->conf->mutex_stdout); } @@ -31,7 +31,7 @@ void event_eat(t_philo *arg) pthread_mutex_lock(&arg->conf->mutex_stdout); if (philo_finished(arg->conf)) return ; - philo_put(arg->id, EVENT_EAT); + philo_put(arg->id, EVENT_EAT, arg->conf->initial_time); pthread_mutex_unlock(&arg->conf->mutex_stdout); usleep(arg->conf->timeout_eat * 1000); } @@ -43,7 +43,7 @@ void event_think(t_philo *arg) pthread_mutex_lock(&arg->conf->mutex_stdout); if (philo_finished(arg->conf)) return ; - philo_put(arg->id, EVENT_THINK); + philo_put(arg->id, EVENT_THINK, arg->conf->initial_time); pthread_mutex_unlock(&arg->conf->mutex_stdout); } @@ -57,7 +57,7 @@ void event_sleep( pthread_mutex_lock(&arg->conf->mutex_stdout); if (philo_finished(arg->conf)) return ; - philo_put(arg->id, EVENT_SLEEP); + philo_put(arg->id, EVENT_SLEEP, arg->conf->initial_time); pthread_mutex_unlock(&arg->conf->mutex_stdout); pthread_mutex_unlock(fork_right); pthread_mutex_unlock(fork_left); @@ -72,6 +72,6 @@ void event_die(t_philo *arg) if (philo_finished(arg->conf)) return ; arg->conf->all_alive = false; - philo_put(arg->id, EVENT_DIE); + philo_put(arg->id, EVENT_DIE, arg->conf->initial_time); pthread_mutex_unlock(&arg->conf->mutex_stdout); } diff --git a/philo_one/src/main.c b/philo_one/src/main.c index 0c406b0..55e70d3 100644 --- a/philo_one/src/main.c +++ b/philo_one/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2021/01/02 10:49:26 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 13:57:04 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,6 +54,7 @@ int main(int argc, char **argv) return (0); if (st_setup(&conf, &philos, &forks) != 0) return (1); + conf.initial_time = h_time_now(); if (!philos_start(philos, conf.philo_num)) { forks_destroy(forks, conf.philo_num); diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index b566f80..de5f2c4 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2021/01/01 13:51:29 by charles ### ########.fr */ +/* Updated: 2021/01/03 13:56:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,7 @@ typedef struct long int meal_num; bool all_alive; long int meal_num_finished_counter; + t_time initial_time; pthread_mutex_t mutex_stdout; pthread_mutex_t mutex_meal_num_finished_counter; } t_philo_conf; diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index 6ee37f1..70e6694 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2021/01/02 10:51:36 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 12:23:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,7 +66,7 @@ void *routine_death(t_philo *arg) current - arg->time_last_eat < arg->conf->timeout_death) { current = h_time_now(); - usleep(200); + usleep(1000); } event_die(arg); return (NULL); -- cgit