init structs
This commit is contained in:
7
Makefile
7
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)
|
||||
|
||||
@@ -8,21 +8,7 @@
|
||||
# include <unistd.h>
|
||||
# include <pthread.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;
|
||||
# include "philo_struct.h"
|
||||
# include "philo_proto.h"
|
||||
|
||||
#endif
|
||||
|
||||
8
headers/philo_proto.h
Normal file
8
headers/philo_proto.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef PHILO_PROTO_H
|
||||
# define PHILO_PROTO_H
|
||||
|
||||
// init.c
|
||||
t_philo *init(char **av);
|
||||
|
||||
#endif
|
||||
|
||||
21
headers/philo_struct.h
Normal file
21
headers/philo_struct.h
Normal file
@@ -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
|
||||
37
init.c
Normal file
37
init.c
Normal file
@@ -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);
|
||||
}
|
||||
51
srcs/main.c
51
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);
|
||||
|
||||
Reference in New Issue
Block a user