some norm and malloc protection
This commit is contained in:
3
Makefile
3
Makefile
@@ -21,7 +21,8 @@ LIBFT_D = ./libft
|
|||||||
LIBFT = $(LIBFT_D)/libft.a
|
LIBFT = $(LIBFT_D)/libft.a
|
||||||
|
|
||||||
SRCS = main.c \
|
SRCS = main.c \
|
||||||
init.c
|
init.c \
|
||||||
|
exec.c
|
||||||
|
|
||||||
DIR_OBJS = builds
|
DIR_OBJS = builds
|
||||||
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
#ifndef PHILO_PROTO_H
|
#ifndef PHILO_PROTO_H
|
||||||
# define PHILO_PROTO_H
|
# define PHILO_PROTO_H
|
||||||
|
|
||||||
|
pthread_mutex_t mutex;
|
||||||
|
|
||||||
// init.c
|
// init.c
|
||||||
t_philo *init(char **av, pthread_t **id);
|
t_philo *init(int ac, char **av, pthread_t **id);
|
||||||
|
|
||||||
|
// exec.c
|
||||||
|
void *philo_exec(void *arg);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
19
srcs/exec.c
Normal file
19
srcs/exec.c
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include "philo.h"
|
||||||
|
|
||||||
|
void *philo_exec(void *arg)
|
||||||
|
{
|
||||||
|
t_philo *philo;
|
||||||
|
int nbr;
|
||||||
|
|
||||||
|
philo = (t_philo*)arg;
|
||||||
|
nbr = philo->philo_nbr;
|
||||||
|
|
||||||
|
pthread_mutex_lock(&mutex);
|
||||||
|
write(1, "-", 1);
|
||||||
|
ft_putnbr_fd(nbr, 1);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
pthread_mutex_unlock(&mutex);
|
||||||
|
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
50
srcs/init.c
50
srcs/init.c
@@ -7,6 +7,13 @@ t_philo *init_struc_philo(t_philo *new, t_conditions *conditions, int i)
|
|||||||
return (new);
|
return (new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// it's just because init_chain_philo was too long
|
||||||
|
void put_prev_n_next(t_philo *new, t_philo *philo)
|
||||||
|
{
|
||||||
|
new->prev = philo;
|
||||||
|
philo->next = new;
|
||||||
|
}
|
||||||
|
|
||||||
t_philo *init_chain_philo(t_conditions *conditions, int n)
|
t_philo *init_chain_philo(t_conditions *conditions, int n)
|
||||||
{
|
{
|
||||||
t_philo *start;
|
t_philo *start;
|
||||||
@@ -18,14 +25,13 @@ t_philo *init_chain_philo(t_conditions *conditions, int n)
|
|||||||
while (i < n)
|
while (i < n)
|
||||||
{
|
{
|
||||||
new = malloc(sizeof(t_philo) * 1);
|
new = malloc(sizeof(t_philo) * 1);
|
||||||
|
if (new == NULL)
|
||||||
|
return (NULL);
|
||||||
new = init_struc_philo(new, conditions, i);
|
new = init_struc_philo(new, conditions, i);
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
start = new;
|
start = new;
|
||||||
else
|
else
|
||||||
{
|
put_prev_n_next(new, philo);
|
||||||
new->prev = philo;
|
|
||||||
philo->next = new;
|
|
||||||
}
|
|
||||||
philo = new;
|
philo = new;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@@ -35,16 +41,38 @@ t_philo *init_chain_philo(t_conditions *conditions, int n)
|
|||||||
return (philo);
|
return (philo);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_philo *init(char **av, pthread_t **id)
|
t_conditions *init_conditions(int ac, char **av)
|
||||||
|
{
|
||||||
|
t_conditions *conditions;
|
||||||
|
|
||||||
|
if (ac == 5 || ac == 6)
|
||||||
|
{
|
||||||
|
conditions = malloc(sizeof(t_conditions));
|
||||||
|
conditions->n_phi = ft_atoi(av[1]);
|
||||||
|
conditions->t_die = ft_atoi(av[2]);
|
||||||
|
conditions->t_eat = ft_atoi(av[3]);
|
||||||
|
conditions->t_slp = ft_atoi(av[4]);
|
||||||
|
if (ac == 6)
|
||||||
|
conditions->n_eat = ft_atoi(av[5]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return (NULL);
|
||||||
|
return (conditions);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_philo *init(int ac, char **av, pthread_t **id)
|
||||||
{
|
{
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
t_conditions *conditions;
|
t_conditions *conditions;
|
||||||
int n;
|
|
||||||
|
|
||||||
n = ft_atoi(av[1]);
|
conditions = init_conditions(ac, av);
|
||||||
conditions = malloc(sizeof(t_conditions));
|
if (conditions == NULL)
|
||||||
conditions->n_phi = n;
|
return (NULL);
|
||||||
*id = malloc(sizeof(int) * n);
|
*id = malloc(sizeof(int) * conditions->n_phi);
|
||||||
philo = init_chain_philo(conditions, n);
|
if (*id == NULL)
|
||||||
|
return (NULL);
|
||||||
|
philo = init_chain_philo(conditions, conditions->n_phi);
|
||||||
|
if (philo == NULL)
|
||||||
|
return (NULL);
|
||||||
return (philo);
|
return (philo);
|
||||||
}
|
}
|
||||||
|
|||||||
42
srcs/main.c
42
srcs/main.c
@@ -1,22 +1,16 @@
|
|||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
|
||||||
pthread_mutex_t mutex;
|
void create_threads(t_philo *philo, pthread_t *id, int n)
|
||||||
|
|
||||||
void *philo_exec(void *arg)
|
|
||||||
{
|
{
|
||||||
t_philo *philo;
|
int i;
|
||||||
int nbr;
|
|
||||||
|
|
||||||
philo = (t_philo*)arg;
|
i = 0;
|
||||||
nbr = philo->philo_nbr;
|
while (i < n)
|
||||||
|
{
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_create(&id[i], NULL, &philo_exec, philo);
|
||||||
write(1, "-", 1);
|
philo = philo->next;
|
||||||
ft_putnbr_fd(nbr, 1);
|
i++;
|
||||||
write(1, "\n", 1);
|
}
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
|
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
@@ -26,24 +20,18 @@ int main(int ac, char **av)
|
|||||||
int i;
|
int i;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
if (ac == 1)
|
philo = init(ac, av, &id);
|
||||||
return (0);
|
if (philo == NULL)
|
||||||
philo = init(av, &id);
|
return (0);
|
||||||
i = 0;
|
|
||||||
n = philo->conditions->n_phi;
|
n = philo->conditions->n_phi;
|
||||||
pthread_mutex_init(&mutex, NULL);
|
pthread_mutex_init(&mutex, NULL);
|
||||||
while (i < n)
|
create_threads(philo, id, n);
|
||||||
{
|
|
||||||
pthread_create(&id[i], NULL, &philo_exec, philo);
|
|
||||||
philo = philo->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < n)
|
while (i < n)
|
||||||
{
|
{
|
||||||
pthread_join(id[i], NULL);
|
pthread_join(id[i], NULL);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
write(1, "main function\n", 14);
|
write(1, "main function\n", 14);
|
||||||
return 0;
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user