diff --git a/philo_one/srcs/parser.c b/philo_one/srcs/parser.c index fd9fa19..55f6723 100644 --- a/philo_one/srcs/parser.c +++ b/philo_one/srcs/parser.c @@ -34,7 +34,7 @@ int parse_param(t_all *all, int optional, char **argv) all->smert = 0; if (optional) { - all->number_of_philosophers = ft_atoi(argv[1]) - 1; + all->number_of_philosophers = ft_atoi(argv[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]); @@ -44,7 +44,7 @@ int parse_param(t_all *all, int optional, char **argv) } else { - all->number_of_philosophers = ft_atoi(argv[1]) - 1; + all->number_of_philosophers = ft_atoi(argv[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]); diff --git a/philo_one/srcs/prepare_simulation.c b/philo_one/srcs/prepare_simulation.c index 1bf6c13..0c6ca1b 100644 --- a/philo_one/srcs/prepare_simulation.c +++ b/philo_one/srcs/prepare_simulation.c @@ -82,7 +82,7 @@ void run_all_phils(t_all *all) int i; i = 0; void *philo; - while(i < all->number_of_philosophers) + while(i <= all->number_of_philosophers) { philo = (void*)(&all->data_philo->philors[i]); if (pthread_create(&all->data_philo->philors[i].thread, NULL, (void*)run_trhead, philo) == -1) diff --git a/philo_two/srcs/prepare_simulation.c b/philo_two/srcs/prepare_simulation.c deleted file mode 100644 index 9ce130a..0000000 --- a/philo_two/srcs/prepare_simulation.c +++ /dev/null @@ -1,86 +0,0 @@ -#include "../includes/philo_header.h" - -int check_all_run(t_all *all) -{ - int i; - - i = 0; - while (i < all->number_of_philosophers) - { - if (all->data_philo->philors[i].worked) - { - i++; - } - } - // printf("все запустились !\n"); - return (1); -} - - -void run_simulation(t_all *all) -{ - int i; - - i = 0; - while (i < all->number_of_philosophers) - { - if (!all->data_philo->philors[i].can_run_simulation) - { - all->data_philo->philors[i].can_run_simulation = 1; - i++; - } - } -} - -void get_data_for_philo(t_all *all) -{ - int i; - - i = 0; - - while(i < all->number_of_philosophers) - { - all->data_philo->philors[i].name = sem_set_name("philo", i); - all->data_philo->philors[i].eat_sem = reopen_sem(all->data_philo->philors[i].name, 1); - if (all->data_philo->philors[i].eat_sem == SEM_FAILED) - exit(0); // TODO fix this - all->data_philo->philors[i].died = 0; - all->data_philo->philors[i].nbr_philo = i; - all->data_philo->philors[i].worked = 0; - all->data_philo->philors[i].can_run_simulation = 0; - all->data_philo->philors[i].all_struct = all; - all->data_philo->philors[i].is_eat = 0; - all->data_philo->philors[i].last_eat = 0; - i++; - } -} - -int prepare_resources(t_all *all) -{ - all->data_philo = ft_calloc(sizeof(t_philo_data), 1); - all->data_philo->sems = reopen_sem(_SEM_NAME_FORKS, all->number_of_philosophers); - all->write = reopen_sem(_SEM_WRITE_LOCK, 1); - if (all->data_philo->sems == SEM_FAILED) - return(1); // TODO выход из программы сделать - if (all->write == SEM_FAILED) - return(1); - all->data_philo->philors = (t_philo *)ft_calloc(all->number_of_philosophers, sizeof(t_philo)); - get_data_for_philo(all); - return (0); -} - -void run_all_phils(t_all *all) -{ - int i; - i = 0; - void *philo; - while(i < all->number_of_philosophers) - { - philo = (void*)(&all->data_philo->philors[i]); - if (pthread_create(&all->data_philo->philors[i].thread, NULL, (void*)run_trhead, philo) == -1) - printf("I can't create a thread t%d\n", i); - else - pthread_detach(all->data_philo->philors[i].thread); - i++; - } -}