add obserber and create mini debug

This commit is contained in:
Matthos Denys 2021-05-13 15:13:28 +03:00
parent c4e2e8bafd
commit bc188d8c05
4 changed files with 84 additions and 21 deletions

View File

@ -12,10 +12,11 @@
typedef struct s_philo
{
int more_fork;
int less_fork; // TODO переделать на наименьшую и наибольшую
pthread_t *thread;
int less_fork;
pthread_t thread;
int count_eat;
time_t time_to_die;
int died;
pthread_mutex_t *eat_mutex;
} t_philo;
@ -32,6 +33,7 @@ typedef struct s_all
time_t time_to_eat;
time_t time_to_sleep;
int number_of_times_each_philosopher_must_eat;
time_t start_time;
t_philo_data *data_philo;
} t_all;
@ -43,5 +45,5 @@ int check_f(char ch);
int runner(t_all *all, int optional);
int return_big(int a, int b);
int return_less(int a, int b);
time_t ft_get_time(void);
#endif

View File

@ -52,6 +52,7 @@ void get_fork_for_philo(t_all *all)
all->data_philo->philors[i].more_fork = return_big(i, i + 1);
all->data_philo->philors[i].less_fork = return_less(i, i + 1);
}
all->data_philo->philors[i].died = 0;
i++;
}
}
@ -73,35 +74,84 @@ int prepare_resources(t_all *all)
return (0);
}
time_t ft_get_time() {
time_t return_value;
struct timeval current_time;
gettimeofday(&current_time, NULL);
return_value = current_time.tv_sec * 1000 + current_time.tv_usec / 1000;
return (return_value);
}
void run_all_philor(t_all *all)
void run_all_phils(t_all *all)
{
int i;
i = 0;
char *str;
while(i <= all->number_of_philosophers)
{
if (i != 0)
{
free(str);
}
str = malloc(sizeof(char) * 80);
str[80] = 0;
sprintf(str, "t%d", i);
if (pthread_create(&all->data_philo->philors[i].thread, NULL, (void*)test, str) == -1)
{
printf("не могу создать поток t%d\n", i);
}
else
{
pthread_detach(all->data_philo->philors[i].thread);
}
i++;
}
if (i > 0)
{
free(str);
}
}
// void run_observer(t_all *all)
// {
int run_observer(t_all *all)
{
int i;
int died;
i = 0;
died = 0;
while(1)
{
i = 0;
while(i <= all->number_of_philosophers)
{
if (all->data_philo->philors[i].died == 0)
{
i++;
}
else
{
died = 42;
break;
}
}
if (died == 42)
{
printf("симуляция окончена фил умер\n");
break;
}
}
return (0);
}
// }
int runner(t_all *all, int optional)
{
if (optional)
{
prepare_resources(all);
run_all_philor(all);
// run_observer(all);
run_all_phils(all);
if (!run_observer(all))
{
return(0);
}
}
else
{
return(1);
}
return(1);
}

View File

@ -60,7 +60,8 @@ int parse_and_check_values(char **argv, int quantity)
if (!parse_param(all, 0, argv))
{
printf("ok standart run\n");
runner(all, 0);
if (runner(all, 0) == 2) // TODO это завершение симуляции
return (2);
return(1);
}
else
@ -74,7 +75,8 @@ int parse_and_check_values(char **argv, int quantity)
if (!parse_param(all, 1, argv))
{
printf("ok run with optional\n");
runner(all, 1);
if (runner(all, 1) == 2) // TODO тоже пофиксить;
return (2);
return(1);
}
else

View File

@ -66,6 +66,7 @@ t_all *structalloc_and_bzero(void)
all->time_to_eat = -1;
all->time_to_sleep = -1;
all->number_of_times_each_philosopher_must_eat = -1;
all->start_time = ft_get_time();
return(all);
}
@ -86,3 +87,11 @@ int return_less(int a, int b)
}
return (b);
}
time_t ft_get_time(void)
{
time_t return_value;
struct timeval current_time;
gettimeofday(&current_time, NULL);
return (return_value = current_time.tv_sec * 1000 + current_time.tv_usec / 1000);
}