From 62373dc04bb35fb9bc56edebc7dcffb152a6389b Mon Sep 17 00:00:00 2001 From: Matthos Denys Date: Thu, 20 May 2021 13:37:08 +0300 Subject: [PATCH] max working --- philo_one/srcs/logic.c | 2 +- philo_two/srcs/ft_exit.c | 3 ++- philo_two/srcs/logic.c | 15 ++++++++------ philo_two/srcs/main.c | 4 +--- philo_two/srcs/messages.c | 11 ++++------ philo_two/srcs/observer.c | 19 ++++++++--------- philo_two/srcs/prepare_simulation.c | 32 +---------------------------- 7 files changed, 27 insertions(+), 59 deletions(-) diff --git a/philo_one/srcs/logic.c b/philo_one/srcs/logic.c index 894e72f..21488a6 100644 --- a/philo_one/srcs/logic.c +++ b/philo_one/srcs/logic.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/18 21:18:08 by mdenys #+# #+# */ -/* Updated: 2021/05/18 21:18:08 by mdenys ### ########.fr */ +/* Updated: 2021/05/20 12:58:27 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo_two/srcs/ft_exit.c b/philo_two/srcs/ft_exit.c index c911cd4..cd7e9c4 100644 --- a/philo_two/srcs/ft_exit.c +++ b/philo_two/srcs/ft_exit.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:16:23 by mdenys #+# #+# */ -/* Updated: 2021/05/20 12:23:12 by mdenys ### ########.fr */ +/* Updated: 2021/05/20 13:21:37 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ int ft_exit(t_all *all, int ret) { sem_close(all->data_philo->philors[i].eat_sem); sem_close(&all->data_philo->sems[i]); + free(all->data_philo->philors[i].name); i++; } free(all->data_philo->philors); diff --git a/philo_two/srcs/logic.c b/philo_two/srcs/logic.c index ab461ce..bf34215 100644 --- a/philo_two/srcs/logic.c +++ b/philo_two/srcs/logic.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */ -/* Updated: 2021/05/20 12:12:44 by mdenys ### ########.fr */ +/* Updated: 2021/05/20 13:36:21 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,12 +14,15 @@ void *run_trhead(t_philo *philo) { - while (philo->all_struct->smert != 42) + while (1) { if (philo->all_struct->opt) { if (philo->count_eat == philo->all_struct->etarate_meal) + { philo->all_struct->smert = 42; + break ; + } } cycle(philo); } @@ -43,12 +46,10 @@ void eating(t_philo *philo) all = philo->all_struct; sem_wait(philo->eat_sem); - messages(philo->all_struct, philo, "eating"); - philo->last_eat = ft_get_time() - philo->all_struct->start_time; philo->count_eat++; + philo->last_eat = ft_get_time() - philo->all_struct->start_time; + messages(philo->all_struct, philo, "eating"); ft_msleep(philo->all_struct->time_to_eat); - sem_post(all->data_philo->sems); - sem_post(all->data_philo->sems); sem_post(philo->eat_sem); } @@ -58,6 +59,8 @@ void sleeping(t_philo *philo) all = philo->all_struct; messages(philo->all_struct, philo, "sleeping"); + sem_post(all->data_philo->sems); + sem_post(all->data_philo->sems); ft_msleep(philo->all_struct->time_to_sleep); } diff --git a/philo_two/srcs/main.c b/philo_two/srcs/main.c index 419334a..cf93f7d 100644 --- a/philo_two/srcs/main.c +++ b/philo_two/srcs/main.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:46 by mdenys #+# #+# */ -/* Updated: 2021/05/20 12:12:46 by mdenys ### ########.fr */ +/* Updated: 2021/05/20 13:26:16 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,6 @@ int main(int argc, char **argv) if (parse_and_check_values(argv, argc) == 2) return (0); else - { return (1); - } return (0); } diff --git a/philo_two/srcs/messages.c b/philo_two/srcs/messages.c index 4d9baf8..6388779 100644 --- a/philo_two/srcs/messages.c +++ b/philo_two/srcs/messages.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:48 by mdenys #+# #+# */ -/* Updated: 2021/05/20 12:12:49 by mdenys ### ########.fr */ +/* Updated: 2021/05/20 13:08:58 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,10 +17,7 @@ void messages(t_all *all, t_philo *philo, char *str) time_t timestemp; timestemp = ft_get_time() - all->start_time; - if (philo->all_struct->smert != 42) - { - sem_wait(all->write); - printf("%ld %d %s\n", timestemp, philo->nbr_philo + 1, str); - sem_post(all->write); - } + sem_wait(all->write); + printf("%ld %d %s\n", timestemp, philo->nbr_philo + 1, str); + sem_post(all->write); } diff --git a/philo_two/srcs/observer.c b/philo_two/srcs/observer.c index 860310d..d271846 100644 --- a/philo_two/srcs/observer.c +++ b/philo_two/srcs/observer.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:50 by mdenys #+# #+# */ -/* Updated: 2021/05/20 12:22:45 by mdenys ### ########.fr */ +/* Updated: 2021/05/20 13:27:11 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ int run_observer_return(t_all *all, int i) { + sem_wait(all->write); printf("%ld %d died\n", \ ft_get_time() - all->start_time, i + 1); all->smert = 42; @@ -31,18 +32,16 @@ int run_observer(t_all *all) i = 0; while (i < all->nbr_phils) { - if (all->smert != 42) - { - time_eat = (ft_get_time() - \ - all->start_time) - all->data_philo->philors[i].last_eat; - if (time_eat > all->time_to_die) - return (run_observer_return(all, i)); - } - else + sem_wait(all->data_philo->philors[i].eat_sem); + time_eat = (ft_get_time() - \ + all->start_time) - all->data_philo->philors[i].last_eat; + if (time_eat > all->time_to_die) + return (run_observer_return(all, i)); + sem_post(all->data_philo->philors[i].eat_sem); + if (all->smert == 42) return (ft_exit(all, 2)); i++; } - ft_msleep(1); } return (0); } diff --git a/philo_two/srcs/prepare_simulation.c b/philo_two/srcs/prepare_simulation.c index 87a4dff..66b94eb 100644 --- a/philo_two/srcs/prepare_simulation.c +++ b/philo_two/srcs/prepare_simulation.c @@ -6,42 +6,12 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:58 by mdenys #+# #+# */ -/* Updated: 2021/05/20 12:12:59 by mdenys ### ########.fr */ +/* Updated: 2021/05/20 13:21:04 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/philo_header.h" -int check_all_run(t_all *all) -{ - int i; - - i = 0; - while (i < all->nbr_phils) - { - if (all->data_philo->philors[i].worked) - { - i++; - } - } - return (1); -} - -void run_simulation(t_all *all) -{ - int i; - - i = 0; - while (i < all->nbr_phils) - { - if (!all->data_philo->philors[i].can_run_simulation) - { - all->data_philo->philors[i].can_run_simulation = 1; - i++; - } - } -} - void get_data_f_philo(t_all *all) { int i;