From bcdbeda7ffa3da60e847517be0be74832dd77a4f Mon Sep 17 00:00:00 2001 From: narnaud Date: Fri, 20 May 2022 10:02:25 +0200 Subject: [PATCH] add: suport meals amount --- philo_bonus/philo.c | 6 +++- philo_bonus/philo.h | 4 +-- philo_bonus/philo_utils.c | 2 +- philo_bonus/philosophers.h | 71 -------------------------------------- 4 files changed, 8 insertions(+), 75 deletions(-) delete mode 100644 philo_bonus/philosophers.h diff --git a/philo_bonus/philo.c b/philo_bonus/philo.c index 728b126..31289ff 100644 --- a/philo_bonus/philo.c +++ b/philo_bonus/philo.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/16 14:03:42 by narnaud #+# #+# */ -/* Updated: 2022/05/20 09:39:41 by narnaud ### ########.fr */ +/* Updated: 2022/05/20 09:57:34 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,8 @@ void *check_halt(void * p) printf("%i %d died\n", time, philo->id); exit(EXIT_SUCCESS); } + if (philo->meals_done == philo->table->param[MEALS_AMOUNT]) + exit(EXIT_SUCCESS); } } @@ -44,6 +46,7 @@ void philo_life(t_philo *philo) philo->last_meal_time = gettime(); printf("%i %d is eating\n", gettime(), philo->id); usleep(table->param[EAT_DURATION] * 1000); + philo->meals_done++; sem_post(table->sptr); sem_post(table->sptr); printf("%i %d is sleeping\n", gettime(), philo->id); @@ -67,6 +70,7 @@ void init_philos(t_table *table) table->pid[i] = fork(); table->philos[i].table = table; table->philos[i].id = i; + table->philos[i].meals_done = 0; if (!table->pid[i]) { pthread_create(&checks, NULL, check_halt, table->philos + i); diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index a5824ac..72e3cde 100644 --- a/philo_bonus/philo.h +++ b/philo_bonus/philo.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/16 14:03:01 by narnaud #+# #+# */ -/* Updated: 2022/05/20 09:26:20 by narnaud ### ########.fr */ +/* Updated: 2022/05/20 09:54:31 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,7 @@ typedef struct s_table t_table; typedef struct s_philo { int id; - size_t meals_done; + int meals_done; int last_meal_time; t_table *table; } t_philo; diff --git a/philo_bonus/philo_utils.c b/philo_bonus/philo_utils.c index 8810a67..c4df637 100644 --- a/philo_bonus/philo_utils.c +++ b/philo_bonus/philo_utils.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/16 14:04:06 by narnaud #+# #+# */ -/* Updated: 2022/05/20 09:40:52 by narnaud ### ########.fr */ +/* Updated: 2022/05/20 09:57:42 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo_bonus/philosophers.h b/philo_bonus/philosophers.h deleted file mode 100644 index 5a9a840..0000000 --- a/philo_bonus/philosophers.h +++ /dev/null @@ -1,71 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* philosophers.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: narnaud +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/05/16 14:03:01 by narnaud #+# #+# */ -/* Updated: 2022/05/16 14:03:20 by narnaud ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef PHILOSOPHERS_H -# define PHILOSOPHERS_H - -/* - -• Chaque philosophe doit être représenté par un thread. -• Toutes les fourchettes sont au centre de la table. -• Elles n’ont pas d’état spécifique en mémoire, mais le nombre de fourchettes -disponibles est représenté par un sémaphore. • Chaque philosophe est représenté -par un processus différent. Cependant, le processus principal ne doit pas être -un philosophe. - -*/ - -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include - -typedef struct s_table t_table; - -typedef struct s_philo -{ - int id; - size_t meals_done; - size_t last_meal_time; - t_table *table; -} t_philo; - -typedef struct s_table -{ - size_t nb_of_philos; - size_t time_to_die; - size_t eating_time; - size_t sleeping_time; - size_t nb_of_meal; - int i; - sem_t *sptr; - sem_t *death; - t_philo *philos; - size_t time; - size_t start_time; - int *pid; -} t_table; - -/***************************/ - -t_table *parsing(char **argv); -size_t gettime(void); - -#endif