diff --git a/philo/init_fork.c b/philo/init_fork.c new file mode 100644 index 0000000..a96d9fc --- /dev/null +++ b/philo/init_fork.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init_fork.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/22 15:29:08 by adjoly #+# #+# */ +/* Updated: 2024/07/22 17:37:26 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +t_fork init_fork(t_init *init, uint16_t i) +{ + 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); +} diff --git a/philo/init_philo.c b/philo/init_philo.c index 1e4b19a..c9fb34a 100644 --- a/philo/init_philo.c +++ b/philo/init_philo.c @@ -6,30 +6,30 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 14:36:59 by adjoly #+# #+# */ -/* Updated: 2024/07/22 14:05:52 by adjoly ### ########.fr */ +/* Updated: 2024/07/23 17:53:14 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" +#include void init_philo(t_pdata data) { - t_init init[PHILO_MAX]; - pthread_mutex_t *print; - int ret; + t_init init[PHILO_MAX]; + int r; + size_t i; i = 0; - init_fork(philo, data); - while (i < data->philo_nbr) + while (i < data.philo_nbr) { - ret = pthread_create(&(init[i]->thread), NULL, philo_routine, tmp); - if (ret != 0) + init[i].data.nbr = 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); + if (r != 0) return ; - init[i]->philo->nbr = i; - gettimeofday(&(init[i]->philo->t0), NULL) - init[i]->philo->data = data; - init[i]->philo->state = EAT; - init[i]->philo->fork = init_fork(init, i, data->philo_nbr); i++; } } diff --git a/philo/log.c b/philo/log.c index 3aef0de..efc5cb6 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/21 21:31:44 by adjoly ### ########.fr */ +/* Updated: 2024/07/22 21:21:21 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,10 +14,12 @@ #include "philo_msg.h" #include -void log_philo(uint32_t timestamp, t_philo philo) +void log_philo(struct timeval t1, t_philo philo) { + uint32_t timestamp; static pthread_mutex_t print = {0}; + 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); diff --git a/philo/philo.c b/philo/philo.c index 950f53d..2b34ad9 100644 --- a/philo/philo.c +++ b/philo/philo.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 15:10:29 by adjoly #+# #+# */ -/* Updated: 2024/07/10 18:47:00 by adjoly ### ########.fr */ +/* Updated: 2024/07/23 17:42:16 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,19 +15,13 @@ #include #include #include -#include "utils.h" int main(int ac, char **av) { t_pdata data; - struct timeval t0; - struct timeval t1; - gettimeofday(&t0, NULL); data = philo_parse(av, ac); - gettimeofday(&t1, NULL); if (data.error == true) return (EXIT_FAILURE); - print_philo_data(data); - log_philo(get_time_in_ms(t0, t1), 0, DIED); + init_philo(data); } diff --git a/philo/philo.h b/philo/philo.h index 682fa8d..825dd9d 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/22 14:01:27 by adjoly ### ########.fr */ +/* Updated: 2024/07/23 17:23:38 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ # include # include # include +# include # define PHILO_MAX 200 @@ -64,16 +65,20 @@ typedef struct s_init /** * Utils */ -void log_philo(uint32_t timestamp, t_philo philo); 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); /** * Main path * by order of call */ -t_philo *init_philo(t_pdata data, uint16_t philo_nbr); +void init_philo(t_pdata data); +t_fork init_fork(t_init *init, uint16_t nbr); + +void *philo_routine(void *content); /** * For debug purpose to be REMOVED diff --git a/philo/philo_routine.c b/philo/philo_routine.c new file mode 100644 index 0000000..fac9a5c --- /dev/null +++ b/philo/philo_routine.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo_routine.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/22 21:24:53 by adjoly #+# #+# */ +/* Updated: 2024/07/23 18:07:56 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" +#include + +void *philo_routine(void *content) +{ + t_philo philo; +// struct timeval t1; + + 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); +// } + return (NULL); +} diff --git a/philo/sleep.c b/philo/sleep.c new file mode 100644 index 0000000..b98e6a0 --- /dev/null +++ b/philo/sleep.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sleep.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */ +/* Updated: 2024/07/23 17:24:30 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +uint32_t time_to_ms(void) +{ + struct timeval time; + + gettimeofday(&time, NULL); + return (time.tv_sec * 1000 + time.tv_usec / 1000); +} + +void sleep_phil(uint32_t sleep_time) +{ + uint32_t t0; + + t0 = time_to_ms() + sleep_time; + while (time_to_ms() < t0) + usleep(100); +}