99 lines
1.9 KiB
C
99 lines
1.9 KiB
C
#include "../includes/philo_header.h"
|
|
|
|
int check_all_run(t_all *all)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (i < all->number_of_philosophers)
|
|
{
|
|
if (all->data_philo->philors[i].worked)
|
|
{
|
|
i++;
|
|
}
|
|
}
|
|
printf("все запустились !\n");
|
|
return (1);
|
|
}
|
|
|
|
|
|
void run_simulation(t_all *all)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (i < all->number_of_philosophers)
|
|
{
|
|
if (!all->data_philo->philors[i].can_run_simulation)
|
|
{
|
|
all->data_philo->philors[i].can_run_simulation = 1;
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
|
|
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].less_fork = 0;
|
|
all->data_philo->philors[i].more_fork = all->number_of_philosophers - 1;
|
|
}
|
|
else
|
|
{
|
|
all->data_philo->philors[i].less_fork = i - 1;
|
|
all->data_philo->philors[i].more_fork = i;
|
|
}
|
|
all->data_philo->philors[i].died = 0;
|
|
all->data_philo->philors[i].nbr_philo = i;
|
|
all->data_philo->philors[i].worked = 0;
|
|
all->data_philo->philors[i].can_run_simulation = 0;
|
|
all->data_philo->philors[i].all_struct = all;
|
|
all->data_philo->philors[i].is_eat = 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(all->number_of_philosophers + 1, sizeof(pthread_mutex_t));
|
|
while (i < all->number_of_philosophers)
|
|
{
|
|
pthread_mutex_init(&all->data_philo->mutex[i], NULL);
|
|
i++;
|
|
}
|
|
all->data_philo->philors = (t_philo *)ft_calloc(all->number_of_philosophers + 1, sizeof(t_philo));
|
|
get_fork_for_philo(all);
|
|
return (0);
|
|
}
|
|
|
|
void run_all_phils(t_all *all)
|
|
{
|
|
int i;
|
|
i = 0;
|
|
void *philo;
|
|
while(i <= all->number_of_philosophers)
|
|
{
|
|
philo = (void*)(&all->data_philo->philors[i]);
|
|
if (pthread_create(&all->data_philo->philors[i].thread, NULL, (void*)run_trhead, philo) == -1)
|
|
{
|
|
printf("I can't create a thread t%d\n", i);
|
|
}
|
|
else
|
|
{
|
|
pthread_detach(all->data_philo->philors[i].thread);
|
|
}
|
|
i++;
|
|
}
|
|
}
|