From 4b37e5392e315179f1befff99b0ab6a5254f4c2d Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 9 Jan 2022 10:10:57 +0100 Subject: [PATCH] create chained list of struct philo and start thread --- headers/philo_proto.h | 2 +- init.c | 37 -------------------------------- srcs/init.c | 50 +++++++++++++++++++++++++++++++++++++++++++ srcs/main.c | 27 ++++++++++++----------- 4 files changed, 65 insertions(+), 51 deletions(-) delete mode 100644 init.c create mode 100644 srcs/init.c diff --git a/headers/philo_proto.h b/headers/philo_proto.h index c7fb798..96652b4 100644 --- a/headers/philo_proto.h +++ b/headers/philo_proto.h @@ -2,7 +2,7 @@ # define PHILO_PROTO_H // init.c -t_philo *init(char **av); +t_philo *init(char **av, pthread_t *id); #endif diff --git a/init.c b/init.c deleted file mode 100644 index 9fdfb4f..0000000 --- a/init.c +++ /dev/null @@ -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); -} diff --git a/srcs/init.c b/srcs/init.c new file mode 100644 index 0000000..7bbd8ec --- /dev/null +++ b/srcs/init.c @@ -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); +} diff --git a/srcs/main.c b/srcs/main.c index 3b7a867..13fcb72 100644 --- a/srcs/main.c +++ b/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; }