158 lines
2.8 KiB
C
158 lines
2.8 KiB
C
#include "../includes/philo_header.h"
|
|
|
|
void *test(char *str)
|
|
{
|
|
printf("поток %s отработал\n", str);
|
|
return(NULL);
|
|
}
|
|
|
|
|
|
int func_test(t_all *all)
|
|
{
|
|
pthread_t t0;
|
|
pthread_t t1;
|
|
void *result;
|
|
(void)all;
|
|
|
|
if (pthread_create(&t0, NULL, (void*)test, "t0") == -1) // предпоследнее это функция и аргумент
|
|
{
|
|
printf("не могу создать поток t0\n");
|
|
}
|
|
if (pthread_create(&t1, NULL, (void*)test, "t1") == -1) // предпоследнее это функция и аргумент
|
|
{
|
|
printf("не могу создать поток t1\n");
|
|
}
|
|
|
|
if (pthread_join(t0, &result) == -1)
|
|
{
|
|
printf("не могу присоединиться к потоку t0\n");
|
|
}
|
|
if (pthread_join(t1, &result) == -1)
|
|
{
|
|
printf("не могу присоединиться к потоку t1\n");
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
void get_fork_for_philo(t_all *all)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
|
|
while(i <= all->number_of_philosophers)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
all->data_philo->philors[i].more_fork = all->number_of_philosophers;
|
|
all->data_philo->philors[i].less_fork = 0;
|
|
}
|
|
else
|
|
{
|
|
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++;
|
|
}
|
|
}
|
|
|
|
int prepare_resources(t_all *all)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
all->data_philo = ft_calloc(sizeof(t_philo_data), 1);
|
|
all->data_philo->mutex = (pthread_mutex_t *)ft_calloc(sizeof(pthread_mutex_t), all->number_of_philosophers);
|
|
while (i < all->number_of_philosophers)
|
|
{
|
|
pthread_mutex_init(&all->data_philo->mutex[i], NULL);
|
|
i++;
|
|
}
|
|
all->data_philo->philors = (t_philo *)ft_calloc(sizeof(t_philo), all->number_of_philosophers);
|
|
get_fork_for_philo(all);
|
|
return (0);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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_phils(all);
|
|
if (!run_observer(all))
|
|
{
|
|
return(0);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return(1);
|
|
}
|
|
return(1);
|
|
}
|