86 lines
2.0 KiB
C
86 lines
2.0 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_data_for_philo(t_all *all)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
|
|
while(i <= all->number_of_philosophers)
|
|
{
|
|
all->data_philo->philors[i].name = sem_set_name("philo", i);
|
|
all->data_philo->philors[i].eat_sem = reopen_sem(all->data_philo->philors[i].name, 1);
|
|
if (all->data_philo->philors[i].eat_sem == SEM_FAILED)
|
|
exit(0); // TODO fix this
|
|
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)
|
|
{
|
|
all->data_philo = ft_calloc(sizeof(t_philo_data), 1);
|
|
all->data_philo->sems = reopen_sem(_SEM_NAME_FORKS, all->number_of_philosophers);
|
|
all->write = reopen_sem(_SEM_WRITE_LOCK, 1);
|
|
if (all->data_philo->sems == SEM_FAILED)
|
|
return(1); // TODO выход из программы сделать
|
|
if (all->write == SEM_FAILED)
|
|
return(1);
|
|
all->data_philo->philors = (t_philo *)ft_calloc(sizeof(t_philo), all->number_of_philosophers);
|
|
get_data_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++;
|
|
}
|
|
}
|