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_three/src/child.c | 4 ++-- philo_three/src/event.c | 12 ++++++------ philo_three/src/main.c | 10 ++++++---- philo_three/src/philo_three.h | 3 ++- 4 files changed, 16 insertions(+), 13 deletions(-) (limited to 'philo_three/src') diff --git a/philo_three/src/child.c b/philo_three/src/child.c index bce5d21..e9c743b 100644 --- a/philo_three/src/child.c +++ b/philo_three/src/child.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 14:36:16 by cacharle #+# #+# */ -/* Updated: 2021/01/02 11:58:03 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 13:47:27 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ void *routine_death(t_philo *philo) while (current - philo->time_last_eat < philo->conf->timeout_death) { current = h_time_now(); - usleep(200); + usleep(500); } event_die(philo); return (NULL); diff --git a/philo_three/src/event.c b/philo_three/src/event.c index 33b1e00..10f82c4 100644 --- a/philo_three/src/event.c +++ b/philo_three/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/01 16:29:31 by charles ### ########.fr */ +/* Updated: 2021/01/03 13:51:20 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,14 @@ void event_take_fork(t_philo *philo) { sem_wait(philo->forks); sem_wait(philo->sem_stdout); - philo_put(philo->id, EVENT_FORK); + philo_put(philo->id, EVENT_FORK, philo->initial_time); sem_post(philo->sem_stdout); } void event_eat(t_philo *philo) { sem_wait(philo->sem_stdout); - philo_put(philo->id, EVENT_EAT); + philo_put(philo->id, EVENT_EAT, philo->initial_time); sem_post(philo->sem_stdout); usleep(philo->conf->timeout_eat * 1000); } @@ -31,14 +31,14 @@ void event_eat(t_philo *philo) void event_think(t_philo *philo) { sem_wait(philo->sem_stdout); - philo_put(philo->id, EVENT_THINK); + philo_put(philo->id, EVENT_THINK, philo->initial_time); sem_post(philo->sem_stdout); } void event_sleep(t_philo *philo) { sem_wait(philo->sem_stdout); - philo_put(philo->id, EVENT_SLEEP); + philo_put(philo->id, EVENT_SLEEP, philo->initial_time); sem_post(philo->sem_stdout); sem_post(philo->forks); sem_post(philo->forks); @@ -50,7 +50,7 @@ void event_die(t_philo *philo) long int i; sem_wait(philo->sem_stdout); - philo_put(philo->id, EVENT_DIE); + philo_put(philo->id, EVENT_DIE, philo->initial_time); if (philo->conf->meal_num == -1) sem_post(philo->sem_finish); else diff --git a/philo_three/src/main.c b/philo_three/src/main.c index 9597fc0..8a5dec4 100644 --- a/philo_three/src/main.c +++ b/philo_three/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */ -/* Updated: 2021/01/02 12:49:44 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 14:01:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,14 +38,14 @@ static int st_destroy(t_sems *sems, pid_t *pids, int philo_num) return (1); } -static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids) +static int st_setup( + t_philo_args *args, t_sems *sems, pid_t **pids, t_time initial_time) { t_philo philo; int i; sems->sem_stdout = SEM_FAILED; sems->sem_finish = SEM_FAILED; - *pids = NULL; if ((sems->forks = st_sem_create(PHILO_SEM_NAME, args->philo_num)) == SEM_FAILED || (sems->sem_stdout = @@ -60,6 +60,7 @@ static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids) { philo.conf = args; philo.id = i + 1; + philo.initial_time = initial_time; if (((*pids)[i] = child_start(&philo)) == -1) return (st_destroy(sems, *pids, i)); usleep(200); @@ -97,7 +98,8 @@ int main(int argc, char **argv) return (1); if (args.philo_num == 0 || args.meal_num == 0) return (0); - if (st_setup(&args, &sems, &pids) != 0) + pids = NULL; + if (st_setup(&args, &sems, &pids, h_time_now()) != 0) return (1); st_wait(&args, &sems); st_destroy(&sems, pids, args.philo_num); diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h index 380efc5..3d6b138 100644 --- a/philo_three/src/philo_three.h +++ b/philo_three/src/philo_three.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:46:26 by cacharle #+# #+# */ -/* Updated: 2021/01/01 15:34:04 by charles ### ########.fr */ +/* Updated: 2021/01/03 13:51:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,7 @@ typedef struct s_philo t_philo_args *conf; int id; t_time time_last_eat; + t_time initial_time; sem_t *forks; sem_t *sem_stdout; sem_t *sem_finish; -- cgit