From b706482b266e4ad17b27c4c42ed65030274ececc Mon Sep 17 00:00:00 2001 From: Adam Joly Date: Sun, 21 Jul 2024 20:55:35 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20testing=20things,=20might=20broke.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- philo/Makefile | 6 ++-- ...hilo_data.c => __debug_print_philo_data.c} | 0 philo/{src => }/ft_atoll.c | 0 philo/{src => }/get_time_in_ms.c | 7 ++-- philo/init_fork.c | 27 +++++++++++++++ philo/init_philo.c | 34 +++++++++++++++++++ philo/{src => }/log.c | 29 +++++++++------- philo/{src/parse_argv.c => parsing.c} | 0 philo/{src => }/philo.c | 2 +- philo/{include => }/philo.h | 34 ++++++++++++++++++- philo/{include => }/philo_msg.h | 0 philo/{include/utils.h => philo_routine.c} | 17 +++++----- philo/start_philo.c | 26 ++++++++++++++ 13 files changed, 155 insertions(+), 27 deletions(-) rename philo/{src/print_philo_data.c => __debug_print_philo_data.c} (100%) rename philo/{src => }/ft_atoll.c (100%) rename philo/{src => }/get_time_in_ms.c (86%) create mode 100644 philo/init_fork.c create mode 100644 philo/init_philo.c rename philo/{src => }/log.c (56%) rename philo/{src/parse_argv.c => parsing.c} (100%) rename philo/{src => }/philo.c (95%) rename philo/{include => }/philo.h (70%) rename philo/{include => }/philo_msg.h (100%) rename philo/{include/utils.h => philo_routine.c} (71%) create mode 100644 philo/start_philo.c diff --git a/philo/Makefile b/philo/Makefile index 83b5855..a2a9c1e 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/07/07 15:08:52 by adjoly #+# #+# # -# Updated: 2024/07/07 15:24:47 by adjoly ### ########.fr # +# Updated: 2024/07/16 20:03:49 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -18,9 +18,9 @@ CC = cc OBJSDIR = obj/ -SRC = $(shell find src -name '*.c') +SRC = $(shell find . -name '*.c') -I_DIR = include/ +I_DIR = ./ INCLUDE = -I $(I_DIR) diff --git a/philo/src/print_philo_data.c b/philo/__debug_print_philo_data.c similarity index 100% rename from philo/src/print_philo_data.c rename to philo/__debug_print_philo_data.c diff --git a/philo/src/ft_atoll.c b/philo/ft_atoll.c similarity index 100% rename from philo/src/ft_atoll.c rename to philo/ft_atoll.c diff --git a/philo/src/get_time_in_ms.c b/philo/get_time_in_ms.c similarity index 86% rename from philo/src/get_time_in_ms.c rename to philo/get_time_in_ms.c index 8787563..02bcc85 100644 --- a/philo/src/get_time_in_ms.c +++ b/philo/get_time_in_ms.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/09 12:02:24 by adjoly #+# #+# */ -/* Updated: 2024/07/09 13:14:50 by adjoly ### ########.fr */ +/* Updated: 2024/07/16 21:12:05 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,5 +15,8 @@ uint16_t get_time_in_ms(struct timeval t0, struct timeval t1) { - return (((t1.tv_sec-t0.tv_sec) * 1000000 + t1.tv_usec-t0.tv_usec)/1000); + int diff; + + diff = t1.tv_sec - t0.tv_sec; + return (((diff) * 1000000 + diff) / 1000); } diff --git a/philo/init_fork.c b/philo/init_fork.c new file mode 100644 index 0000000..c69bf17 --- /dev/null +++ b/philo/init_fork.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init_fork.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/16 19:53:01 by adjoly #+# #+# */ +/* Updated: 2024/07/18 21:03:00 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void init_fork(t_philo *philo, t_pdata data) +{ + int i; + + i = 0; + while (philo[i] && i <= data.philo_nbr) + { + pthread_mutex_init(philo[i].fork.left); + if (i == data.philo_nbr) + philo[i].fork.right = &philo[0].fork.left; + else + philo[i].fork.right = &philo[i + 1].fork.left; + i++; + } +} diff --git a/philo/init_philo.c b/philo/init_philo.c new file mode 100644 index 0000000..35be015 --- /dev/null +++ b/philo/init_philo.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init_philo.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/11 14:36:59 by adjoly #+# #+# */ +/* Updated: 2024/07/17 22:53:48 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +void init_philo(t_pdata data) +{ + t_philo philo[PHILO_MAX]; + t_philo *tmp; + pthread_mutex_t *print; + int ret; + + tmp = philo; + i = 0; + init_fork(philo, data); + while (tmp && i < data->philo_nbr) + { + ret = pthread_create(&(tmp->thread), NULL, philo_routine, tmp); + if (ret != 0) + return ; + tmp->data = data; + tmp->state = EAT; + tmp++; + } +} diff --git a/philo/src/log.c b/philo/log.c similarity index 56% rename from philo/src/log.c rename to philo/log.c index b6faa42..cdd711d 100644 --- a/philo/src/log.c +++ b/philo/log.c @@ -6,23 +6,28 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */ -/* Updated: 2024/07/08 16:22:28 by adjoly ### ########.fr */ +/* Updated: 2024/07/16 20:24:43 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" #include "philo_msg.h" +#include -void log_philo(uint32_t timestamp, uint16_t philo, t_pstate type) +void log_philo(uint32_t timestamp, t_philo philo) { - if (type == EAT) - printf("%u %hu %s", timestamp, philo, EATING_MSG); - else if (type == THINK) - printf("%u %hu %s", timestamp, philo, THINK_MSG); - else if (type == SLEEP) - printf("%u %hu %s", timestamp, philo, SLEEP_MSG); - else if (type == DIED) - printf("%u %hu %s", timestamp, philo, DIED_MSG); - else if (type == FORK_TAKEN) - printf("%u %hu %s", timestamp, philo, FORK_MSG); + static pthread_mutex_t print = {0}; + + pthread_mutex_lock(&print); + if (philo->state == EAT) + printf("%u %hu %s", timestamp, philo->nbr, EATING_MSG); + else if (philo->state == THINK) + printf("%u %hu %s", timestamp, philo->nbr, 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); + else if (philo->state == FORK_TAKEN) + printf("%u %hu %s", timestamp, philo->nbr, FORK_MSG); + pthread_mutex_unlock(&print); } diff --git a/philo/src/parse_argv.c b/philo/parsing.c similarity index 100% rename from philo/src/parse_argv.c rename to philo/parsing.c diff --git a/philo/src/philo.c b/philo/philo.c similarity index 95% rename from philo/src/philo.c rename to philo/philo.c index c567c33..950f53d 100644 --- a/philo/src/philo.c +++ b/philo/philo.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 15:10:29 by adjoly #+# #+# */ -/* Updated: 2024/07/09 13:17:55 by adjoly ### ########.fr */ +/* Updated: 2024/07/10 18:47:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo/include/philo.h b/philo/philo.h similarity index 70% rename from philo/include/philo.h rename to philo/philo.h index 439f74e..967eadc 100644 --- a/philo/include/philo.h +++ b/philo/philo.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 15:11:02 by adjoly #+# #+# */ -/* Updated: 2024/07/09 12:04:14 by adjoly ### ########.fr */ +/* Updated: 2024/07/18 18:30:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,9 @@ # include # include # include +# include + +# define PHILO_MAX 200 typedef enum s_pstate { @@ -37,10 +40,39 @@ typedef struct s_pdata bool error; } t_pdata; +typedef struct s_fork +{ + pthread_mutex_t left; + pthread_mutex_t *right; +} t_fork; + +typedef struct s_philo +{ + uint16_t nbr; + struct timeval t0; + t_fork fork; + t_pstate state; + t_pdata data; + pthread_t thread; +} t_philo; + +/** + * Utils + */ void log_philo(uint32_t timestamp, uint16_t philo, t_pstate type); 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 *philo_routine(void *param) +/** + * Main path + * by order of call + */ +t_philo *init_philo(t_pdata data, uint16_t philo_nbr); +void init_fork(t_philo *philo, t_pdata data); +void start_philo(t_philo *philo, t_pdata data); + /** * For debug purpose to be REMOVED */ diff --git a/philo/include/philo_msg.h b/philo/philo_msg.h similarity index 100% rename from philo/include/philo_msg.h rename to philo/philo_msg.h diff --git a/philo/include/utils.h b/philo/philo_routine.c similarity index 71% rename from philo/include/utils.h rename to philo/philo_routine.c index 751aafa..149b197 100644 --- a/philo/include/utils.h +++ b/philo/philo_routine.c @@ -1,18 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* utils.h :+: :+: :+: */ +/* philo_routine.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/07/08 17:46:19 by adjoly #+# #+# */ -/* Updated: 2024/07/08 17:46:50 by adjoly ### ########.fr */ +/* Created: 2024/07/16 19:54:02 by adjoly #+# #+# */ +/* Updated: 2024/07/16 20:57:54 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef UTILS_H -# define UTILS_H +void *philo_routine(void *param) +{ + t_philo philo; -long long ft_atoll(const char *nptr); - -#endif + philo = (t_philo)param; + log +} diff --git a/philo/start_philo.c b/philo/start_philo.c new file mode 100644 index 0000000..fdb2355 --- /dev/null +++ b/philo/start_philo.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* start_philo.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/18 18:31:21 by adjoly #+# #+# */ +/* Updated: 2024/07/18 19:08:43 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void start_philo(t_philo *philo, t_pdata data) +{ + int i; + int ret; + + i = 0; + while (philo[i] && i < data.philo_nbr) + { + ret = pthread_join(&(philo->thread), NULL); + if (ret != 0) + return ; + i++; + } +}