create chained list of struct philo and start thread
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
# define PHILO_PROTO_H
|
||||
|
||||
// init.c
|
||||
t_philo *init(char **av);
|
||||
t_philo *init(char **av, pthread_t *id);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
37
init.c
37
init.c
@@ -1,37 +0,0 @@
|
||||
#include "philo.h"
|
||||
|
||||
t_philo *init(char **av)
|
||||
{
|
||||
t_philo *philo;
|
||||
t_philo *new;
|
||||
t_conditions *conditions;
|
||||
int n;
|
||||
int i;
|
||||
|
||||
n = ft_atoi(av[1]);
|
||||
conditions = malloc(sizeof(t_conditions));
|
||||
conditions->n_phi = n;
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
new = malloc(sizeof(t_philo) * 1);
|
||||
new->conditions = conditions;
|
||||
new->philo_nbr = i + 1;
|
||||
if (i > 0)
|
||||
{
|
||||
new->prev = philo;
|
||||
philo->next = new;
|
||||
}
|
||||
philo = new;
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (i < n - 1)
|
||||
{
|
||||
philo = philo->prev;
|
||||
i++;
|
||||
}
|
||||
philo->prev = new;
|
||||
new->next = philo;
|
||||
return (philo);
|
||||
}
|
||||
50
srcs/init.c
Normal file
50
srcs/init.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#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);
|
||||
}
|
||||
27
srcs/main.c
27
srcs/main.c
@@ -1,34 +1,35 @@
|
||||
#include "philo.h"
|
||||
|
||||
/*
|
||||
void *philo_exec(void *arg)
|
||||
{
|
||||
t_philo philo;
|
||||
t_philo *philo;
|
||||
int nbr;
|
||||
|
||||
philo = *(t_philo*)arg;
|
||||
nbr = philo.n_philo;
|
||||
philo = (t_philo*)arg;
|
||||
nbr = philo->philo_nbr;
|
||||
ft_putnbr_fd(nbr, 1);
|
||||
write(1, "\n", 1);
|
||||
return (NULL);
|
||||
}
|
||||
*/
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
// pthread_t id;
|
||||
pthread_t id;
|
||||
t_philo *philo;
|
||||
int n;
|
||||
|
||||
if (ac == 1)
|
||||
return (0);
|
||||
philo = init(av);
|
||||
philo = init(av, &id);
|
||||
|
||||
// while (philo->n_philo)
|
||||
// {
|
||||
// pthread_create(&id, NULL, &philo_exec, philo);
|
||||
// philo->n_philo--;
|
||||
// }
|
||||
n = philo->conditions->n_phi;
|
||||
while (n)
|
||||
{
|
||||
pthread_create(&id, NULL, &philo_exec, philo);
|
||||
philo = philo->next;
|
||||
n--;
|
||||
}
|
||||
// while (1)
|
||||
// write(1, "main function\n", 14);
|
||||
write(1, "main function\n", 14);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user