correction loop chained list
This commit is contained in:
79
srcs/init.c
79
srcs/init.c
@@ -1,78 +1,77 @@
|
||||
#include "philo.h"
|
||||
|
||||
t_philo *init_struc_philo(t_philo *new, t_conditions *conditions, int i)
|
||||
t_philo *create_each_philo(t_philo *philo, t_params *params, t_mtx *mtx, int i)
|
||||
{
|
||||
new->conditions = conditions;
|
||||
t_philo *new;
|
||||
|
||||
new = malloc(sizeof(t_philo) * 1);
|
||||
if (new == NULL)
|
||||
return (NULL);
|
||||
new->params = params;
|
||||
new->philo_nbr = i + 1;
|
||||
new->fork = 1;
|
||||
new->msg_mtx = mtx;
|
||||
if (philo)
|
||||
philo->next = new;
|
||||
new->prev = philo;
|
||||
return (new);
|
||||
}
|
||||
|
||||
// it's just because init_chain_philo was too long
|
||||
void put_prev_n_next(t_philo *new, t_philo *philo)
|
||||
// looping chained list
|
||||
t_philo *init_chain_philo(t_params *params, t_mtx *mutex)
|
||||
{
|
||||
new->prev = philo;
|
||||
philo->next = new;
|
||||
}
|
||||
|
||||
t_philo *init_chain_philo(t_conditions *conditions, int n)
|
||||
{
|
||||
t_philo *start;
|
||||
t_philo *end;
|
||||
t_philo *philo;
|
||||
t_philo *new;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < n)
|
||||
philo = NULL;
|
||||
while (i < params->n_phi)
|
||||
{
|
||||
new = malloc(sizeof(t_philo) * 1);
|
||||
if (new == NULL)
|
||||
return (NULL);
|
||||
new = init_struc_philo(new, conditions, i);
|
||||
if (i == 0)
|
||||
start = new;
|
||||
else
|
||||
put_prev_n_next(new, philo);
|
||||
philo = new;
|
||||
philo = create_each_philo(philo, params, mutex, i);
|
||||
i++;
|
||||
}
|
||||
philo = start;
|
||||
philo->prev = new;
|
||||
new->next = philo;
|
||||
end = philo;
|
||||
while (philo->prev != NULL)
|
||||
philo = philo->prev;
|
||||
philo->prev = end;
|
||||
end->next = philo;
|
||||
return (philo);
|
||||
}
|
||||
|
||||
t_conditions *init_conditions(int ac, char **av)
|
||||
t_params *init_params(int ac, char **av)
|
||||
{
|
||||
t_conditions *conditions;
|
||||
t_params *params;
|
||||
|
||||
if (ac == 5 || ac == 6)
|
||||
{
|
||||
conditions = malloc(sizeof(t_conditions));
|
||||
conditions->n_phi = ft_atoi(av[1]);
|
||||
conditions->t_die = ft_atoi(av[2]);
|
||||
conditions->t_eat = ft_atoi(av[3]);
|
||||
conditions->t_slp = ft_atoi(av[4]);
|
||||
params = malloc(sizeof(t_params));
|
||||
params->n_phi = ft_atoi(av[1]);
|
||||
params->t_die = ft_atoi(av[2]);
|
||||
params->t_eat = ft_atoi(av[3]);
|
||||
params->t_slp = ft_atoi(av[4]);
|
||||
if (ac == 6)
|
||||
conditions->n_eat = ft_atoi(av[5]);
|
||||
params->n_eat = ft_atoi(av[5]);
|
||||
}
|
||||
else
|
||||
return (NULL);
|
||||
return (conditions);
|
||||
return (params);
|
||||
}
|
||||
|
||||
t_philo *init(int ac, char **av, pthread_t **id)
|
||||
{
|
||||
t_philo *philo;
|
||||
t_conditions *conditions;
|
||||
t_philo *philo;
|
||||
t_params *params;
|
||||
t_mtx msg_mtx;
|
||||
|
||||
conditions = init_conditions(ac, av);
|
||||
if (conditions == NULL)
|
||||
params = init_params(ac, av);
|
||||
if (params == NULL)
|
||||
return (NULL);
|
||||
*id = malloc(sizeof(int) * conditions->n_phi);
|
||||
*id = malloc(sizeof(int) * params->n_phi);
|
||||
if (*id == NULL)
|
||||
return (NULL);
|
||||
philo = init_chain_philo(conditions, conditions->n_phi);
|
||||
pthread_mutex_init(&msg_mtx, NULL);
|
||||
philo = init_chain_philo(params, &msg_mtx);
|
||||
if (philo == NULL)
|
||||
return (NULL);
|
||||
return (philo);
|
||||
|
||||
Reference in New Issue
Block a user