after optinal

This commit is contained in:
Matthos Denys 2021-05-15 22:06:30 +03:00
parent 797bb0d3bc
commit ccb9943abb
6 changed files with 59 additions and 43 deletions

View File

@ -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;
@ -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

View File

@ -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,31 +66,24 @@ 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))
@ -103,10 +95,5 @@ int runner(t_all *all, int optional)
{
return(0);
}
}
else
{
return(1);
}
return(1);
}

View File

@ -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);

View File

@ -3,8 +3,10 @@
void messages(t_all *all, t_philo *philo, char *str)
{
time_t timestemp = ft_get_time() - all->start_time;
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);
}
}

View File

@ -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);
}

View File

@ -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++;
}
}