diff --git a/philo/include/philo.h b/philo/include/philo.h index 4b884b7..439f74e 100644 --- a/philo/include/philo.h +++ b/philo/include/philo.h @@ -6,26 +6,44 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 15:11:02 by adjoly #+# #+# */ -/* Updated: 2024/07/07 16:18:23 by adjoly ### ########.fr */ +/* Updated: 2024/07/09 12:04:14 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PHILO_H # define PHILO_H -# include # include # include +# include +# include -typedef enum s_philo_state +typedef enum s_pstate { DIED = -1, EAT, THINK, SLEEP, FORK_TAKEN -} t_philo_state; +} t_pstate; -void log_philo(uint32_t timestamp, uint16_t philo, t_philo_state type); +typedef struct s_pdata +{ + uint32_t die_time; + uint32_t sleep_time; + uint32_t eat_time; + uint16_t philo_nbr; + uint8_t meal_nbr; + bool error; +} t_pdata; + +void log_philo(uint32_t timestamp, uint16_t philo, t_pstate type); +t_pdata philo_parse(char **argv, int ac); +uint16_t get_time_in_ms(struct timeval t0, struct timeval t1); + +/** + * For debug purpose to be REMOVED + */ +void print_philo_data(t_pdata data); #endif diff --git a/philo/include/utils.h b/philo/include/utils.h new file mode 100644 index 0000000..751aafa --- /dev/null +++ b/philo/include/utils.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/08 17:46:19 by adjoly #+# #+# */ +/* Updated: 2024/07/08 17:46:50 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef UTILS_H +# define UTILS_H + +long long ft_atoll(const char *nptr); + +#endif diff --git a/philo/philo b/philo/philo deleted file mode 100755 index 6c0685f..0000000 Binary files a/philo/philo and /dev/null differ diff --git a/philo/src/ft_atoll.c b/philo/src/ft_atoll.c new file mode 100644 index 0000000..a724d16 --- /dev/null +++ b/philo/src/ft_atoll.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoll.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/08 17:29:13 by adjoly #+# #+# */ +/* Updated: 2024/07/08 17:29:15 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +long long ft_atoll(const char *nptr) +{ + char sign; + long long nbr; + + sign = 1; + nbr = 0; + while ((*nptr >= 7 && *nptr <= 13) || *nptr == 32) + nptr++; + if (*nptr == '-') + { + sign *= -1; + nptr++; + } + else if (*nptr == '+') + nptr++; + while (*nptr >= '0' && *nptr <= '9') + { + nbr = nbr * 10 + (*nptr - '0'); + nptr++; + } + return (nbr * sign); +} diff --git a/philo/src/get_time_in_ms.c b/philo/src/get_time_in_ms.c new file mode 100644 index 0000000..8787563 --- /dev/null +++ b/philo/src/get_time_in_ms.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_time_in_ms.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/09 12:02:24 by adjoly #+# #+# */ +/* Updated: 2024/07/09 13:14:50 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +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); +} diff --git a/philo/src/log.c b/philo/src/log.c index 869da9b..b6faa42 100644 --- a/philo/src/log.c +++ b/philo/src/log.c @@ -6,14 +6,14 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */ -/* Updated: 2024/07/07 17:56:28 by adjoly ### ########.fr */ +/* Updated: 2024/07/08 16:22:28 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" #include "philo_msg.h" -void log_philo(uint32_t timestamp, uint16_t philo, t_philo_state type) +void log_philo(uint32_t timestamp, uint16_t philo, t_pstate type) { if (type == EAT) printf("%u %hu %s", timestamp, philo, EATING_MSG); diff --git a/philo/src/parse_argv.c b/philo/src/parse_argv.c new file mode 100644 index 0000000..c2d2c48 --- /dev/null +++ b/philo/src/parse_argv.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_argv.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/08 15:30:46 by adjoly #+# #+# */ +/* Updated: 2024/07/09 10:46:47 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" +#include "utils.h" + +t_pdata fill_pdata(char **av) +{ + t_pdata data; + + if (!av) + { + data.error = true; + return (data); + } + data.philo_nbr = ft_atoll(av[1]); + data.die_time = ft_atoll(av[2]); + data.eat_time = ft_atoll(av[3]); + data.sleep_time = ft_atoll(av[4]); + if (av[5]) + data.meal_nbr = ft_atoll(av[5]); + data.error = false; + return (data); +} + +t_pdata philo_parse(char **av, int ac) +{ + if (!(ac > 4 && ac <= 6)) + return (fill_pdata(NULL)); + return (fill_pdata(av)); +} diff --git a/philo/src/philo.c b/philo/src/philo.c index 2640641..c567c33 100644 --- a/philo/src/philo.c +++ b/philo/src/philo.c @@ -6,15 +6,28 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 15:10:29 by adjoly #+# #+# */ -/* Updated: 2024/07/07 17:44:08 by adjoly ### ########.fr */ +/* Updated: 2024/07/09 13:17:55 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" +#include +#include +#include +#include +#include "utils.h" int main(int ac, char **av) { - (void)ac; - (void)av; - log_philo(10, 5, DIED); + 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); } diff --git a/philo/src/print_philo_data.c b/philo/src/print_philo_data.c new file mode 100644 index 0000000..d91c410 --- /dev/null +++ b/philo/src/print_philo_data.c @@ -0,0 +1,18 @@ +#include "philo.h" +#include + +void print_philo_data(t_pdata data) +{ + if (data.error == true) + { + printf("error = true\n"); + return ; + } + else + printf("error = false\n"); + printf("die_time = %u\n", data.die_time); + printf("sleep_time = %u\n", data.sleep_time); + printf("eat_time = %u\n", data.eat_time); + printf("philo_nbr = %u\n", data.philo_nbr); + printf("meal_nbr = %u\n", data.meal_nbr); +}