diff --git a/Makefile b/Makefile index b970f35..12dec78 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,9 @@ DIR_SRCS = srcs INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D) HEADERS_D = ./headers -HEADERS = philo.h +HEADERS = philo.h \ + philo_struct.h \ + philo_proto.h LIBS = -L $(LIBFT_D) -lft \ -lpthread @@ -18,7 +20,8 @@ LIBS = -L $(LIBFT_D) -lft \ LIBFT_D = ./libft LIBFT = $(LIBFT_D)/libft.a -SRCS = main.c +SRCS = main.c \ + init.c DIR_OBJS = builds OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o) diff --git a/headers/philo.h b/headers/philo.h index da3aaac..074f399 100644 --- a/headers/philo.h +++ b/headers/philo.h @@ -8,21 +8,7 @@ # include # include -typedef struct s_conditions -{ - int n_phi; // number_of_philosophers - int t_die; // time_to_die - int t_eat; // time_to_eat - int t_slp; // time_to_sleep - int n_eat; // [number_of_times_each_philosopher_must_eat] -} t_conditions; - -typedef struct s_philo -{ - t_conditions *conditions; - int philo_nbr; - struct s_philo *prev; - struct s_philo *next; -} t_philo; +# include "philo_struct.h" +# include "philo_proto.h" #endif diff --git a/headers/philo_proto.h b/headers/philo_proto.h new file mode 100644 index 0000000..c7fb798 --- /dev/null +++ b/headers/philo_proto.h @@ -0,0 +1,8 @@ +#ifndef PHILO_PROTO_H +# define PHILO_PROTO_H + +// init.c +t_philo *init(char **av); + +#endif + diff --git a/headers/philo_struct.h b/headers/philo_struct.h new file mode 100644 index 0000000..662eb5b --- /dev/null +++ b/headers/philo_struct.h @@ -0,0 +1,21 @@ +#ifndef PHILO_STRUCT_H +# define PHILO_STRUCT_H + +typedef struct s_conditions +{ + int n_phi; // number_of_philosophers + int t_die; // time_to_die + int t_eat; // time_to_eat + int t_slp; // time_to_sleep + int n_eat; // [number_of_times_each_philosopher_must_eat] +} t_conditions; + +typedef struct s_philo +{ + t_conditions *conditions; + int philo_nbr; + struct s_philo *prev; + struct s_philo *next; +} t_philo; + +#endif diff --git a/init.c b/init.c new file mode 100644 index 0000000..9fdfb4f --- /dev/null +++ b/init.c @@ -0,0 +1,37 @@ +#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/main.c b/srcs/main.c index 534e54b..3b7a867 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -14,41 +14,6 @@ void *philo_exec(void *arg) } */ -t_philo *init(char **av) -{ - t_philo *philo; - t_philo *new; - t_conditions conditions; - int n; - int i; - - n = ft_atoi(av[1]); - 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); -} - int main(int ac, char **av) { // pthread_t id; @@ -57,21 +22,7 @@ int main(int ac, char **av) if (ac == 1) return (0); philo = init(av); - int i; int n; - i = 0; - n = philo->conditions->n_phi; - ft_putnbr_fd(n, 1); - write(1, "\n\n", 2); - //while (i < n * 2 + n / 2) - while (i < philo->conditions->n_phi * 2 + 5) - { - ft_putnbr_fd(philo->conditions->n_phi, 1); - write(1, " - ", 3); - ft_putnbr_fd(philo->philo_nbr, 1); - write(1, "\n", 1); - philo = philo->prev; - i++; - } + // while (philo->n_philo) // { // pthread_create(&id, NULL, &philo_exec, philo);