From 051e5fa4382a629b76f32e46d3e766ed2eb306fc Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sun, 10 Jan 2021 14:26:22 +0100 Subject: Fixing philo_three destructor, Norming philo_three --- philo_three/src/child.c | 44 ++++++++++++-------- philo_three/src/helper.c | 35 ++++++++++++++++ philo_three/src/main.c | 94 ++++++++++++++++++++----------------------- philo_three/src/philo_three.h | 18 ++++++++- 4 files changed, 123 insertions(+), 68 deletions(-) create mode 100644 philo_three/src/helper.c diff --git a/philo_three/src/child.c b/philo_three/src/child.c index 5516151..661e0a6 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/10 12:06:40 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 14:24:05 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,11 +26,12 @@ static void *st_routine_death(t_philo *philo) return (NULL); } -void st_child_loop(t_philo *philo) +static void st_child_loop(t_philo *philo) { long int eat_counter; eat_counter = 0; + philo->time_last_eat = h_time_now(); while (true) { sem_wait(philo->sem_grab); @@ -52,31 +53,42 @@ void st_child_loop(t_philo *philo) #define PHILO_SEM_EAT_PREFIX "semaphore_philo_three_eat_" -pid_t child_start(t_philo *philo) +static bool st_child_setup(t_philo *philo) +{ + if (!h_sem_create(PHILO_SEM_FINISH_NAME, 0, &philo->sem_finish) || + !h_sem_create(philo_sem_eat_name(PHILO_SEM_EAT_PREFIX, philo->id), + 1, &philo->sem_eat) || + !h_sem_create(PHILO_SEM_NAME, 0, &philo->forks) || + !h_sem_create(PHILO_SEM_STDOUT_NAME, 0, &philo->sem_stdout) || + !h_sem_create(PHILO_SEM_MEAL_NUM_NAME, 0, &philo->sem_meal_num) || + !h_sem_create(PHILO_SEM_START_NAME, 0, &philo->sem_start) || + !h_sem_create(PHILO_SEM_GRAB_NAME, 0, &philo->sem_grab)) + return (false); + return (true); +} + +pid_t child_start(t_philo *philo) { pthread_t thread_death; pid_t pid; - const char *sem_eat_name; if ((pid = fork()) == -1) return (-1); if (pid == 0) { - philo->forks = sem_open(PHILO_SEM_NAME, 0); - philo->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, 0); - philo->sem_finish = sem_open(PHILO_SEM_FINISH_NAME, 0); - philo->sem_meal_num = sem_open(PHILO_SEM_MEAL_NUM_NAME, 0); - philo->sem_start = sem_open(PHILO_SEM_START_NAME, 0); - philo->sem_grab = sem_open(PHILO_SEM_GRAB_NAME, 0); - sem_eat_name = philo_sem_eat_name(PHILO_SEM_EAT_PREFIX, philo->id); - sem_unlink(sem_eat_name); - philo->sem_eat = sem_open(sem_eat_name, O_CREAT | O_EXCL, 0700, 1); + philo->sem_finish = SEM_FAILED; + if (!st_child_setup(philo)) + { + if (philo->sem_finish != SEM_FAILED) + sem_post(philo->sem_finish); + exit(1); + } sem_wait(philo->sem_start); philo->time_last_eat = h_time_now(); - if (pthread_create(&thread_death, NULL, (t_routine)st_routine_death, philo) != 0) - exit(1); + if (pthread_create(&thread_death, NULL, + (t_routine)st_routine_death, philo) != 0) + exit(sem_post(philo->sem_finish)); pthread_detach(thread_death); - philo->time_last_eat = h_time_now(); st_child_loop(philo); exit(0); } diff --git a/philo_three/src/helper.c b/philo_three/src/helper.c new file mode 100644 index 0000000..3b718bc --- /dev/null +++ b/philo_three/src/helper.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/01/10 13:58:26 by cacharle #+# #+# */ +/* Updated: 2021/01/10 14:24:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo_three.h" + +void h_destroy_sem(const char *name, sem_t *sem) +{ + if (sem != SEM_FAILED) + sem_close(sem); + sem_unlink(name); +} + +bool h_sem_create(const char *name, unsigned int value, sem_t **sem) +{ + if (value == 0) + { + *sem = sem_open(name, 0); + return (*sem != SEM_FAILED); + } + else + { + sem_unlink(name); + return ((*sem = sem_open(name, + O_CREAT | O_EXCL, 0700, value)) != SEM_FAILED); + } +} diff --git a/philo_three/src/main.c b/philo_three/src/main.c index e3f3022..a3128a0 100644 --- a/philo_three/src/main.c +++ b/philo_three/src/main.c @@ -6,21 +6,15 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */ -/* Updated: 2021/01/10 13:23:54 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 14:25:49 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_three.h" -static sem_t *st_sem_create(char *name, unsigned int value) +static int st_destroy(t_philo_conf *conf, pid_t *pids, long int philo_num) { - sem_unlink(name); - return (sem_open(name, O_CREAT | O_EXCL, 0700, value)); -} - -static int st_destroy(t_philo_conf *conf, pid_t *pids, int philo_num) -{ - int i; + long int i; if (pids != NULL) { @@ -29,45 +23,42 @@ static int st_destroy(t_philo_conf *conf, pid_t *pids, int philo_num) kill(pids[i], SIGKILL); free(pids); } - sem_close(conf->forks); - sem_unlink(PHILO_SEM_NAME); - sem_close(conf->sem_stdout); - sem_unlink(PHILO_SEM_STDOUT_NAME); - sem_close(conf->sem_finish); - sem_unlink(PHILO_SEM_FINISH_NAME); - sem_close(conf->sem_meal_num); - sem_unlink(PHILO_SEM_MEAL_NUM_NAME); - sem_close(conf->sem_start); - sem_unlink(PHILO_SEM_START_NAME); - sem_close(conf->sem_grab); - sem_unlink(PHILO_SEM_GRAB_NAME); + h_destroy_sem(PHILO_SEM_NAME, conf->forks); + h_destroy_sem(PHILO_SEM_STDOUT_NAME, conf->sem_stdout); + h_destroy_sem(PHILO_SEM_FINISH_NAME, conf->sem_finish); + h_destroy_sem(PHILO_SEM_MEAL_NUM_NAME, conf->sem_meal_num); + h_destroy_sem(PHILO_SEM_START_NAME, conf->sem_start); + h_destroy_sem(PHILO_SEM_GRAB_NAME, conf->sem_grab); return (1); } -/* static bool st_sem_create(const char *name, unsigned int value, sem_t **sem) */ -/* { */ -/* sem_unlink(name); */ -/* return ((*sem = sem_open(name, O_CREAT | O_EXCL, 0700, value)) */ -/* != SEM_FAILED); */ -/* } */ - -static int st_setup( - t_philo_conf *conf, pid_t **pids, t_time initial_time) +static int st_setup(t_philo_conf *conf, pid_t **pids) { - t_philo philo; - int i; - + conf->forks = SEM_FAILED; conf->sem_stdout = SEM_FAILED; conf->sem_finish = SEM_FAILED; + conf->sem_meal_num = SEM_FAILED; conf->sem_start = SEM_FAILED; - if ((conf->forks = st_sem_create(PHILO_SEM_NAME, conf->philo_num)) == SEM_FAILED - || (conf->sem_stdout = st_sem_create(PHILO_SEM_STDOUT_NAME, 1)) == SEM_FAILED - || (conf->sem_finish = st_sem_create(PHILO_SEM_FINISH_NAME, 1)) == SEM_FAILED - || (conf->sem_meal_num = st_sem_create(PHILO_SEM_MEAL_NUM_NAME, conf->philo_num)) == SEM_FAILED - || (conf->sem_start = st_sem_create(PHILO_SEM_START_NAME, conf->philo_num)) == SEM_FAILED - || (conf->sem_grab = st_sem_create(PHILO_SEM_GRAB_NAME, 1)) == SEM_FAILED - || (*pids = malloc(sizeof(pid_t) * conf->philo_num)) == NULL) - return (st_destroy(conf, *pids, 0)); + conf->sem_grab = SEM_FAILED; + *pids = NULL; + if (!h_sem_create(PHILO_SEM_NAME, conf->philo_num, &conf->forks) || + !h_sem_create(PHILO_SEM_STDOUT_NAME, 1, &conf->sem_stdout) || + !h_sem_create(PHILO_SEM_FINISH_NAME, 1, &conf->sem_finish) || + !h_sem_create(PHILO_SEM_MEAL_NUM_NAME, + conf->philo_num, &conf->sem_meal_num) || + !h_sem_create(PHILO_SEM_START_NAME, + conf->philo_num, &conf->sem_start) || + !h_sem_create(PHILO_SEM_GRAB_NAME, 1, &conf->sem_grab) || + (*pids = malloc(sizeof(pid_t) * conf->philo_num)) == NULL) + return (1); + return (0); +} + +static int st_start(t_philo_conf *conf, pid_t *pids, t_time initial_time) +{ + t_philo philo; + long int i; + i = -1; while (++i < conf->philo_num) sem_wait(conf->sem_start); @@ -77,8 +68,8 @@ static int st_setup( philo.conf = conf; philo.id = i + 1; philo.initial_time = initial_time; - if (((*pids)[i] = child_start(&philo)) == -1) - return (st_destroy(conf, *pids, i)); + if ((pids[i] = child_start(&philo)) == -1) + return (1); } i = -1; while (++i < conf->philo_num) @@ -86,9 +77,9 @@ static int st_setup( return (0); } -static void *st_routine_meal_num(t_philo_conf *conf) +static void *st_routine_meal_num(t_philo_conf *conf) { - long int i; + long int i; i = -1; while (++i < conf->philo_num) @@ -101,22 +92,23 @@ static void *st_routine_meal_num(t_philo_conf *conf) return (NULL); } -int main(int argc, char **argv) +int main(int argc, char **argv) { t_philo_conf conf; pid_t *pids; + pthread_t thread_meal_num; if (!parse_args((t_philo_args*)&conf, argc, argv)) return (1); if (conf.philo_num == 0 || conf.meal_num == 0) return (0); - pids = NULL; - if (st_setup(&conf, &pids, h_time_now()) != 0) - return (1); + if (st_setup(&conf, &pids) != 0 || st_start(&conf, pids, h_time_now()) != 0) + return (st_destroy(&conf, pids, conf.philo_num)); if (conf.meal_num != -1) { - pthread_t thread_meal_num; - pthread_create(&thread_meal_num, NULL, (t_routine)st_routine_meal_num, &conf); + if (pthread_create(&thread_meal_num, + NULL, (t_routine)st_routine_meal_num, &conf) != 0) + return (st_destroy(&conf, pids, conf.philo_num)); pthread_detach(thread_meal_num); } sem_wait(conf.sem_finish); diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h index f11b8c7..76819d8 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/10 10:26:27 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 14:00:13 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,12 +61,28 @@ typedef struct s_philo sem_t *sem_eat; } t_philo; +/* +** child.c +*/ + pid_t child_start(t_philo *arg); +/* +** event.c +*/ + void event_take_fork(t_philo *arg); void event_eat(t_philo *arg); void event_think(t_philo *arg); void event_sleep(t_philo *arg); void event_die(t_philo *arg); +/* +** helper.c +*/ + +void h_destroy_sem(const char *name, sem_t *sem); +bool h_sem_create( + const char *name, unsigned int value, sem_t **sem); + #endif -- cgit