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_three/src/child.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'philo_three/src/child.c') diff --git a/philo_three/src/child.c b/philo_three/src/child.c index 88a4b7c..144f7ba 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/08 15:42:02 by charles ### ########.fr */ +/* Updated: 2021/01/08 20:52:22 by charles ### ########.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(500); + usleep(1000); } event_die(philo); return (NULL); @@ -33,7 +33,10 @@ void st_child_loop(t_philo *philo) eat_counter = 0; while (true) { + sem_wait(philo->sem_grab); event_take_fork(philo); + event_take_fork(philo); + sem_post(philo->sem_grab); philo->time_last_eat = h_time_now(); event_eat(philo); eat_counter++; @@ -61,10 +64,10 @@ pid_t child_start(t_philo *philo) philo->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, 0); philo->sem_finish = sem_open(PHILO_SEM_FINISH_NAME, 0); philo->sem_start = sem_open(PHILO_SEM_START_NAME, 0); + philo->sem_grab = sem_open(PHILO_SEM_GRAB_NAME, 0); philo->time_last_eat = h_time_now(); pthread_create(&thread_death, NULL, (t_routine)routine_death, philo); pthread_detach(thread_death); - event_think(philo); sem_wait(philo->sem_start); st_child_loop(philo); exit(0); -- cgit