From 42360f381cebd6376c261b16038a7f12971f925e Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 30 Sep 2020 08:18:29 +0200 Subject: Added 'has taken fork' log, Refactoring philo_one to work with new common lib --- philo_one/Makefile | 5 ++++- philo_one/src/io.c | 35 ++++++++++++++++------------------- philo_one/src/main.c | 34 ++++++++++++++++------------------ philo_one/src/philo.c | 14 +++----------- philo_one/src/philo_one.h | 29 +++++++++++++---------------- philo_one/src/routine.c | 11 ++++------- 6 files changed, 56 insertions(+), 72 deletions(-) diff --git a/philo_one/Makefile b/philo_one/Makefile index 091bd56..ea58d9e 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -6,11 +6,14 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/09/30 07:45:51 by cacharle ### ########.fr # +# Updated: 2020/09/30 08:00:20 by cacharle ### ########.fr # # # # **************************************************************************** # RM = rm -f +MAKE = make --no-print-directory + +COMMONDIR = ../common CC = gcc CCFLAGS = -I$(COMMONDIR) -Wall -Wextra #-Werror diff --git a/philo_one/src/io.c b/philo_one/src/io.c index 6fb6424..43ae3e6 100644 --- a/philo_one/src/io.c +++ b/philo_one/src/io.c @@ -6,28 +6,23 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2020/09/30 07:49:22 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 08:16:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -/* static void philo_put(int id, t_philo_event event) */ -/* { */ -/* h_putnbr(h_time_now()); */ -/* h_putchar(' '); */ -/* h_putnbr(id); */ -/* if (event == EVENT_FORK) */ -/* h_putstr(" has taken fork\n"); */ -/* else if (event == EVENT_EAT) */ -/* h_putstr(" is eating\n"); */ -/* else if (event == EVENT_SLEEP) */ -/* h_putstr(" is sleeping\n"); */ -/* else if (event == EVENT_THINK) */ -/* h_putstr(" is thinking\n"); */ -/* else if (event == EVENT_DIE) */ -/* h_putstr(" died\n"); */ -/* } */ +void io_take_fork(t_routine_arg *arg, pthread_mutex_t *fork) +{ + pthread_mutex_lock(fork); + pthread_mutex_lock(&arg->conf->mutex_all_alive); + if (!arg->conf->all_alive) + return ; + pthread_mutex_lock(&arg->conf->mutex_stdout); + philo_put(arg->philo->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) { @@ -59,7 +54,7 @@ void io_think(t_routine_arg *arg) pthread_mutex_unlock(&arg->conf->mutex_all_alive); } -void io_sleep(t_routine_arg *arg) +void io_sleep(t_routine_arg *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left) { pthread_mutex_lock(&arg->conf->mutex_all_alive); if (!arg->conf->all_alive) @@ -68,7 +63,9 @@ void io_sleep(t_routine_arg *arg) philo_put(arg->philo->id, EVENT_SLEEP); pthread_mutex_unlock(&arg->conf->mutex_stdout); pthread_mutex_unlock(&arg->conf->mutex_all_alive); - /* usleep(arg->conf->timeout_sleep * 1000); */ + pthread_mutex_unlock(fork_right); + pthread_mutex_unlock(fork_left); + usleep(arg->conf->timeout_sleep * 1000); } void io_die(t_routine_arg *arg) diff --git a/philo_one/src/main.c b/philo_one/src/main.c index 511fdbe..53c1c56 100644 --- a/philo_one/src/main.c +++ b/philo_one/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2020/09/29 13:26:07 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 08:06:03 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,35 +14,33 @@ int main(int argc, char **argv) { - t_philo_conf philo_args; + t_philo_conf conf; t_philo *philos; pthread_mutex_t *forks; t_routine_arg *routine_args; - if (!parse_args(&philo_args, argc, argv)) + if (!parse_args((t_philo_args*)&conf, argc, argv)) return (1); - if (philo_args.philo_num == 0) + if (conf.philo_num == 0) return (0); - if ((forks = forks_new(philo_args.philo_num)) == NULL) + if ((forks = forks_new(conf.philo_num)) == NULL) return (1); - if ((philos = philos_new(philo_args.philo_num)) == NULL) + if ((philos = philos_new(conf.philo_num)) == NULL) return (1); - if ((routine_args = forks_dispatch(philos, forks, &philo_args)) == NULL) + if ((routine_args = forks_dispatch(philos, forks, &conf)) == NULL) return (1); - philo_args.all_alive = true; - pthread_mutex_init(&philo_args.mutex_all_alive, NULL); - pthread_mutex_init(&philo_args.mutex_stdout, NULL); - if (!philos_start(philos, routine_args, philo_args.philo_num)) + conf.all_alive = true; + pthread_mutex_init(&conf.mutex_all_alive, NULL); + pthread_mutex_init(&conf.mutex_stdout, NULL); + if (!philos_start(philos, routine_args, conf.philo_num)) return (1); - /* pthread_mutex_lock(philo_args.mutex_all_alive); */ - /* pthread_mutex_lock(philo_args.mutex_all_alive); */ - while (philo_args.all_alive) + while (conf.all_alive) ; - philos_join(philos, philo_args.philo_num); - pthread_mutex_destroy(&philo_args.mutex_stdout); - pthread_mutex_destroy(&philo_args.mutex_all_alive); + philos_detach(philos, conf.philo_num); + pthread_mutex_destroy(&conf.mutex_stdout); + pthread_mutex_destroy(&conf.mutex_all_alive); free(routine_args); free(philos); - forks_destroy(forks, philo_args.philo_num); + forks_destroy(forks, conf.philo_num); return (0); } diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index e0ecfec..3e21490 100644 --- a/philo_one/src/philo.c +++ b/philo_one/src/philo.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2020/09/29 13:29:49 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 08:10:55 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,10 +23,7 @@ t_philo *philos_new(int num) return (NULL); i = -1; while (++i < num) - { philos[i].id = i + 1; - philos[i].alive = false; - } return (philos); } @@ -37,7 +34,6 @@ bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num) i = -1; while (++i < num) { - philos[i].alive = true; if (pthread_create(&philos[i].thread, NULL, (void *(*)(void*))routine_philo, (void*)(routine_args + i)) == -1) return (false); @@ -45,17 +41,13 @@ bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num) return (true); } -void philos_join(t_philo *philos, int num) +void philos_detach(t_philo *philos, int num) { int i; i = -1; while (++i < num) { - if (philos[i].alive) - { - philos[i].alive = false; - pthread_detach(philos[i].thread); - } + pthread_detach(philos[i].thread); } } diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index 2c04316..2742072 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2020/09/30 07:48:36 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 08:16:11 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,31 +23,27 @@ # include "common.h" -// typedef long int t_time; - -// typedef enum -// { -// EVENT_FORK, -// EVENT_EAT, -// EVENT_SLEEP, -// EVENT_THINK, -// EVENT_DIE -// } t_philo_event; +/* +** t_philo_args with more fields at the end +*/ typedef struct { - t_philo_args args; + long int philo_num; + t_time timeout_death; + t_time timeout_eat; + t_time timeout_sleep; + long int meal_num; bool all_alive; - pthread_mutex_t mutex_stdout; + pthread_mutex_t mutex_stdout; // duplication in t_routine_arg pthread_mutex_t mutex_all_alive; } t_philo_conf; typedef struct s_philo { int id; - bool alive; - t_time time_last_eat; pthread_t thread; + t_time time_last_eat; } t_philo; typedef struct s_routine_arg @@ -93,9 +89,10 @@ void *routine_death(t_routine_arg *arg); ** io.c */ +void io_take_fork(t_routine_arg *arg, pthread_mutex_t *fork); void io_eat(t_routine_arg *arg); void io_think(t_routine_arg *arg); -void io_sleep(t_routine_arg *arg); +void io_sleep(t_routine_arg *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left); void io_die(t_routine_arg *arg); #endif diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index 0115e91..1803b7d 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2020/09/29 14:30:06 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 08:17:21 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,14 +24,11 @@ void *routine_philo(t_routine_arg *arg) io_think(arg); while (arg->conf->all_alive) { - pthread_mutex_lock(arg->fork_left); - pthread_mutex_lock(arg->fork_right); + io_take_fork(arg, arg->fork_left); + io_take_fork(arg, arg->fork_right); arg->philo->time_last_eat = h_time_now(); io_eat(arg); - io_sleep(arg); - pthread_mutex_unlock(arg->fork_right); - pthread_mutex_unlock(arg->fork_left); - usleep(arg->conf->timeout_sleep * 1000); + io_sleep(arg, arg->fork_right, arg->fork_left); io_think(arg); } pthread_join(thread_death, NULL); -- cgit