meilleur organisation code structures et variables
This commit is contained in:
114
:w
Normal file
114
:w
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
#include "philo.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
t_params *params;
|
||||||
|
t_params *global;
|
||||||
|
int p_nbr;
|
||||||
|
t_mtx m_fork;
|
||||||
|
long int t_last_meal_s;
|
||||||
|
long int t_last_meal_u;
|
||||||
|
*/
|
||||||
|
t_philo *lst_add_philo(t_params *params, t_mtx *m_print, t_mtx *m_dead, int i)
|
||||||
|
{
|
||||||
|
t_philo *new;
|
||||||
|
|
||||||
|
new = malloc(sizeof(t_philo) * 1);
|
||||||
|
if (new == NULL)
|
||||||
|
return (NULL);
|
||||||
|
new->params = params;
|
||||||
|
new->global = global;
|
||||||
|
new->p_nbr = i + 1;
|
||||||
|
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
||||||
|
return (NULL);
|
||||||
|
new->next = NULL;
|
||||||
|
return (new);
|
||||||
|
}
|
||||||
|
|
||||||
|
// looping chained list
|
||||||
|
t_philo *init_chain_philo(t_params *params, t_global *global)
|
||||||
|
{
|
||||||
|
t_philo *philo;
|
||||||
|
t_philo *tmp;
|
||||||
|
t_philo *start;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
philo = NULL;
|
||||||
|
while (i < params->n_phi)
|
||||||
|
{
|
||||||
|
tmp = lst_add_philo(params, global, i);
|
||||||
|
if (philo)
|
||||||
|
philo->next = tmp;
|
||||||
|
else
|
||||||
|
start = tmp;
|
||||||
|
philo = tmp;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
philo->next = start;
|
||||||
|
return (philo);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_params *init_params(int ac, char **av)
|
||||||
|
{
|
||||||
|
t_params *params;
|
||||||
|
|
||||||
|
if (ac == 5 || ac == 6)
|
||||||
|
{
|
||||||
|
params = malloc(sizeof(t_params));
|
||||||
|
if (!params)
|
||||||
|
return (NULL);
|
||||||
|
params->n_phi = ft_atoi(av[1]);
|
||||||
|
params->t_die = ft_atoi(av[2]);
|
||||||
|
params->t_eat = ft_atoi(av[3]);
|
||||||
|
params->t_slp = ft_atoi(av[4]);
|
||||||
|
if (ac == 6)
|
||||||
|
params->n_eat = ft_atoi(av[5]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return (NULL);
|
||||||
|
return (params);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_global *init_global(t_mtx *m_print, t_mtx *m_dead)
|
||||||
|
{
|
||||||
|
t_global *global;
|
||||||
|
|
||||||
|
global = malloc(sizeof(t_global));
|
||||||
|
if (!global)
|
||||||
|
return (NULL);
|
||||||
|
global->dead = 0;
|
||||||
|
global->t_start_s = 0;
|
||||||
|
global->t_start_u = 0;
|
||||||
|
global->m_print = m_print;
|
||||||
|
global->m_dead = m_dead;
|
||||||
|
return (global);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_philo *init(int ac, char **av, pthread_t **id)
|
||||||
|
{
|
||||||
|
t_philo *philo;
|
||||||
|
t_params *params;
|
||||||
|
t_global *global;
|
||||||
|
t_mtx *m_print;
|
||||||
|
t_mtx *m_dead;
|
||||||
|
|
||||||
|
m_print = malloc(sizeof(t_mtx));
|
||||||
|
if (pthread_mutex_init(m_print, NULL) != 0)
|
||||||
|
return (NULL);
|
||||||
|
m_dead = malloc(sizeof(t_mtx));
|
||||||
|
if (pthread_mutex_init(m_dead, NULL) != 0)
|
||||||
|
return (NULL);
|
||||||
|
params = init_params(ac, av);
|
||||||
|
if (params == NULL)
|
||||||
|
return (NULL);
|
||||||
|
global = init_global(m_print, m_dead);
|
||||||
|
if (params == NULL)
|
||||||
|
return (NULL);
|
||||||
|
*id = malloc(sizeof(pthread_t) * params->n_phi);
|
||||||
|
if (*id == NULL)
|
||||||
|
return (NULL);
|
||||||
|
philo = init_chain_philo(params, global);
|
||||||
|
if (philo == NULL)
|
||||||
|
return (NULL);
|
||||||
|
return (philo);
|
||||||
|
}
|
||||||
@@ -10,17 +10,23 @@ typedef struct s_params
|
|||||||
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_params;
|
||||||
|
|
||||||
|
typedef struct s_global
|
||||||
|
{
|
||||||
int dead;
|
int dead;
|
||||||
long int t_start_s;
|
long int t_start_s;
|
||||||
long int t_start_u;
|
long int t_start_u;
|
||||||
} t_params;
|
t_mtx m_print;
|
||||||
|
t_mtx m_dead;
|
||||||
|
} t_global;
|
||||||
|
|
||||||
typedef struct s_philo
|
typedef struct s_philo
|
||||||
{
|
{
|
||||||
t_params *params;
|
t_params *params;
|
||||||
|
t_global *global;
|
||||||
int p_nbr;
|
int p_nbr;
|
||||||
t_mtx m_fork;
|
t_mtx m_fork;
|
||||||
t_mtx *m_print;
|
|
||||||
long int t_last_meal_s;
|
long int t_last_meal_s;
|
||||||
long int t_last_meal_u;
|
long int t_last_meal_u;
|
||||||
struct s_philo *next;
|
struct s_philo *next;
|
||||||
|
|||||||
10
srcs/exec.c
10
srcs/exec.c
@@ -19,14 +19,15 @@ int ret_unlock(t_mtx *mutex_1, t_mtx *mutex_2, int ret)
|
|||||||
int take_forks(t_philo *philo)
|
int take_forks(t_philo *philo)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&(philo->m_fork));
|
pthread_mutex_lock(&(philo->m_fork));
|
||||||
if (philo->params->dead != 0)
|
if (philo->global->dead != 0)
|
||||||
return (ret_unlock(&(philo->m_fork), NULL, 1));
|
return (ret_unlock(&(philo->m_fork), NULL, 1));
|
||||||
print_message(philo, "has taken a fork");
|
print_message(philo, "has taken a fork");
|
||||||
pthread_mutex_lock(&(philo->next->m_fork));
|
pthread_mutex_lock(&(philo->next->m_fork));
|
||||||
if (philo->params->dead != 0)
|
if (philo->global->dead != 0)
|
||||||
return (ret_unlock(&(philo->m_fork), &(philo->next->m_fork), 1));
|
return (ret_unlock(&(philo->m_fork), &(philo->next->m_fork), 1));
|
||||||
print_message(philo, "has taken a fork");
|
print_message(philo, "has taken a fork");
|
||||||
|
|
||||||
|
update_time(philo);
|
||||||
print_message(philo, "is eating");
|
print_message(philo, "is eating");
|
||||||
go_sleep(philo, philo->params->t_eat);
|
go_sleep(philo, philo->params->t_eat);
|
||||||
|
|
||||||
@@ -35,11 +36,6 @@ int take_forks(t_philo *philo)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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]
|
|
||||||
void *philo_exec(void *arg)
|
void *philo_exec(void *arg)
|
||||||
{
|
{
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
|
|||||||
42
srcs/init.c
42
srcs/init.c
@@ -1,6 +1,6 @@
|
|||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
|
||||||
t_philo *lst_add_philo(t_params *params, t_mtx *m_print, int i)
|
t_philo *lst_add_philo(t_params *params, t_global *global, int i)
|
||||||
{
|
{
|
||||||
t_philo *new;
|
t_philo *new;
|
||||||
|
|
||||||
@@ -8,16 +8,18 @@ t_philo *lst_add_philo(t_params *params, t_mtx *m_print, int i)
|
|||||||
if (new == NULL)
|
if (new == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
new->params = params;
|
new->params = params;
|
||||||
|
new->global = global;
|
||||||
new->p_nbr = i + 1;
|
new->p_nbr = i + 1;
|
||||||
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
new->m_print = m_print;
|
new->t_last_meal_s = 0;
|
||||||
|
new->t_last_meal_u = 0;
|
||||||
new->next = NULL;
|
new->next = NULL;
|
||||||
return (new);
|
return (new);
|
||||||
}
|
}
|
||||||
|
|
||||||
// looping chained list
|
// looping chained list
|
||||||
t_philo *init_chain_philo(t_params *params, t_mtx *m_print)
|
t_philo *init_chain_philo(t_params *params, t_global *global)
|
||||||
{
|
{
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
t_philo *tmp;
|
t_philo *tmp;
|
||||||
@@ -28,7 +30,7 @@ t_philo *init_chain_philo(t_params *params, t_mtx *m_print)
|
|||||||
philo = NULL;
|
philo = NULL;
|
||||||
while (i < params->n_phi)
|
while (i < params->n_phi)
|
||||||
{
|
{
|
||||||
tmp = lst_add_philo(params, m_print, i);
|
tmp = lst_add_philo(params, global, i);
|
||||||
if (philo)
|
if (philo)
|
||||||
philo->next = tmp;
|
philo->next = tmp;
|
||||||
else
|
else
|
||||||
@@ -47,37 +49,53 @@ t_params *init_params(int ac, char **av)
|
|||||||
if (ac == 5 || ac == 6)
|
if (ac == 5 || ac == 6)
|
||||||
{
|
{
|
||||||
params = malloc(sizeof(t_params));
|
params = malloc(sizeof(t_params));
|
||||||
|
if (!params)
|
||||||
|
return (NULL);
|
||||||
params->n_phi = ft_atoi(av[1]);
|
params->n_phi = ft_atoi(av[1]);
|
||||||
params->t_die = ft_atoi(av[2]);
|
params->t_die = ft_atoi(av[2]);
|
||||||
params->t_eat = ft_atoi(av[3]);
|
params->t_eat = ft_atoi(av[3]);
|
||||||
params->t_slp = ft_atoi(av[4]);
|
params->t_slp = ft_atoi(av[4]);
|
||||||
if (ac == 6)
|
if (ac == 6)
|
||||||
params->n_eat = ft_atoi(av[5]);
|
params->n_eat = ft_atoi(av[5]);
|
||||||
params->dead = 0;
|
|
||||||
params->t_start_s = 0;
|
|
||||||
params->t_start_u = 0;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return (NULL);
|
return (NULL);
|
||||||
return (params);
|
return (params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t_global *init_global(void)
|
||||||
|
{
|
||||||
|
t_global *global;
|
||||||
|
|
||||||
|
global = malloc(sizeof(t_global));
|
||||||
|
if (!global)
|
||||||
|
return (NULL);
|
||||||
|
global->dead = 0;
|
||||||
|
global->t_start_s = 0;
|
||||||
|
global->t_start_u = 0;
|
||||||
|
if (pthread_mutex_init(&(global->m_print), NULL) != 0)
|
||||||
|
return (NULL);
|
||||||
|
if (pthread_mutex_init(&(global->m_dead), NULL) != 0)
|
||||||
|
return (NULL);
|
||||||
|
return (global);
|
||||||
|
}
|
||||||
|
|
||||||
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_params *params;
|
t_params *params;
|
||||||
t_mtx *m_print;
|
t_global *global;
|
||||||
|
|
||||||
params = init_params(ac, av);
|
params = init_params(ac, av);
|
||||||
|
if (params == NULL)
|
||||||
|
return (NULL);
|
||||||
|
global = init_global();
|
||||||
if (params == NULL)
|
if (params == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
*id = malloc(sizeof(pthread_t) * params->n_phi);
|
*id = malloc(sizeof(pthread_t) * params->n_phi);
|
||||||
if (*id == NULL)
|
if (*id == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
m_print = malloc(sizeof(t_mtx));
|
philo = init_chain_philo(params, global);
|
||||||
if (pthread_mutex_init(m_print, NULL) != 0)
|
|
||||||
return (NULL);
|
|
||||||
philo = init_chain_philo(params, m_print);
|
|
||||||
if (philo == NULL)
|
if (philo == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
return (philo);
|
return (philo);
|
||||||
|
|||||||
11
srcs/main.c
11
srcs/main.c
@@ -13,11 +13,22 @@ void launch_threads(t_philo *philo, pthread_t *id)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int is_dead(t_philo *philo)
|
||||||
|
{
|
||||||
|
(void)philo;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
void watch_threads(t_philo *philo, pthread_t *id)
|
void watch_threads(t_philo *philo, pthread_t *id)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
if (is_dead(philo) == 1)
|
||||||
|
break ;
|
||||||
|
}
|
||||||
while (i < philo->params->n_phi)
|
while (i < philo->params->n_phi)
|
||||||
{
|
{
|
||||||
pthread_join(id[i], NULL);
|
pthread_join(id[i], NULL);
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ void print_message(t_philo *philo, char *msg)
|
|||||||
char *color;
|
char *color;
|
||||||
|
|
||||||
gettimeofday(&stime, NULL);
|
gettimeofday(&stime, NULL);
|
||||||
time_stamp = (stime.tv_sec - philo->params->t_start_s) * 1000;
|
time_stamp = (stime.tv_sec - philo->global->t_start_s) * 1000;
|
||||||
time_stamp += (stime.tv_usec - philo->params->t_start_u) / 1000;
|
time_stamp += (stime.tv_usec - philo->global->t_start_u) / 1000;
|
||||||
color = WHITE;
|
color = WHITE;
|
||||||
if (ft_strnstr(msg, "eating", ft_strlen(msg)))
|
if (ft_strnstr(msg, "eating", ft_strlen(msg)))
|
||||||
color = B_YELLOW;
|
color = B_YELLOW;
|
||||||
@@ -16,7 +16,7 @@ void print_message(t_philo *philo, char *msg)
|
|||||||
color = B_BLUE;
|
color = B_BLUE;
|
||||||
else if (ft_strnstr(msg, "thinking", ft_strlen(msg)))
|
else if (ft_strnstr(msg, "thinking", ft_strlen(msg)))
|
||||||
color = B_GREEN;
|
color = B_GREEN;
|
||||||
pthread_mutex_lock(philo->m_print);
|
pthread_mutex_lock(&(philo->global->m_print));
|
||||||
ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET);
|
ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET);
|
||||||
pthread_mutex_unlock(philo->m_print);
|
pthread_mutex_unlock(&(philo->global->m_print));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ void init_time(t_philo *philo)
|
|||||||
{
|
{
|
||||||
struct timeval stime;
|
struct timeval stime;
|
||||||
|
|
||||||
if (philo->params->t_start_s == 0)
|
if (philo->global->t_start_s == 0)
|
||||||
{
|
{
|
||||||
gettimeofday(&stime, NULL);
|
gettimeofday(&stime, NULL);
|
||||||
philo->params->t_start_s = stime.tv_sec;
|
philo->global->t_start_s = stime.tv_sec;
|
||||||
philo->params->t_start_u = stime.tv_usec;
|
philo->global->t_start_u = stime.tv_usec;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user