From 0bfc8e498fe20187b7951d5de4beb775faff7f4d Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 26 Mar 2024 09:13:48 +0100 Subject: [PATCH] Create outline of the solution --- philo/main.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ philo/philo.h | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 philo/main.c create mode 100644 philo/philo.h diff --git a/philo/main.c b/philo/main.c new file mode 100644 index 0000000..ee3eba9 --- /dev/null +++ b/philo/main.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */ +/* Updated: 2024/03/26 09:13:38 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" +#include +#include +#include + +int parse_input(t_settings *settings, int argc, char **argv) +{ + if (argc != 5 && argc != 6) + return (1); + settings->end = 0; + settings->should_check_hunger = 0; + if (parse_arg(&settings->philo_count, argv[1]) + || parse_arg(&settings->time_to_die, argv[2]) + || parse_arg(&settings->time_to_eat, argv[3]) + || parse_arg(&settings->time_to_sleep, argv[4])) + { + return (1); + } + if (argc == 6) + { + if (parse_arg(&settings->min_eat_num, argv[5])) + return (1); + settings->should_check_hunger = 1; + } + return (0); +} + +int main(int argc, char **argv) +{ + t_diner diner; + pthread_t *threads; + + if (parse_input(&diner.setting, argc, argv)) + { + return (1); + } + if (init(&diner, threads)) + { + cleanup(&diner, threads); + return (2); + } + seat_philosophers(&diner, threads); + wait_till_death_possible(diner.setting.time_to_die, diner.setting.start); + report_end(watch_philosophers(&diner)); + cleanup(&diner, threads); + return (0); +} diff --git a/philo/philo.h b/philo/philo.h new file mode 100644 index 0000000..a618cd5 --- /dev/null +++ b/philo/philo.h @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */ +/* Updated: 2024/03/26 09:13:38 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PHILO_H +# define PHILO_H + +# include +# include +# include + +typedef struct s_settings +{ + int end; + int should_check_hunger; + size_t philo_count; + suseconds_t time_to_die; + suseconds_t time_to_eat; + suseconds_t time_to_sleep; + suseconds_t start; + unsigned int min_eat_num; + pthread_mutex_t terminal_lock; + pthread_mutex_t end_lock; +} t_settings; + +typedef struct s_philosopher +{ + int num_eaten; + size_t id; + suseconds_t last_eaten; + t_settings *settings; + pthread_mutex_t *forks[2]; + pthread_mutex_t philo_lock; +} t_philo; + +typedef struct s_diner +{ + t_settings setting; + t_philo *philos; + pthread_mutex_t *forks; +} t_diner; + +#endif //PHILO_H -- 2.30.2