diff --git a/philo/check_death.c b/philo/check_death.c new file mode 100644 index 0000000..2801b82 --- /dev/null +++ b/philo/check_death.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_death.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/24 23:03:08 by adjoly #+# #+# */ +/* Updated: 2024/07/24 23:05:15 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +bool get_death(bool in, bool ret) +{ + static bool death; + + if (ret == false) + death = in; + return (death); +} diff --git a/philo/get_time_in_ms.c b/philo/get_time_in_ms.c index 02bcc85..093f291 100644 --- a/philo/get_time_in_ms.c +++ b/philo/get_time_in_ms.c @@ -6,17 +6,16 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/09 12:02:24 by adjoly #+# #+# */ -/* Updated: 2024/07/16 21:12:05 by adjoly ### ########.fr */ +/* Updated: 2024/07/24 23:51:20 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include "philo.h" uint16_t get_time_in_ms(struct timeval t0, struct timeval t1) { - int diff; - - diff = t1.tv_sec - t0.tv_sec; - return (((diff) * 1000000 + diff) / 1000); + return (((t1.tv_sec - t0.tv_sec) * 1000000 + \ + t1.tv_usec - t0.tv_usec) / 1000); } diff --git a/philo/init_fork.c b/philo/init_fork.c index a96d9fc..ce5589d 100644 --- a/philo/init_fork.c +++ b/philo/init_fork.c @@ -6,20 +6,27 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/22 15:29:08 by adjoly #+# #+# */ -/* Updated: 2024/07/22 17:37:26 by adjoly ### ########.fr */ +/* Updated: 2024/07/24 18:47:29 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -t_fork init_fork(t_init *init, uint16_t i) +void init_fork(t_init *init, uint16_t philo_nbr) { - t_fork fork; - - if (i != 0) - fork.right = &(init[i - 1].data.fork.left); - else - fork.right = &(init[init[i].data.data.philo_nbr - 1].data.fork.left); - pthread_mutex_init(&(init[i].data.fork.left), NULL); - return (fork); + uint16_t i; + + i = 0; + while (i < philo_nbr) + { + pthread_mutex_init(&(init[i].data.fork.left), NULL); + i++; + } + init[0].data.fork.right = &(init[philo_nbr - 1].data.fork.left); + i = 1; + while (i < philo_nbr) + { + init[i].data.fork.right = &(init[i - 1].data.fork.left); + i++; + } } diff --git a/philo/init_philo.c b/philo/init_philo.c index c9fb34a..b0c3e71 100644 --- a/philo/init_philo.c +++ b/philo/init_philo.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 14:36:59 by adjoly #+# #+# */ -/* Updated: 2024/07/23 17:53:14 by adjoly ### ########.fr */ +/* Updated: 2024/07/25 15:14:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,21 +15,25 @@ #include void init_philo(t_pdata data) { - t_init init[PHILO_MAX]; - int r; - size_t i; + t_init init[PHILO_MAX]; + int r; + pthread_mutex_t check; + uint16_t i; i = 0; + pthread_mutex_init(&check, NULL); + init_fork(init, init[0].data.data.philo_nbr); while (i < data.philo_nbr) { - init[i].data.nbr = i; + init[i].data.id = i; init[i].data.data = data; init[i].data.state = EAT; - init[i].data.fork = init_fork(init, i); - r = pthread_create(&(init[i].thread), NULL, - philo_routine, &init[i].data); + init[i].data.check = ✓ + r = pthread_create(&init[i].thread, NULL, + philo_routine, &(init[i].data)); if (r != 0) - return ; + break ; i++; } + start_philo(init, i); } diff --git a/philo/log.c b/philo/log.c index efc5cb6..1ea18c0 100644 --- a/philo/log.c +++ b/philo/log.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */ -/* Updated: 2024/07/22 21:21:21 by adjoly ### ########.fr */ +/* Updated: 2024/07/24 23:56:07 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,22 +14,24 @@ #include "philo_msg.h" #include -void log_philo(struct timeval t1, t_philo philo) +void log_philo(t_philo philo) { uint32_t timestamp; + struct timeval t1; static pthread_mutex_t print = {0}; + gettimeofday(&t1, NULL); timestamp = get_time_in_ms(philo.t0, t1); pthread_mutex_lock(&print); if (philo.state == EAT) - printf("%u %hu %s", timestamp, philo.nbr, EATING_MSG); + printf("%u %hu %s", timestamp, philo.id, EATING_MSG); else if (philo.state == THINK) - printf("%u %hu %s", timestamp, philo.nbr, THINK_MSG); + printf("%u %hu %s", timestamp, philo.id, THINK_MSG); else if (philo.state == SLEEP) - printf("%u %hu %s", timestamp, philo.nbr, SLEEP_MSG); - else if (philo.state == DIED) - printf("%u %hu %s", timestamp, philo.nbr, DIED_MSG); + printf("%u %hu %s", timestamp, philo.id, SLEEP_MSG); + else if (philo.state == DEAD) + printf("%u %hu %s", timestamp, philo.id, DIED_MSG); else if (philo.state == FORK_TAKEN) - printf("%u %hu %s", timestamp, philo.nbr, FORK_MSG); + printf("%u %hu %s", timestamp, philo.id, FORK_MSG); pthread_mutex_unlock(&print); } diff --git a/philo/note b/philo/note new file mode 100644 index 0000000..f1f20a1 --- /dev/null +++ b/philo/note @@ -0,0 +1,15 @@ +EAT +lock left +lock right +geteattime +print +sleep time_to_eat ms + +SLEEP +print +sleep time_to_sleep ms + +THINK +print + +REPEAT diff --git a/philo/philo.h b/philo/philo.h index 825dd9d..2d4b02c 100644 --- a/philo/philo.h +++ b/philo/philo.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 15:11:02 by adjoly #+# #+# */ -/* Updated: 2024/07/23 17:23:38 by adjoly ### ########.fr */ +/* Updated: 2024/07/25 00:03:53 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,12 +19,13 @@ # include # include # include +# include # define PHILO_MAX 200 typedef enum s_pstate { - DIED = -1, + DEAD = -1, EAT, THINK, SLEEP, @@ -49,11 +50,12 @@ typedef struct s_fork typedef struct s_philo { - uint16_t nbr; + uint16_t id; struct timeval t0; t_fork fork; t_pstate state; t_pdata data; + pthread_mutex_t *check; } t_philo; typedef struct s_init @@ -68,15 +70,17 @@ typedef struct s_init t_pdata philo_parse(char **argv, int ac); long long ft_atoll(const char *nptr); uint16_t get_time_in_ms(struct timeval t0, struct timeval t1); -void log_philo(struct timeval t1, t_philo philo); -void sleep_phil(uint32_t sleep_time); +void log_philo(t_philo philo); +bool sleep_phil(uint32_t sleep_time, pthread_mutex_t *death); +bool get_death(bool in, bool ret); /** * Main path * by order of call */ void init_philo(t_pdata data); -t_fork init_fork(t_init *init, uint16_t nbr); +void init_fork(t_init *init, uint16_t philo_nbr); +void start_philo(t_init *init, uint16_t philo_nbr); void *philo_routine(void *content); diff --git a/philo/philo_routine.c b/philo/philo_routine.c index fac9a5c..cfd9bcf 100644 --- a/philo/philo_routine.c +++ b/philo/philo_routine.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/22 21:24:53 by adjoly #+# #+# */ -/* Updated: 2024/07/23 18:07:56 by adjoly ### ########.fr */ +/* Updated: 2024/07/25 16:12:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,16 +16,28 @@ void *philo_routine(void *content) { t_philo philo; -// struct timeval t1; + bool death; + int i = 0; philo = *(t_philo *)content; - printf("\t\t%hu\n", philo.nbr); - //print_philo_data(philo.data); -// while (1) -// { -// gettimeofday(&t1, NULL); -// log_philo(t1, philo); -// sleep_phil(philo.data.eat_time); -// } + gettimeofday(&(philo.t0), NULL); + while (i < philo.data.meal_nbr) + { + philo.state = EAT; + pthread_mutex_lock(&philo.fork.left); + pthread_mutex_lock(philo.fork.right); + log_philo(philo); + death = sleep_phil(philo.data.eat_time, philo.check); + if (death == true) + return (NULL); + philo.state = SLEEP; + log_philo(philo); + death = sleep_phil(philo.data.sleep_time, philo.check); + if (death == true) + return (NULL); + philo.state = THINK; + log_philo(philo); + i++; + } return (NULL); } diff --git a/philo/sleep.c b/philo/sleep.c index b98e6a0..6517e16 100644 --- a/philo/sleep.c +++ b/philo/sleep.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */ -/* Updated: 2024/07/23 17:24:30 by adjoly ### ########.fr */ +/* Updated: 2024/07/25 16:08:44 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,11 +20,18 @@ uint32_t time_to_ms(void) return (time.tv_sec * 1000 + time.tv_usec / 1000); } -void sleep_phil(uint32_t sleep_time) +bool sleep_phil(uint32_t sleep_time, pthread_mutex_t *death) { uint32_t t0; t0 = time_to_ms() + sleep_time; while (time_to_ms() < t0) - usleep(100); + { + pthread_mutex_lock(death); + if (get_death(false, true) == true) + return (true); + pthread_mutex_unlock(death); + usleep(1000); + } + return (false); } diff --git a/philo/start_philo.c b/philo/start_philo.c new file mode 100644 index 0000000..251099e --- /dev/null +++ b/philo/start_philo.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* start_philo.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/24 18:19:14 by adjoly #+# #+# */ +/* Updated: 2024/07/24 23:01:29 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +void start_philo(t_init *init, uint16_t philo_nbr) +{ + uint16_t i; + int r; + + i = 0; + while (i < philo_nbr) + { + r = pthread_join(init[i].thread, NULL); + if (r != 0) + return ; + i++; + } +}