From adbe9cdb61e5d12299b8872b2ac27a48036bc95d Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 14 Feb 2020 01:42:10 +0100 Subject: WIP: philo_one running a bit --- philo_one/Makefile | 6 ++++-- philo_one/fork.c | 6 +++--- philo_one/main.c | 12 +++++++----- philo_one/philo.c | 9 +++++---- philo_one/philo_one.h | 10 +++++----- philo_one/routine.c | 48 +++++++++++++++++++++++++++++------------------- 6 files changed, 53 insertions(+), 38 deletions(-) (limited to 'philo_one') diff --git a/philo_one/Makefile b/philo_one/Makefile index e647316..b043ab5 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -6,7 +6,7 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/02/10 00:16:03 by cacharle ### ########.fr # +# Updated: 2020/02/14 00:50:47 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -22,7 +22,9 @@ NAME = philo_one SRC = main.c \ philo.c \ - fork.c + fork.c \ + routine.c + OBJ = $(SRC:.c=.o) all: $(NAME) diff --git a/philo_one/fork.c b/philo_one/fork.c index d42510b..62e3355 100644 --- a/philo_one/fork.c +++ b/philo_one/fork.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:46:40 by cacharle #+# #+# */ -/* Updated: 2020/02/10 00:34:17 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:29:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ void forks_destroy(t_fork *forks, int num) while (num-- > 0) { forks[num].used = TRUE; - pthread_mutex_destroy(&forks[num].mutex); + /* pthread_mutex_destroy(&forks[num].mutex); */ } } @@ -52,7 +52,7 @@ t_routine_arg *forks_dispatch(t_philo *philos, t_fork *forks, t_philo_args *args { routine_args[i].args = args; routine_args[i].philo = philos + i; - routine_args[i].fork_left = forks + i % args->philo_num; + routine_args[i].fork_right = forks + i % args->philo_num; routine_args[i].fork_left = forks + (i - 1) % args->philo_num; } return (routine_args); diff --git a/philo_one/main.c b/philo_one/main.c index ef00a0b..94ac0f7 100644 --- a/philo_one/main.c +++ b/philo_one/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2020/02/10 01:29:50 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:41:21 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,10 +19,12 @@ int main(int argc, char **argv) t_philo *philos; t_fork *forks; t_routine_arg *routine_args; + /* mutex_t mutex_stdout; */ + /* pthread_mutex_init(mutex_stdout); */ if (!parse_args(&philo_args, argc, argv)) return (1); - if ((forks = forks_new(philo_args.philo_num - 1)) == NULL) + if ((forks = forks_new(philo_args.philo_num)) == NULL) return (1); if ((philos = philos_new(philo_args.philo_num)) == NULL) { @@ -31,8 +33,8 @@ int main(int argc, char **argv) } if ((routine_args = forks_dispatch(philos, forks, &philo_args)) == NULL) { - free(forks); free(philos); + free(forks); return (1); } if (!philos_start(philos, routine_args, philo_args.philo_num)) @@ -46,8 +48,8 @@ int main(int argc, char **argv) if (philos_starved(philos, philo_args.philo_num)) break; philos_join(philos, philo_args.philo_num); - free(philos); - free(forks); free(routine_args); + free(philos); + forks_destroy(forks, philo_args.philo_num); return (0); } diff --git a/philo_one/philo.c b/philo_one/philo.c index 7cc3ca4..c585e34 100644 --- a/philo_one/philo.c +++ b/philo_one/philo.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2020/02/10 01:29:25 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:38:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,14 +40,13 @@ void philos_destroy(t_philo *philos, int num) t_bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num) { - t_routine_arg *routine_arg; int i; i = -1; while (++i < num) { philos[i].alive = TRUE; - if (pthread_create(philos[i].thread, NULL, philo_one_routine, (void*)(routine_args + i)) == -1) + if (pthread_create(&philos[i].thread, NULL, &routine_philo, (void*)(routine_args + i)) == -1) return (FALSE); } return (TRUE); @@ -63,7 +62,7 @@ void philos_join(t_philo *philos, int num) if (philos[i].alive) { philos[i].alive = FALSE; - pthread_join(philos[i].thread, NULL); + /* pthread_join(philos[i].thread, NULL); */ } } } @@ -74,6 +73,7 @@ t_bool philos_starved(t_philo *philos, int num) i = -1; while (++i < num) + { if (!philos[i].alive) { i = -1; @@ -81,5 +81,6 @@ t_bool philos_starved(t_philo *philos, int num) philos[i].alive = FALSE; return (TRUE); } + } return (FALSE); } diff --git a/philo_one/philo_one.h b/philo_one/philo_one.h index bcac634..9c888ee 100644 --- a/philo_one/philo_one.h +++ b/philo_one/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2020/02/10 01:14:27 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:41:39 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ struct s_philo int id; t_bool alive; t_watchdog watchdog; - int time_last_eat; + t_time time_last_eat; t_philo_state state; pthread_t thread; }; @@ -55,8 +55,8 @@ typedef struct t_philo *philo; t_fork *fork_left; t_fork *fork_right; + pthread_mutex_t *mutex_stdout; } t_routine_arg; -tine_death_arg; /* ** fork.c @@ -80,7 +80,7 @@ t_bool philos_starved(t_philo *philos, int num); ** routine.c */ -void *routine_philo(t_routine_arg *arg); -void *routine_death(t_routine_arg *arg); +void *routine_philo(void *void_arg); +void *routine_death(void *void_arg); #endif diff --git a/philo_one/routine.c b/philo_one/routine.c index 48f3c0d..713e4ee 100644 --- a/philo_one/routine.c +++ b/philo_one/routine.c @@ -6,48 +6,58 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2020/02/10 01:19:13 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 00:45:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -void *routine_philo(t_routine_arg *routine_arg) +void *routine_philo(void *void_arg) { - pthread_t thread_death; + t_routine_arg *arg; + pthread_t thread_death; - if (pthread_create(&thread_death, NULL, routine_death, routine_arg) != 0) + arg = (t_routine_arg*)void_arg; + if (pthread_create(&thread_death, NULL, routine_death, arg) != 0) return (NULL); - philo_think(routine_arg->philo->id); - while (routine_arg->philo->alive && !philo_starved(routine_arg->philo)) + philo_think(arg->philo->id); + while (arg->philo->alive) { - if (!routine_arg->fork_left->used && !routine_arg->fork_right->used) + if (!arg->fork_left->used && !arg->fork_right->used) { - pthread_mutex_lock(&routine_arg->fork_left->mutex); - pthread_mutex_lock(&routine_arg->fork_right->mutex); - philo_eat(routine_arg->philo->id, routine_arg->args->timeout_eat); - pthread_mutex_unlock(&routine_arg->fork_left->mutex); - pthread_mutex_unlock(&routine_arg->fork_right->mutex); - philo_sleep(routine_arg->philo->id, routine_arg->args->timeout_sleep); - philo_think(routine_arg->philo->id); + pthread_mutex_lock(&arg->fork_left->mutex); + pthread_mutex_lock(&arg->fork_right->mutex); + philo_eat(arg->philo->id, arg->args->timeout_eat); + pthread_mutex_unlock(&arg->fork_left->mutex); + pthread_mutex_unlock(&arg->fork_right->mutex); + philo_sleep(arg->philo->id, arg->args->timeout_sleep); + philo_think(arg->philo->id); } } - if (routine_arg->philo->alive) - philo_die(routine_arg->philo->id); - free(routine_arg); + if (arg->philo->alive) + philo_die(arg->philo->id); + /* pthread_join( */ + free(arg); return (NULL); } -void *routine_death(t_routine_arg *arg) +void *routine_death(void *void_arg) { + t_routine_arg *arg; + t_time current; struct timeval tv; + arg = (t_routine_arg*)void_arg; if (gettimeofday(&tv, NULL) == -1) return (NULL); + current = h_timeval_to_time(&tv); while (arg->philo->alive && - arg->philo->time_last_eat - tv.asd > arg->args->timeout_death) + current - arg->philo->time_last_eat < arg->args->timeout_death) + { if (gettimeofday(&tv, NULL) == -1) return (NULL); + current = h_timeval_to_time(&tv); + } arg->philo->alive = FALSE; return (NULL); } -- cgit