From a69a877b3a2758aafe3de0db87a56063b28ed00f Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Fri, 8 Jan 2021 22:11:19 +0100 Subject: Add sem_grab in philo_two and philo_three to fix philosophers each taking 1 fork --- philo_two/src/routine.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'philo_two/src/routine.c') diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index d69488d..52bf53d 100644 --- a/philo_two/src/routine.c +++ b/philo_two/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */ -/* Updated: 2021/01/08 16:31:27 by charles ### ########.fr */ +/* Updated: 2021/01/08 20:18:00 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,18 +19,15 @@ void *routine_philo(t_philo *arg) eat_counter = 0; sem_wait(arg->conf->sem_start); - /* if (arg->conf->philo_num % 2 == 0 && arg->id % 2 == 0) */ - /* usleep(1000); */ - /* if (arg->conf->philo_num % 2 == 1 && arg->id % 3 == 0) */ - /* usleep(1000); */ - /* if (arg->conf->philo_num % 2 == 1 && arg->id % 3 == 1) */ - /* usleep(500); */ arg->time_last_eat = h_time_now(); if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); while (true) { + sem_wait(arg->conf->sem_grab); event_take_fork(arg); + event_take_fork(arg); + sem_post(arg->conf->sem_grab); arg->time_last_eat = h_time_now(); event_eat(arg); if (arg->conf->meal_num != -1 && ++eat_counter == arg->conf->meal_num) -- cgit