after optinal
This commit is contained in:
parent
797bb0d3bc
commit
ccb9943abb
@ -31,6 +31,8 @@ typedef struct s_all
|
|||||||
time_t start_time;
|
time_t start_time;
|
||||||
t_philo_data *data_philo;
|
t_philo_data *data_philo;
|
||||||
pthread_mutex_t write;
|
pthread_mutex_t write;
|
||||||
|
int smert;
|
||||||
|
int opt;
|
||||||
} t_all;
|
} t_all;
|
||||||
|
|
||||||
|
|
||||||
@ -48,7 +50,7 @@ typedef struct s_philo
|
|||||||
time_t last_eat;
|
time_t last_eat;
|
||||||
time_t limit;
|
time_t limit;
|
||||||
t_all *all_struct;
|
t_all *all_struct;
|
||||||
int is_eat;
|
int is_eat;
|
||||||
pthread_mutex_t eat_mutex;
|
pthread_mutex_t eat_mutex;
|
||||||
} t_philo;
|
} t_philo;
|
||||||
|
|
||||||
@ -63,7 +65,7 @@ t_all *structalloc_and_bzero(void);
|
|||||||
int parse_and_check_values(char **argv, int quantity);
|
int parse_and_check_values(char **argv, int quantity);
|
||||||
int ft_atoi(const char *str);
|
int ft_atoi(const char *str);
|
||||||
int check_f(char ch);
|
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_big(int a, int b);
|
||||||
int return_less(int a, int b);
|
int return_less(int a, int b);
|
||||||
time_t ft_get_time(void);
|
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 cycle(t_philo *philo);
|
||||||
void ft_msleep(time_t interval_ms);
|
void ft_msleep(time_t interval_ms);
|
||||||
void my_usleep(long time);
|
void my_usleep(long time);
|
||||||
|
void ft_exit(t_all *all);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -10,10 +10,15 @@ void *run_trhead(t_philo *philo)
|
|||||||
}
|
}
|
||||||
if (philo->can_run_simulation)
|
if (philo->can_run_simulation)
|
||||||
{
|
{
|
||||||
// philo->last_eat = ft_get_time();
|
while(philo->can_run_simulation && philo->all_struct->smert != 42)
|
||||||
// philo->limit = philo->last_eat + philo->all_struct->time_to_die;
|
|
||||||
while(philo->can_run_simulation)
|
|
||||||
{
|
{
|
||||||
|
if (philo->all_struct->opt)
|
||||||
|
{
|
||||||
|
if (philo->count_eat < philo->all_struct->number_of_times_each_philosopher_must_eat)
|
||||||
|
{
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
cycle(philo);
|
cycle(philo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -31,12 +36,10 @@ void take_forks(t_philo *philo)
|
|||||||
}
|
}
|
||||||
void eating(t_philo *philo)
|
void eating(t_philo *philo)
|
||||||
{
|
{
|
||||||
// pthread_mutex_lock(&philo->eat_mutex);
|
|
||||||
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;
|
||||||
philo->count_eat++;
|
philo->count_eat++;
|
||||||
my_usleep(philo->all_struct->time_to_eat);
|
my_usleep(philo->all_struct->time_to_eat);
|
||||||
// pthread_mutex_unlock(&philo->eat_mutex);
|
|
||||||
}
|
}
|
||||||
void sleeping(t_philo *philo)
|
void sleeping(t_philo *philo)
|
||||||
{
|
{
|
||||||
@ -48,9 +51,6 @@ void sleeping(t_philo *philo)
|
|||||||
my_usleep(philo->all_struct->time_to_sleep);
|
my_usleep(philo->all_struct->time_to_sleep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cycle(t_philo *philo)
|
void cycle(t_philo *philo)
|
||||||
{
|
{
|
||||||
take_forks(philo);
|
take_forks(philo);
|
||||||
@ -58,7 +58,6 @@ void cycle(t_philo *philo)
|
|||||||
sleeping(philo);
|
sleeping(philo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int run_observer(t_all *all)
|
int run_observer(t_all *all)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@ -67,46 +66,34 @@ int run_observer(t_all *all)
|
|||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
|
|
||||||
while(i <= all->number_of_philosophers)
|
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;
|
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 пофиксить смерть
|
if (time > 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);
|
||||||
exit(0);
|
// ft_exit(all);
|
||||||
|
return(0);
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
// pthread_mutex_unlock(&all->data_philo->philors[i].eat_mutex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (0);
|
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);
|
all->start_time = ft_get_time();
|
||||||
run_all_phils(all);
|
run_simulation(all);
|
||||||
if (check_all_run(all))
|
|
||||||
{
|
|
||||||
all->start_time = ft_get_time();
|
|
||||||
run_simulation(all);
|
|
||||||
}
|
|
||||||
if (!run_observer(all))
|
|
||||||
{
|
|
||||||
return(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
if (!run_observer(all))
|
||||||
{
|
{
|
||||||
return(1);
|
return(0);
|
||||||
}
|
}
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,11 +2,10 @@
|
|||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
if (parse_and_check_values(argv, argc))
|
if (parse_and_check_values(argv, argc) == 2)
|
||||||
;
|
return(0);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("exit because error\n");
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
return(0);
|
return(0);
|
||||||
|
|||||||
@ -3,8 +3,10 @@
|
|||||||
void messages(t_all *all, t_philo *philo, char *str)
|
void messages(t_all *all, t_philo *philo, char *str)
|
||||||
{
|
{
|
||||||
time_t timestemp = ft_get_time() - all->start_time;
|
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_lock(&all->write);
|
||||||
pthread_mutex_unlock(&all->write);
|
printf("%ld %*s%d %s\n", timestemp, philo->nbr_philo * 30, "", philo->nbr_philo + 1, str);
|
||||||
|
pthread_mutex_unlock(&all->write);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,6 +31,7 @@ int check_param(t_all *all, int optional)
|
|||||||
|
|
||||||
int parse_param(t_all *all, int optional, char **argv)
|
int parse_param(t_all *all, int optional, char **argv)
|
||||||
{
|
{
|
||||||
|
all->smert = 0;
|
||||||
if (optional)
|
if (optional)
|
||||||
{
|
{
|
||||||
all->number_of_philosophers = ft_atoi(argv[1]) - 1;
|
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_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 = ft_atoi(argv[5]);
|
all->number_of_times_each_philosopher_must_eat = ft_atoi(argv[5]);
|
||||||
|
all->opt = 1;
|
||||||
return (check_param(all, 1));
|
return (check_param(all, 1));
|
||||||
}
|
}
|
||||||
else
|
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_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->opt = 0;
|
||||||
return (check_param(all, 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))
|
if (!parse_param(all, 0, argv))
|
||||||
{
|
{
|
||||||
printf("ok standart run\n");
|
printf("ok standart run\n");
|
||||||
if (runner(all, 0) == 2) // TODO это завершение симуляции
|
if (runner(all) == 2) // TODO это завершение симуляции
|
||||||
return (2);
|
return (2);
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
@ -75,7 +78,7 @@ int parse_and_check_values(char **argv, int quantity)
|
|||||||
if (!parse_param(all, 1, argv))
|
if (!parse_param(all, 1, argv))
|
||||||
{
|
{
|
||||||
printf("ok run with optional\n");
|
printf("ok run with optional\n");
|
||||||
if (runner(all, 1) == 2) // TODO тоже пофиксить;
|
if (runner(all) == 2) // TODO тоже пофиксить;
|
||||||
return (2);
|
return (2);
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -125,3 +125,25 @@ void my_usleep(long time)
|
|||||||
while (get_time() - t < time)
|
while (get_time() - t < time)
|
||||||
usleep(1);
|
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++;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user