From 99f67bde096ad84dad5b41bc779ae2ad2d807e6f Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 30 Sep 2020 10:35:23 +0200 Subject: Renaming io_* -> event_*, Changed philo_put with only 1 write call --- philo_two/src/event.c | 71 ++++++++++++++++++++++++++++++++++++++++++ philo_two/src/io.c | 78 ----------------------------------------------- philo_two/src/main.c | 16 +++++----- philo_two/src/philo_two.h | 23 +++++++------- philo_two/src/routine.c | 29 ++++++++---------- 5 files changed, 102 insertions(+), 115 deletions(-) create mode 100644 philo_two/src/event.c delete mode 100644 philo_two/src/io.c (limited to 'philo_two') diff --git a/philo_two/src/event.c b/philo_two/src/event.c new file mode 100644 index 0000000..d86f6aa --- /dev/null +++ b/philo_two/src/event.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* io.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ +/* Updated: 2020/09/30 09:56:53 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo_two.h" + +void event_take_fork(t_philo *arg) +{ + sem_wait(arg->forks); + pthread_mutex_lock(&arg->conf->mutex_stdout); + if (!arg->conf->all_alive) + return ; + philo_put(arg->id, EVENT_FORK); + pthread_mutex_unlock(&arg->conf->mutex_stdout); +} + +void event_eat(t_philo *arg) +{ + int eat_counter; + + eat_counter = 0; + while (eat_counter < arg->conf->meal_num) + { + pthread_mutex_lock(&arg->conf->mutex_stdout); + if (!arg->conf->all_alive) + return ; + philo_put(arg->id, EVENT_EAT); + pthread_mutex_unlock(&arg->conf->mutex_stdout); + usleep(arg->conf->timeout_eat * 1000); + eat_counter++; + } +} + +void event_think(t_philo *arg) +{ + pthread_mutex_lock(&arg->conf->mutex_stdout); + if (!arg->conf->all_alive) + return ; + philo_put(arg->id, EVENT_THINK); + pthread_mutex_unlock(&arg->conf->mutex_stdout); +} + +void event_sleep(t_philo *arg) +{ + pthread_mutex_lock(&arg->conf->mutex_stdout); + if (!arg->conf->all_alive) + return ; + philo_put(arg->id, EVENT_SLEEP); + pthread_mutex_unlock(&arg->conf->mutex_stdout); + sem_post(arg->forks); + sem_post(arg->forks); + usleep(arg->conf->timeout_sleep * 1000); +} + +void event_die(t_philo *arg) +{ + pthread_mutex_lock(&arg->conf->mutex_stdout); + if (!arg->conf->all_alive) + return ; + philo_put(arg->id, EVENT_DIE); + arg->conf->all_alive = false; + pthread_mutex_unlock(&arg->conf->mutex_stdout); +} diff --git a/philo_two/src/io.c b/philo_two/src/io.c deleted file mode 100644 index 771d9ee..0000000 --- a/philo_two/src/io.c +++ /dev/null @@ -1,78 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* io.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2020/09/30 08:43:45 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "philo_two.h" - -void io_take_fork(t_routine_arg *arg) -{ - pthread_mutex_lock(&arg->conf->mutex_all_alive); - if (!arg->conf->all_alive) - return ; - sem_wait(arg->forks); - pthread_mutex_lock(&arg->conf->mutex_stdout); - philo_put(arg->id, EVENT_FORK); - pthread_mutex_unlock(&arg->conf->mutex_stdout); - pthread_mutex_unlock(&arg->conf->mutex_all_alive); -} - -void io_eat(t_routine_arg *arg) -{ - int eat_counter; - - eat_counter = 0; - while (eat_counter < arg->conf->meal_num) - { - pthread_mutex_lock(&arg->conf->mutex_all_alive); - if (!arg->conf->all_alive) - return ; - pthread_mutex_lock(&arg->conf->mutex_stdout); - philo_put(arg->id, EVENT_EAT); - pthread_mutex_unlock(&arg->conf->mutex_stdout); - pthread_mutex_unlock(&arg->conf->mutex_all_alive); - usleep(arg->conf->timeout_eat * 1000); - eat_counter++; - } -} - -void io_think(t_routine_arg *arg) -{ - pthread_mutex_lock(&arg->conf->mutex_all_alive); - if (!arg->conf->all_alive) - return ; - pthread_mutex_lock(&arg->conf->mutex_stdout); - philo_put(arg->id, EVENT_THINK); - pthread_mutex_unlock(&arg->conf->mutex_stdout); - pthread_mutex_unlock(&arg->conf->mutex_all_alive); -} - -void io_sleep(t_routine_arg *arg) -{ - pthread_mutex_lock(&arg->conf->mutex_all_alive); - if (!arg->conf->all_alive) - return ; - pthread_mutex_lock(&arg->conf->mutex_stdout); - philo_put(arg->id, EVENT_SLEEP); - pthread_mutex_unlock(&arg->conf->mutex_stdout); - pthread_mutex_unlock(&arg->conf->mutex_all_alive); - sem_post(arg->forks); - sem_post(arg->forks); - usleep(arg->conf->timeout_sleep * 1000); -} - -void io_die(t_routine_arg *arg) -{ - if (!arg->conf->all_alive) - return ; - pthread_mutex_lock(&arg->conf->mutex_stdout); - philo_put(arg->id, EVENT_DIE); - pthread_mutex_unlock(&arg->conf->mutex_stdout); -} diff --git a/philo_two/src/main.c b/philo_two/src/main.c index 27dce34..c0c9fd6 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,39 +6,38 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2020/09/30 08:44:46 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 09:58:01 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_two.h" -#define PHILO_SEM_NAME "philo_two" +#define PHILO_SEM_NAME "semaphore_philo_two" int main(int argc, char **argv) { int i; t_philo_conf conf; - t_routine_arg *routine_args; + t_philo *philos; sem_t *forks; pthread_t *threads; if (!parse_args((t_philo_args*)&conf, argc, argv)) return (1); sem_unlink(PHILO_SEM_NAME); - forks = sem_open(PHILO_SEM_NAME, O_CREAT, 0700, conf.philo_num); + forks = sem_open(PHILO_SEM_NAME, O_CREAT | O_EXCL, 0600, conf.philo_num); if (forks == SEM_FAILED) return (1); - if ((routine_args = routine_args_create(&conf, forks)) == NULL) + if ((philos = routine_create_philos(&conf, forks)) == NULL) return (1); if ((threads = malloc(sizeof(pthread_t) * conf.philo_num)) == NULL) return (1); conf.all_alive = true; - pthread_mutex_init(&conf.mutex_all_alive, NULL); pthread_mutex_init(&conf.mutex_stdout, NULL); i = -1; while (++i < conf.philo_num) - if (pthread_create(threads + i, NULL, (t_routine)routine_philo, routine_args + i) < 0) + if (pthread_create(threads + i, NULL, (t_routine)routine_philo, philos + i) < 0) return (1); while (conf.all_alive) ; @@ -50,8 +49,7 @@ int main(int argc, char **argv) sem_unlink(PHILO_SEM_NAME); pthread_mutex_destroy(&conf.mutex_stdout); - pthread_mutex_destroy(&conf.mutex_all_alive); free(threads); - free(routine_args); + free(philos); return (0); } diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h index 5318510..a697382 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: 2020/09/30 08:41:15 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 10:02:50 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,34 +28,33 @@ typedef struct t_time timeout_sleep; long int meal_num; bool all_alive; - pthread_mutex_t mutex_all_alive; pthread_mutex_t mutex_stdout; } t_philo_conf; typedef struct { - int id; + long int id; t_philo_conf *conf; t_time time_last_eat; sem_t *forks; -} t_routine_arg; +} t_philo; /* ** routine.c */ -void *routine_philo(t_routine_arg *arg); -void *routine_death(t_routine_arg *arg); -t_routine_arg *routine_args_create(t_philo_conf *conf, sem_t *forks); +void *routine_philo(t_philo *arg); +void *routine_death(t_philo *arg); +t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks); /* ** io.c */ -void io_take_fork(t_routine_arg *arg); -void io_eat(t_routine_arg *arg); -void io_think(t_routine_arg *arg); -void io_sleep(t_routine_arg *arg); -void io_die(t_routine_arg *arg); +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); #endif diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index cfce645..6554c89 100644 --- a/philo_two/src/routine.c +++ b/philo_two/src/routine.c @@ -6,17 +6,17 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */ -/* Updated: 2020/09/30 08:41:23 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 09:57:41 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_two.h" -void *routine_philo(t_routine_arg *arg) +void *routine_philo(t_philo *arg) { pthread_t thread_death; - io_think(arg); + event_think(arg); if (!arg->conf->all_alive) return (NULL); arg->time_last_eat = h_time_now(); @@ -24,18 +24,18 @@ void *routine_philo(t_routine_arg *arg) return (NULL); while (arg->conf->all_alive) { - io_take_fork(arg); - io_take_fork(arg); - io_eat(arg); + event_take_fork(arg); + event_take_fork(arg); + event_eat(arg); arg->time_last_eat = h_time_now(); - io_sleep(arg); - io_think(arg); + event_sleep(arg); + event_think(arg); } pthread_join(thread_death, NULL); return (NULL); } -void *routine_death(t_routine_arg *arg) +void *routine_death(t_philo *arg) { t_time current; @@ -43,19 +43,16 @@ void *routine_death(t_routine_arg *arg) while (arg->conf->all_alive && current - arg->time_last_eat < arg->conf->timeout_death) current = h_time_now(); - pthread_mutex_lock(&arg->conf->mutex_all_alive); - io_die(arg); - arg->conf->all_alive = false; - pthread_mutex_unlock(&arg->conf->mutex_all_alive); + event_die(arg); return (NULL); } -t_routine_arg *routine_args_create(t_philo_conf *conf, sem_t *forks) +t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks) { int i; - t_routine_arg *routine_conf; + t_philo *routine_conf; - if ((routine_conf = malloc(sizeof(t_routine_arg) * conf->philo_num)) == NULL) + if ((routine_conf = malloc(sizeof(t_philo) * conf->philo_num)) == NULL) return (NULL); i = -1; while (++i < conf->philo_num) -- cgit