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/event.c | 4 +--- philo_two/src/main.c | 17 +++++++++++------ philo_two/src/philo_two.h | 3 ++- philo_two/src/routine.c | 11 ++++------- 4 files changed, 18 insertions(+), 17 deletions(-) (limited to 'philo_two/src') diff --git a/philo_two/src/event.c b/philo_two/src/event.c index 4eafd90..3ce471e 100644 --- a/philo_two/src/event.c +++ b/philo_two/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/04 12:15:05 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 19:54:40 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,9 @@ void event_take_fork(t_philo *arg) { - sem_wait(arg->forks); sem_wait(arg->forks); sem_wait(arg->conf->sem_stdout); philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); - philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); sem_post(arg->conf->sem_stdout); } diff --git a/philo_two/src/main.c b/philo_two/src/main.c index 2d5c003..034518c 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2021/01/08 16:32:15 by charles ### ########.fr */ +/* Updated: 2021/01/08 20:16:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout" #define PHILO_SEM_FINISH_NAME "semaphore_philo_two_finish" #define PHILO_SEM_START_NAME "semaphore_philo_two_start" +#define PHILO_SEM_GRAB_NAME "semaphore_philo_two_grab" static int st_destroy( t_philo *philos, @@ -25,6 +26,7 @@ static int st_destroy( sem_unlink(PHILO_SEM_STDOUT_NAME); sem_unlink(PHILO_SEM_FINISH_NAME); sem_unlink(PHILO_SEM_START_NAME); + sem_unlink(PHILO_SEM_GRAB_NAME); free(philos); free(threads); return (1); @@ -60,7 +62,8 @@ static int st_setup( !st_sem_create(PHILO_SEM_STDOUT_NAME, 1, &conf->sem_stdout) || !st_sem_create(PHILO_SEM_FINISH_NAME, conf->meal_num == -1 ? 1 : conf->philo_num, &conf->sem_finish) || - !st_sem_create(PHILO_SEM_START_NAME, conf->philo_num, &conf->sem_start)) + !st_sem_create(PHILO_SEM_START_NAME, conf->philo_num, &conf->sem_start) || + !st_sem_create(PHILO_SEM_GRAB_NAME, 1, &conf->sem_grab)) return (1); *threads = NULL; if ((*philos = routine_create_philos(conf, *forks)) == NULL || @@ -69,9 +72,9 @@ static int st_setup( i = -1; while (++i < conf->philo_num) sem_wait(conf->sem_start); - conf->initial_time = h_time_now(); i = -1; while (++i < conf->philo_num) + { if (pthread_create(*threads + i, NULL, (t_routine)routine_philo, *philos + i) != 0) { @@ -79,6 +82,11 @@ static int st_setup( pthread_detach((*threads)[i]); return (st_destroy(*philos, *threads)); } + } + conf->initial_time = h_time_now(); + i = -1; + while (++i < conf->philo_num) + sem_post(conf->sem_start); return (0); } @@ -119,9 +127,6 @@ int main(int argc, char **argv) pthread_t thread_flush; pthread_create(&thread_flush, NULL, (t_routine)routine_flush, (void*)&conf); pthread_detach(thread_flush); - i = -1; - while (++i < conf.philo_num) - sem_post(conf.sem_start); st_wait(&conf); philo_put_flush(); i = -1; diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h index bde41c1..91cdbef 100644 --- a/philo_two/src/philo_two.h +++ b/philo_two/src/philo_two.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:47:23 by cacharle #+# #+# */ -/* Updated: 2021/01/04 09:35:42 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 20:10:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,7 @@ typedef struct sem_t *sem_stdout; sem_t *sem_finish; sem_t *sem_start; + sem_t *sem_grab; } t_philo_conf; typedef struct 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