add obserber and create mini debug
This commit is contained in:
parent
c4e2e8bafd
commit
bc188d8c05
@ -12,10 +12,11 @@
|
|||||||
typedef struct s_philo
|
typedef struct s_philo
|
||||||
{
|
{
|
||||||
int more_fork;
|
int more_fork;
|
||||||
int less_fork; // TODO переделать на наименьшую и наибольшую
|
int less_fork;
|
||||||
pthread_t *thread;
|
pthread_t thread;
|
||||||
int count_eat;
|
int count_eat;
|
||||||
time_t time_to_die;
|
time_t time_to_die;
|
||||||
|
int died;
|
||||||
pthread_mutex_t *eat_mutex;
|
pthread_mutex_t *eat_mutex;
|
||||||
} t_philo;
|
} t_philo;
|
||||||
|
|
||||||
@ -32,6 +33,7 @@ typedef struct s_all
|
|||||||
time_t time_to_eat;
|
time_t time_to_eat;
|
||||||
time_t time_to_sleep;
|
time_t time_to_sleep;
|
||||||
int number_of_times_each_philosopher_must_eat;
|
int number_of_times_each_philosopher_must_eat;
|
||||||
|
time_t start_time;
|
||||||
t_philo_data *data_philo;
|
t_philo_data *data_philo;
|
||||||
} t_all;
|
} t_all;
|
||||||
|
|
||||||
@ -43,5 +45,5 @@ int check_f(char ch);
|
|||||||
int runner(t_all *all, int optional);
|
int runner(t_all *all, int optional);
|
||||||
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);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -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].more_fork = return_big(i, i + 1);
|
||||||
all->data_philo->philors[i].less_fork = return_less(i, i + 1);
|
all->data_philo->philors[i].less_fork = return_less(i, i + 1);
|
||||||
}
|
}
|
||||||
|
all->data_philo->philors[i].died = 0;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -73,35 +74,84 @@ int prepare_resources(t_all *all)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
time_t ft_get_time() {
|
void run_all_phils(t_all *all)
|
||||||
time_t return_value;
|
|
||||||
struct timeval current_time;
|
|
||||||
gettimeofday(¤t_time, NULL);
|
|
||||||
return_value = current_time.tv_sec * 1000 + current_time.tv_usec / 1000;
|
|
||||||
return (return_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void run_all_philor(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)
|
int runner(t_all *all, int optional)
|
||||||
{
|
{
|
||||||
if (optional)
|
if (optional)
|
||||||
{
|
{
|
||||||
prepare_resources(all);
|
prepare_resources(all);
|
||||||
run_all_philor(all);
|
run_all_phils(all);
|
||||||
// run_observer(all);
|
if (!run_observer(all))
|
||||||
return(0);
|
{
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
return(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,7 +60,8 @@ 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");
|
||||||
runner(all, 0);
|
if (runner(all, 0) == 2) // TODO это завершение симуляции
|
||||||
|
return (2);
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -74,7 +75,8 @@ 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");
|
||||||
runner(all, 1);
|
if (runner(all, 1) == 2) // TODO тоже пофиксить;
|
||||||
|
return (2);
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@ -66,6 +66,7 @@ t_all *structalloc_and_bzero(void)
|
|||||||
all->time_to_eat = -1;
|
all->time_to_eat = -1;
|
||||||
all->time_to_sleep = -1;
|
all->time_to_sleep = -1;
|
||||||
all->number_of_times_each_philosopher_must_eat = -1;
|
all->number_of_times_each_philosopher_must_eat = -1;
|
||||||
|
all->start_time = ft_get_time();
|
||||||
return(all);
|
return(all);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,3 +87,11 @@ int return_less(int a, int b)
|
|||||||
}
|
}
|
||||||
return (b);
|
return (b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time_t ft_get_time(void)
|
||||||
|
{
|
||||||
|
time_t return_value;
|
||||||
|
struct timeval current_time;
|
||||||
|
gettimeofday(¤t_time, NULL);
|
||||||
|
return (return_value = current_time.tv_sec * 1000 + current_time.tv_usec / 1000);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user