Files
42_INT_08_philosophers/srcs/init.c

51 lines
871 B
C

#include "philo.h"
t_philo *init_struc_philo(t_philo *new, t_conditions *conditions, int i)
{
new->conditions = conditions;
new->philo_nbr = i + 1;
return (new);
}
t_philo *init_chain_philo(t_conditions *conditions, int n)
{
t_philo *start;
t_philo *philo;
t_philo *new;
int i;
i = 0;
while (i < n)
{
new = malloc(sizeof(t_philo) * 1);
new = init_struc_philo(new, conditions, i);
if (i == 0)
start = new;
else
{
new->prev = philo;
philo->next = new;
}
philo = new;
i++;
}
philo = start;
philo->prev = new;
new->next = philo;
return (philo);
}
t_philo *init(char **av, pthread_t **id)
{
t_philo *philo;
t_conditions *conditions;
int n;
n = ft_atoi(av[1]);
conditions = malloc(sizeof(t_conditions));
conditions->n_phi = n;
*id = malloc(sizeof(int) * n);
philo = init_chain_philo(conditions, n);
return (philo);
}