From ccb9943abbd9a85e1821337ffa08039ba226f003 Mon Sep 17 00:00:00 2001 From: Matthos Denys Date: Sat, 15 May 2021 22:06:30 +0300 Subject: [PATCH] after optinal --- philo_one/includes/philo_header.h | 7 +++-- philo_one/srcs/logic.c | 49 ++++++++++++------------------- philo_one/srcs/main.c | 5 ++-- philo_one/srcs/messages.c | 10 ++++--- philo_one/srcs/parser.c | 9 ++++-- philo_one/srcs/utilc.c | 22 ++++++++++++++ 6 files changed, 59 insertions(+), 43 deletions(-) diff --git a/philo_one/includes/philo_header.h b/philo_one/includes/philo_header.h index 9a8d96b..a57f609 100644 --- a/philo_one/includes/philo_header.h +++ b/philo_one/includes/philo_header.h @@ -31,6 +31,8 @@ typedef struct s_all time_t start_time; t_philo_data *data_philo; pthread_mutex_t write; + int smert; + int opt; } t_all; @@ -48,7 +50,7 @@ typedef struct s_philo time_t last_eat; time_t limit; t_all *all_struct; - int is_eat; + int is_eat; pthread_mutex_t eat_mutex; } t_philo; @@ -63,7 +65,7 @@ t_all *structalloc_and_bzero(void); int parse_and_check_values(char **argv, int quantity); int ft_atoi(const char *str); int check_f(char ch); -int runner(t_all *all, int optional); +int runner(t_all *all); int return_big(int a, int b); int return_less(int a, int b); time_t ft_get_time(void); @@ -76,4 +78,5 @@ void messages(t_all *all, t_philo *philo, char *str); void cycle(t_philo *philo); void ft_msleep(time_t interval_ms); void my_usleep(long time); +void ft_exit(t_all *all); #endif diff --git a/philo_one/srcs/logic.c b/philo_one/srcs/logic.c index 6960a9e..35d54b7 100644 --- a/philo_one/srcs/logic.c +++ b/philo_one/srcs/logic.c @@ -10,10 +10,15 @@ void *run_trhead(t_philo *philo) } if (philo->can_run_simulation) { - // philo->last_eat = ft_get_time(); - // philo->limit = philo->last_eat + philo->all_struct->time_to_die; - while(philo->can_run_simulation) + while(philo->can_run_simulation && philo->all_struct->smert != 42) { + if (philo->all_struct->opt) + { + if (philo->count_eat < philo->all_struct->number_of_times_each_philosopher_must_eat) + { + return(NULL); + } + } cycle(philo); } } @@ -31,12 +36,10 @@ void take_forks(t_philo *philo) } void eating(t_philo *philo) { - // pthread_mutex_lock(&philo->eat_mutex); messages(philo->all_struct, philo, "eating"); philo->last_eat = ft_get_time() - philo->all_struct->start_time; philo->count_eat++; my_usleep(philo->all_struct->time_to_eat); - // pthread_mutex_unlock(&philo->eat_mutex); } void sleeping(t_philo *philo) { @@ -48,9 +51,6 @@ void sleeping(t_philo *philo) my_usleep(philo->all_struct->time_to_sleep); } - - - void cycle(t_philo *philo) { take_forks(philo); @@ -58,7 +58,6 @@ void cycle(t_philo *philo) sleeping(philo); } - int run_observer(t_all *all) { int i; @@ -67,46 +66,34 @@ int run_observer(t_all *all) while(1) { i = 0; - - while(i <= all->number_of_philosophers) { - - // pthread_mutex_lock(&all->data_philo->philors[i].eat_mutex); time = (ft_get_time() - all->start_time) - all->data_philo->philors[i].last_eat; - // printf("%ld test\n", time); if (time > all->time_to_die) // TODO пофиксить смерть { printf("%ld---------------симуляция окончена фил %d умер ---------------\n", ft_get_time()- all->start_time, i + 1); - exit(0); + // ft_exit(all); + return(0); } i++; - // pthread_mutex_unlock(&all->data_philo->philors[i].eat_mutex); } } return (0); } -int runner(t_all *all, int optional) +int runner(t_all *all) { - if (optional) + prepare_resources(all); + run_all_phils(all); + if (check_all_run(all)) { - prepare_resources(all); - run_all_phils(all); - if (check_all_run(all)) - { - all->start_time = ft_get_time(); - run_simulation(all); - } - if (!run_observer(all)) - { - return(0); - } + all->start_time = ft_get_time(); + run_simulation(all); } - else + if (!run_observer(all)) { - return(1); + return(0); } return(1); } diff --git a/philo_one/srcs/main.c b/philo_one/srcs/main.c index 4a25c98..01b6bd3 100644 --- a/philo_one/srcs/main.c +++ b/philo_one/srcs/main.c @@ -2,11 +2,10 @@ int main(int argc, char **argv) { - if (parse_and_check_values(argv, argc)) - ; + if (parse_and_check_values(argv, argc) == 2) + return(0); else { - printf("exit because error\n"); return (1); } return(0); diff --git a/philo_one/srcs/messages.c b/philo_one/srcs/messages.c index b0e133e..ebffc98 100644 --- a/philo_one/srcs/messages.c +++ b/philo_one/srcs/messages.c @@ -3,8 +3,10 @@ void messages(t_all *all, t_philo *philo, char *str) { time_t timestemp = ft_get_time() - all->start_time; - - pthread_mutex_lock(&all->write); - printf("%ld %*s%d %s\n", timestemp, philo->nbr_philo * 30, "", philo->nbr_philo + 1, str); - pthread_mutex_unlock(&all->write); + if (philo->all_struct->smert != 42) + { + pthread_mutex_lock(&all->write); + printf("%ld %*s%d %s\n", timestemp, philo->nbr_philo * 30, "", philo->nbr_philo + 1, str); + pthread_mutex_unlock(&all->write); + } } diff --git a/philo_one/srcs/parser.c b/philo_one/srcs/parser.c index b39d025..24869e0 100644 --- a/philo_one/srcs/parser.c +++ b/philo_one/srcs/parser.c @@ -31,6 +31,7 @@ int check_param(t_all *all, int optional) int parse_param(t_all *all, int optional, char **argv) { + all->smert = 0; if (optional) { all->number_of_philosophers = ft_atoi(argv[1]) - 1; @@ -38,14 +39,16 @@ int parse_param(t_all *all, int optional, char **argv) all->time_to_eat = ft_atoi(argv[3]); all->time_to_sleep = ft_atoi(argv[4]); all->number_of_times_each_philosopher_must_eat = ft_atoi(argv[5]); + all->opt = 1; return (check_param(all, 1)); } else { - all->number_of_philosophers = ft_atoi(argv[1]); + all->number_of_philosophers = ft_atoi(argv[1]) - 1; all->time_to_die = ft_atoi(argv[2]); all->time_to_eat = ft_atoi(argv[3]); all->time_to_sleep = ft_atoi(argv[4]); + all->opt = 0; return (check_param(all, 0)); } } @@ -60,7 +63,7 @@ int parse_and_check_values(char **argv, int quantity) if (!parse_param(all, 0, argv)) { printf("ok standart run\n"); - if (runner(all, 0) == 2) // TODO это завершение симуляции + if (runner(all) == 2) // TODO это завершение симуляции return (2); return(1); } @@ -75,7 +78,7 @@ int parse_and_check_values(char **argv, int quantity) if (!parse_param(all, 1, argv)) { printf("ok run with optional\n"); - if (runner(all, 1) == 2) // TODO тоже пофиксить; + if (runner(all) == 2) // TODO тоже пофиксить; return (2); return(1); } diff --git a/philo_one/srcs/utilc.c b/philo_one/srcs/utilc.c index 1db2e38..4bb1033 100644 --- a/philo_one/srcs/utilc.c +++ b/philo_one/srcs/utilc.c @@ -125,3 +125,25 @@ void my_usleep(long time) while (get_time() - t < time) usleep(1); } + +void ft_exit(t_all *all) +{ + int i; + + i = 0; + all->smert = 42; + ft_msleep(200); + while (i <= all->number_of_philosophers) + { + pthread_mutex_destroy(&all->data_philo->philors[i].eat_mutex); + pthread_mutex_destroy(&all->data_philo->mutex[i]); + i++; + } + i = 0; + while (i < all->number_of_philosophers) + { + free(&all->data_philo->philors[i]); + i++; + } + +}