83 lines
1.7 KiB
C
83 lines
1.7 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++;
|
|
}
|
|
}
|
|
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_f_philo(t_all *all)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (i < all->number_of_philosophers)
|
|
{
|
|
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;
|
|
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);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
int prepare_resources(t_all *all)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
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);
|
|
all->data_philo->philors = (t_philo *)ft_calloc(all->number_of_philosophers \
|
|
+ 1, sizeof(t_philo));
|
|
get_data_f_philo(all);
|
|
return (0);
|
|
}
|
|
|
|
void run_all_phils(t_all *all)
|
|
{
|
|
void *philo;
|
|
int i;
|
|
|
|
i = 0;
|
|
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++;
|
|
}
|
|
}
|