diff options
| -rw-r--r-- | philo_one/src/philo_one.h | 3 | ||||
| -rw-r--r-- | philo_one/src/routine.c | 35 | ||||
| -rw-r--r-- | philo_three/src/child.c | 8 | ||||
| -rw-r--r-- | philo_three/src/main.c | 4 | ||||
| -rw-r--r-- | philo_two/src/main.c | 102 | ||||
| -rw-r--r-- | philo_two/src/philo_two.h | 12 | ||||
| -rw-r--r-- | philo_two/src/routine.c | 63 | ||||
| -rw-r--r-- | philo_two/src/routine_meta.c | 41 |
8 files changed, 147 insertions, 121 deletions
diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index 2af457e..a327d2e 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2021/01/10 11:42:48 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 12:00:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,7 +77,6 @@ void philos_destroy(t_philo *philos, long int num); bool philo_finished(t_philo_conf *conf); void *routine_philo(t_philo *arg); -void *routine_death(t_philo *arg); /* ** io.c diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index 4e1f39f..4ba4844 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2021/01/10 11:52:44 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 12:05:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,6 +44,20 @@ static void st_take_both_forks(t_philo *arg) } } +static void *st_routine_death(t_philo *arg) +{ + while (!philo_finished(arg->conf)) + { + pthread_mutex_lock(&arg->mutex_eat); + if (h_time_now() - arg->time_last_eat > arg->conf->timeout_death) + break ; + pthread_mutex_unlock(&arg->mutex_eat); + usleep(2000); + } + event_die(arg); + return (NULL); +} + void *routine_philo(t_philo *arg) { pthread_t thread_death; @@ -54,8 +68,10 @@ void *routine_philo(t_philo *arg) if (philo_finished(arg->conf)) return (NULL); arg->time_last_eat = h_time_now(); - if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) + if (pthread_create(&thread_death, NULL, + (t_routine)st_routine_death, arg) != 0) return (NULL); + pthread_detach(thread_death); arg->time_last_eat = h_time_now(); while (!philo_finished(arg->conf)) { @@ -66,20 +82,5 @@ void *routine_philo(t_philo *arg) event_sleep(arg, arg->fork_right, arg->fork_left); event_think(arg); } - pthread_join(thread_death, NULL); - return (NULL); -} - -void *routine_death(t_philo *arg) -{ - while (!philo_finished(arg->conf)) - { - pthread_mutex_lock(&arg->mutex_eat); - if (h_time_now() - arg->time_last_eat > arg->conf->timeout_death) - break ; - pthread_mutex_unlock(&arg->mutex_eat); - usleep(2000); - } - event_die(arg); return (NULL); } diff --git a/philo_three/src/child.c b/philo_three/src/child.c index 63ec3e4..5516151 100644 --- a/philo_three/src/child.c +++ b/philo_three/src/child.c @@ -6,13 +6,13 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 14:36:16 by cacharle #+# #+# */ -/* Updated: 2021/01/10 10:39:19 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 12:06:40 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_three.h" -void *routine_death(t_philo *philo) +static void *st_routine_death(t_philo *philo) { while (true) { @@ -73,8 +73,10 @@ pid_t child_start(t_philo *philo) philo->sem_eat = sem_open(sem_eat_name, O_CREAT | O_EXCL, 0700, 1); sem_wait(philo->sem_start); philo->time_last_eat = h_time_now(); - pthread_create(&thread_death, NULL, (t_routine)routine_death, philo); + if (pthread_create(&thread_death, NULL, (t_routine)st_routine_death, philo) != 0) + exit(1); pthread_detach(thread_death); + philo->time_last_eat = h_time_now(); st_child_loop(philo); exit(0); } diff --git a/philo_three/src/main.c b/philo_three/src/main.c index 64b1004..e3f3022 100644 --- a/philo_three/src/main.c +++ b/philo_three/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */ -/* Updated: 2021/01/09 15:57:47 by charles ### ########.fr */ +/* Updated: 2021/01/10 13:23:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -116,7 +116,7 @@ int main(int argc, char **argv) if (conf.meal_num != -1) { pthread_t thread_meal_num; - pthread_create(&thread_meal_num, NULL, (t_routine)st_routine_meal_num, (void*)&conf); + pthread_create(&thread_meal_num, NULL, (t_routine)st_routine_meal_num, &conf); pthread_detach(thread_meal_num); } sem_wait(conf.sem_finish); diff --git a/philo_two/src/main.c b/philo_two/src/main.c index 197ee5c..2861536 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2021/01/10 10:18:12 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 13:39:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,23 +19,18 @@ #define PHILO_SEM_GRAB_NAME "semaphore_philo_two_grab" #define PHILO_SEM_MEAL_NUM_NAME "semaphore_philo_two_meal_num" -static int st_destroy( - t_philo *philos, - pthread_t *threads) +static int st_destroy(t_philo *philos, long int philo_num) { - /* long int i; */ - sem_unlink(PHILO_SEM_NAME); sem_unlink(PHILO_SEM_STDOUT_NAME); sem_unlink(PHILO_SEM_FINISH_NAME); sem_unlink(PHILO_SEM_MEAL_NUM_NAME); sem_unlink(PHILO_SEM_START_NAME); sem_unlink(PHILO_SEM_GRAB_NAME); - /* i = -1; */ - /* while (++i < philos[0].conf->philo_num) */ - /* sem_unlink(philo_sem_eat_name(philo[i].id)); */ + while (philo_num-- > 0) + sem_unlink(philo_sem_eat_name( + PHILO_SEM_EAT_PREFIX, philos[philo_num].id)); free(philos); - free(threads); return (1); } @@ -46,48 +41,37 @@ static bool st_sem_create(const char *name, unsigned int value, sem_t **sem) != SEM_FAILED); } -void *routine_flush(t_philo_conf *conf) +static int st_setup(t_philo_conf *conf, t_philo **philos) { - while (true) - { - sem_wait(conf->sem_stdout); - philo_put_flush(); - sem_post(conf->sem_stdout); - usleep(250000); - } + if (!st_sem_create(PHILO_SEM_NAME, conf->philo_num, &conf->sem_forks) || + !st_sem_create(PHILO_SEM_STDOUT_NAME, 1, &conf->sem_stdout) || + !st_sem_create(PHILO_SEM_FINISH_NAME, 1, &conf->sem_finish) || + !st_sem_create(PHILO_SEM_START_NAME, + conf->philo_num, &conf->sem_start) || + !st_sem_create(PHILO_SEM_GRAB_NAME, 1, &conf->sem_grab) || + !st_sem_create(PHILO_SEM_MEAL_NUM_NAME, + conf->philo_num, &conf->sem_meal_num)) + return (st_destroy(NULL, 0)); + if ((*philos = routine_create_philos(conf)) == NULL) + return (st_destroy(NULL, 0)); + return (0); } -static int st_setup( - t_philo_conf *conf, - t_philo **philos, - pthread_t **threads) +static int st_start(t_philo_conf *conf, t_philo *philos) { long int i; + pthread_t thread; - if (!st_sem_create(PHILO_SEM_NAME, conf->philo_num, &conf->sem_forks) || - !st_sem_create(PHILO_SEM_STDOUT_NAME, 1, &conf->sem_stdout) || - !st_sem_create(PHILO_SEM_FINISH_NAME, 1, &conf->sem_finish) || - !st_sem_create(PHILO_SEM_MEAL_NUM_NAME, conf->philo_num, &conf->sem_meal_num) || - !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)) == NULL || - (*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL) - return (st_destroy(*philos, *threads)); i = -1; while (++i < conf->philo_num) sem_wait(conf->sem_start); i = -1; while (++i < conf->philo_num) { - if (pthread_create(*threads + i, NULL, - (t_routine)routine_philo, *philos + i) != 0) - { - while (--i >= 0) - pthread_detach((*threads)[i]); - return (st_destroy(*philos, *threads)); - } + if (pthread_create(&thread, NULL, + (t_routine)routine_philo, philos + i) != 0) + return (st_destroy(philos, conf->philo_num)); + pthread_detach(thread); } conf->initial_time = h_time_now(); i = -1; @@ -96,48 +80,30 @@ static int st_setup( return (0); } -static void *st_routine_meal_num(t_philo_conf *conf) -{ - long int i; - - if (conf->meal_num == -1) - return (NULL); - i = -1; - while (++i < conf->philo_num) - sem_wait(conf->sem_meal_num); - i = -1; - while (++i < conf->philo_num) - sem_wait(conf->sem_meal_num); - sem_wait(conf->sem_stdout); - sem_post(conf->sem_finish); - return (NULL); -} - int main(int argc, char **argv) { - long int i; t_philo_conf conf; t_philo *philos; - pthread_t *threads; + pthread_t thread_flush; + 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); - if (st_setup(&conf, &philos, &threads) != 0) + if (st_setup(&conf, &philos) != 0 || st_start(&conf, philos) != 0) return (1); - pthread_t thread_flush; - pthread_create(&thread_flush, NULL, (t_routine)routine_flush, (void*)&conf); + if (pthread_create(&thread_flush, NULL, + (t_routine)routine_flush, &conf) != 0) + return (st_destroy(philos, conf.philo_num)); pthread_detach(thread_flush); - pthread_t thread_meal_num; - pthread_create(&thread_meal_num, NULL, (t_routine)st_routine_meal_num, (void*)&conf); + if (pthread_create(&thread_meal_num, NULL, + (t_routine)routine_meal_num, &conf) != 0) + return (st_destroy(philos, conf.philo_num)); pthread_detach(thread_meal_num); sem_wait(conf.sem_finish); sem_wait(conf.sem_finish); philo_put_flush(); - i = -1; - while (++i < conf.philo_num) - pthread_detach(threads[i]); - st_destroy(philos, threads); + st_destroy(philos, conf.philo_num); return (0); } diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h index 0126430..a186d6c 100644 --- a/philo_two/src/philo_two.h +++ b/philo_two/src/philo_two.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:47:23 by cacharle #+# #+# */ -/* Updated: 2021/01/10 10:03:58 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 13:36:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,8 @@ # include "common.h" +# define PHILO_SEM_EAT_PREFIX "semaphore_philo_two_eat_" + typedef struct { long int philo_num; @@ -51,10 +53,16 @@ typedef struct */ void *routine_philo(t_philo *arg); -void *routine_death(t_philo *arg); t_philo *routine_create_philos(t_philo_conf *conf); /* +** routine_meta.c +*/ + +void *routine_flush(t_philo_conf *conf); +void *routine_meal_num(t_philo_conf *conf); + +/* ** io.c */ diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index ab01958..ec06b19 100644 --- a/philo_two/src/routine.c +++ b/philo_two/src/routine.c @@ -6,13 +6,37 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */ -/* Updated: 2021/01/10 10:30:04 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 13:39:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_two.h" -void *routine_philo(t_philo *arg) +static void *st_routine_death(t_philo *arg) +{ + while (true) + { + sem_wait(arg->sem_eat); + if (h_time_now() - arg->time_last_eat > arg->conf->timeout_death) + break ; + sem_post(arg->sem_eat); + usleep(2000); + } + event_die(arg); + return (NULL); +} + +static void st_check_meal_num_finished(t_philo *arg, long int eat_counter) +{ + if (arg->conf->meal_num != -1 && eat_counter == arg->conf->meal_num) + { + sem_wait(arg->conf->sem_stdout); + sem_post(arg->conf->sem_meal_num); + sem_post(arg->conf->sem_stdout); + } +} + +void *routine_philo(t_philo *arg) { long int eat_counter; pthread_t thread_death; @@ -20,8 +44,11 @@ void *routine_philo(t_philo *arg) eat_counter = 0; sem_wait(arg->conf->sem_start); arg->time_last_eat = h_time_now(); - if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) + if (pthread_create( + &thread_death, NULL, (t_routine)st_routine_death, arg) != 0) return (NULL); + pthread_detach(thread_death); + arg->time_last_eat = h_time_now(); while (true) { sem_wait(arg->conf->sem_grab); @@ -29,36 +56,15 @@ void *routine_philo(t_philo *arg) event_take_fork(arg); sem_post(arg->conf->sem_grab); event_eat(arg); - if (arg->conf->meal_num != -1 && ++eat_counter == arg->conf->meal_num) - { - sem_wait(arg->conf->sem_stdout); - sem_post(arg->conf->sem_meal_num); - sem_post(arg->conf->sem_stdout); - } + eat_counter++; + st_check_meal_num_finished(arg, eat_counter); event_sleep(arg); event_think(arg); } - pthread_join(thread_death, NULL); return (NULL); } -void *routine_death(t_philo *arg) -{ - while (true) - { - sem_wait(arg->sem_eat); - if (h_time_now() - arg->time_last_eat > arg->conf->timeout_death) - break ; - sem_post(arg->sem_eat); - usleep(2000); - } - event_die(arg); - return (NULL); -} - -#define PHILO_SEM_EAT_PREFIX "semaphore_philo_two_eat_" - -t_philo *routine_create_philos(t_philo_conf *conf) +t_philo *routine_create_philos(t_philo_conf *conf) { long int i; t_philo *philos; @@ -76,6 +82,9 @@ t_philo *routine_create_philos(t_philo_conf *conf) philos[i].sem_eat = sem_open(name, O_CREAT | O_EXCL, 0700, 1); if (philos[i].sem_eat == SEM_FAILED) { + while (i-- > 0) + sem_unlink(philo_sem_eat_name( + PHILO_SEM_EAT_PREFIX, philos[i].id)); free(philos); return (NULL); } diff --git a/philo_two/src/routine_meta.c b/philo_two/src/routine_meta.c new file mode 100644 index 0000000..8bbc67a --- /dev/null +++ b/philo_two/src/routine_meta.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* routine_meta.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/01/10 13:31:54 by cacharle #+# #+# */ +/* Updated: 2021/01/10 13:32:38 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo_two.h" + +void *routine_flush(t_philo_conf *conf) +{ + while (true) + { + sem_wait(conf->sem_stdout); + philo_put_flush(); + sem_post(conf->sem_stdout); + usleep(250000); + } +} + +void *routine_meal_num(t_philo_conf *conf) +{ + long int i; + + if (conf->meal_num == -1) + return (NULL); + i = -1; + while (++i < conf->philo_num) + sem_wait(conf->sem_meal_num); + i = -1; + while (++i < conf->philo_num) + sem_wait(conf->sem_meal_num); + sem_wait(conf->sem_stdout); + sem_post(conf->sem_finish); + return (NULL); +} |
