ok but fix todo

This commit is contained in:
Denis 2021-05-16 01:21:23 +03:00
parent ccb9943abb
commit dc72e9fd05
2 changed files with 11 additions and 10 deletions

View File

@ -14,9 +14,9 @@ void *run_trhead(t_philo *philo)
{ {
if (philo->all_struct->opt) if (philo->all_struct->opt)
{ {
if (philo->count_eat < philo->all_struct->number_of_times_each_philosopher_must_eat) if (philo->count_eat == philo->all_struct->number_of_times_each_philosopher_must_eat)
{ {
return(NULL); exit(0); // TODO реализовать завершение программы
} }
} }
cycle(philo); cycle(philo);
@ -38,8 +38,8 @@ void eating(t_philo *philo)
{ {
messages(philo->all_struct, philo, "eating"); messages(philo->all_struct, philo, "eating");
philo->last_eat = ft_get_time() - philo->all_struct->start_time; philo->last_eat = ft_get_time() - philo->all_struct->start_time;
ft_msleep(philo->all_struct->time_to_eat);
philo->count_eat++; philo->count_eat++;
my_usleep(philo->all_struct->time_to_eat);
} }
void sleeping(t_philo *philo) void sleeping(t_philo *philo)
{ {
@ -48,11 +48,12 @@ void sleeping(t_philo *philo)
messages(philo->all_struct, philo, "sleeping"); messages(philo->all_struct, philo, "sleeping");
pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]); pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]);
pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]); pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]);
my_usleep(philo->all_struct->time_to_sleep); ft_msleep(philo->all_struct->time_to_sleep);
} }
void cycle(t_philo *philo) void cycle(t_philo *philo)
{ {
messages(philo->all_struct, philo, "thinking");
take_forks(philo); take_forks(philo);
eating(philo); eating(philo);
sleeping(philo); sleeping(philo);
@ -62,18 +63,17 @@ int run_observer(t_all *all)
{ {
int i; int i;
i = 0; i = 0;
time_t time; time_t time_eat;
while(1) while(1)
{ {
i = 0; i = 0;
while(i <= all->number_of_philosophers) while(i <= all->number_of_philosophers)
{ {
time = (ft_get_time() - all->start_time) - all->data_philo->philors[i].last_eat; time_eat = (ft_get_time() - all->start_time) - all->data_philo->philors[i].last_eat;
if (time > all->time_to_die) // TODO пофиксить смерть if (time_eat > all->time_to_die) // TODO пофиксить смерть
{ {
printf("%ld---------------симуляция окончена фил %d умер ---------------\n", ft_get_time()- all->start_time, i + 1); printf("%ld---------------симуляция окончена фил %d умер ---------------\n", ft_get_time() - all->start_time, i + 1);
// ft_exit(all); return(2);
return(0);
} }
i++; i++;
} }

View File

@ -48,6 +48,7 @@ int parse_param(t_all *all, int optional, char **argv)
all->time_to_die = ft_atoi(argv[2]); all->time_to_die = ft_atoi(argv[2]);
all->time_to_eat = ft_atoi(argv[3]); all->time_to_eat = ft_atoi(argv[3]);
all->time_to_sleep = ft_atoi(argv[4]); all->time_to_sleep = ft_atoi(argv[4]);
all->number_of_times_each_philosopher_must_eat = 0;
all->opt = 0; all->opt = 0;
return (check_param(all, 0)); return (check_param(all, 0));
} }