correction loop chained list

This commit is contained in:
hugogogo
2022-01-13 08:53:27 +01:00
parent 81f0ded5e3
commit bc40b571e9
5 changed files with 96 additions and 70 deletions

View File

@@ -1,7 +1,7 @@
#ifndef PHILO_PROTO_H #ifndef PHILO_PROTO_H
# define PHILO_PROTO_H # define PHILO_PROTO_H
pthread_mutex_t mutex; // t_mtx mutex;
// init.c // init.c
t_philo *init(int ac, char **av, pthread_t **id); t_philo *init(int ac, char **av, pthread_t **id);

View File

@@ -1,20 +1,23 @@
#ifndef PHILO_STRUCT_H #ifndef PHILO_STRUCT_H
# define PHILO_STRUCT_H # define PHILO_STRUCT_H
typedef struct s_conditions typedef pthread_mutex_t t_mtx;
typedef struct s_params
{ {
int n_phi; // number_of_philosophers int n_phi; // number_of_philosophers
int t_die; // time_to_die int t_die; // time_to_die
int t_eat; // time_to_eat int t_eat; // time_to_eat
int t_slp; // time_to_sleep int t_slp; // time_to_sleep
int n_eat; // [number_of_times_each_philosopher_must_eat] int n_eat; // [number_of_times_each_philosopher_must_eat]
} t_conditions; } t_params;
typedef struct s_philo typedef struct s_philo
{ {
t_conditions *conditions; t_params *params;
int philo_nbr; int philo_nbr;
int fork; int fork;
t_mtx *msg_mtx;
struct s_philo *prev; struct s_philo *prev;
struct s_philo *next; struct s_philo *next;
} t_philo; } t_philo;

View File

@@ -1,5 +1,21 @@
#include "philo.h" #include "philo.h"
int take_forks(t_philo *philo)
{
// pthread_mutex_lock(&mutex);
while (philo->fork == 0 || philo->next->fork == 0)
continue ;
if (philo->fork == 1 && philo->next->fork == 1)
{
philo->fork = 0;
philo->next->fork = 0;
ft_printf("%i has taken a fork\n", philo->philo_nbr);
return (1);
}
// pthread_mutex_unlock(&mutex);
return (0);
}
void *philo_exec(void *arg) void *philo_exec(void *arg)
{ {
t_philo *philo; t_philo *philo;
@@ -8,6 +24,12 @@ void *philo_exec(void *arg)
philo = (t_philo*)arg; philo = (t_philo*)arg;
nbr = philo->philo_nbr; nbr = philo->philo_nbr;
pthread_mutex_lock(philo->msg_mtx);
// pthread_mutex_lock(&mutex);
ft_printf("%i is thinking\n", philo->philo_nbr);
pthread_mutex_unlock(philo->msg_mtx);
// pthread_mutex_unlock(&mutex);
// eat // eat
// "has taken a fork" // "has taken a fork"
// "is eating" // "is eating"
@@ -17,30 +39,32 @@ void *philo_exec(void *arg)
// "is thinking" // "is thinking"
// die // die
// "died" // "died"
int i = 0; //int i = 0;
while (i < 2) // while (i < 2)
{ // {
pthread_mutex_lock(&mutex); // if (!take_forks(philo))
if (philo->fork == 1 && philo->next->fork == 1) // continue ;
{ //
philo->fork = 0; // usleep(philo->params->t_eat);
philo->next->fork = 0; //
ft_putnbr_fd(philo->philo_nbr, 1); // pthread_mutex_lock(&mutex);
ft_putstr_fd(" has taken a fork\n", 1); // philo->fork = 1;
} // philo->next->fork = 1;
pthread_mutex_unlock(&mutex); // ft_printf("%i is sleeping\n", philo->philo_nbr);
// pthread_mutex_unlock(&mutex);
//
// usleep(philo->params->t_slp);
//
// pthread_mutex_lock(&mutex);
// ft_printf("%i is thinking\n", philo->philo_nbr);
// pthread_mutex_unlock(&mutex);
usleep(philo->conditions->t_eat); // pthread_mutex_lock(philo->mutex);
// pthread_mutex_lock(&mutex);
pthread_mutex_lock(&mutex); // ft_printf("%i is thinking\n", philo->philo_nbr);
philo->fork = 1; // pthread_mutex_unlock(philo->mutex);
philo->next->fork = 1; // pthread_mutex_unlock(&mutex);
ft_printf("%i is sleeping\n", philo->philo_nbr); // i++;
pthread_mutex_unlock(&mutex); // }
usleep(philo->conditions->t_slp);
i++;
}
return (NULL); return (NULL);
} }

View File

@@ -1,78 +1,77 @@
#include "philo.h" #include "philo.h"
t_philo *init_struc_philo(t_philo *new, t_conditions *conditions, int i) t_philo *create_each_philo(t_philo *philo, t_params *params, t_mtx *mtx, int i)
{ {
new->conditions = conditions; t_philo *new;
new = malloc(sizeof(t_philo) * 1);
if (new == NULL)
return (NULL);
new->params = params;
new->philo_nbr = i + 1; new->philo_nbr = i + 1;
new->fork = 1; new->fork = 1;
new->msg_mtx = mtx;
if (philo)
philo->next = new;
new->prev = philo;
return (new); return (new);
} }
// it's just because init_chain_philo was too long // looping chained list
void put_prev_n_next(t_philo *new, t_philo *philo) t_philo *init_chain_philo(t_params *params, t_mtx *mutex)
{ {
new->prev = philo; t_philo *end;
philo->next = new;
}
t_philo *init_chain_philo(t_conditions *conditions, int n)
{
t_philo *start;
t_philo *philo; t_philo *philo;
t_philo *new;
int i; int i;
i = 0; i = 0;
while (i < n) philo = NULL;
while (i < params->n_phi)
{ {
new = malloc(sizeof(t_philo) * 1); philo = create_each_philo(philo, params, mutex, i);
if (new == NULL)
return (NULL);
new = init_struc_philo(new, conditions, i);
if (i == 0)
start = new;
else
put_prev_n_next(new, philo);
philo = new;
i++; i++;
} }
philo = start; end = philo;
philo->prev = new; while (philo->prev != NULL)
new->next = philo; philo = philo->prev;
philo->prev = end;
end->next = philo;
return (philo); return (philo);
} }
t_conditions *init_conditions(int ac, char **av) t_params *init_params(int ac, char **av)
{ {
t_conditions *conditions; t_params *params;
if (ac == 5 || ac == 6) if (ac == 5 || ac == 6)
{ {
conditions = malloc(sizeof(t_conditions)); params = malloc(sizeof(t_params));
conditions->n_phi = ft_atoi(av[1]); params->n_phi = ft_atoi(av[1]);
conditions->t_die = ft_atoi(av[2]); params->t_die = ft_atoi(av[2]);
conditions->t_eat = ft_atoi(av[3]); params->t_eat = ft_atoi(av[3]);
conditions->t_slp = ft_atoi(av[4]); params->t_slp = ft_atoi(av[4]);
if (ac == 6) if (ac == 6)
conditions->n_eat = ft_atoi(av[5]); params->n_eat = ft_atoi(av[5]);
} }
else else
return (NULL); return (NULL);
return (conditions); return (params);
} }
t_philo *init(int ac, char **av, pthread_t **id) t_philo *init(int ac, char **av, pthread_t **id)
{ {
t_philo *philo; t_philo *philo;
t_conditions *conditions; t_params *params;
t_mtx msg_mtx;
conditions = init_conditions(ac, av); params = init_params(ac, av);
if (conditions == NULL) if (params == NULL)
return (NULL); return (NULL);
*id = malloc(sizeof(int) * conditions->n_phi); *id = malloc(sizeof(int) * params->n_phi);
if (*id == NULL) if (*id == NULL)
return (NULL); return (NULL);
philo = init_chain_philo(conditions, conditions->n_phi); pthread_mutex_init(&msg_mtx, NULL);
philo = init_chain_philo(params, &msg_mtx);
if (philo == NULL) if (philo == NULL)
return (NULL); return (NULL);
return (philo); return (philo);

View File

@@ -23,8 +23,8 @@ int main(int ac, char **av)
philo = init(ac, av, &id); philo = init(ac, av, &id);
if (philo == NULL) if (philo == NULL)
return (0); return (0);
n = philo->conditions->n_phi; n = philo->params->n_phi;
pthread_mutex_init(&mutex, NULL); // pthread_mutex_init(&mutex, NULL);
create_threads(philo, id, n); create_threads(philo, id, n);
i = 0; i = 0;
while (i < n) while (i < n)