ajout static, renames, pere fouettard tweaks

This commit is contained in:
Hugo LAMY
2022-01-26 16:31:36 +01:00
parent 6d3280631c
commit 77f2e6b978
9 changed files with 130 additions and 120 deletions

View File

@@ -19,7 +19,7 @@ LIBS = -lpthread
SRCS = main.c \ SRCS = main.c \
init.c \ init.c \
pere_fouettard.c \ launch.c \
exec.c \ exec.c \
utils.c \ utils.c \
generic.c generic.c

View File

@@ -16,20 +16,19 @@
// init.c // init.c
t_philo *init(int ac, char **av, pthread_t **id); t_philo *init(int ac, char **av, pthread_t **id);
// pere_fouettard.c // launch.c
int is_dead(t_philo *philo); void launch(t_philo *philo, pthread_t *id);
void launch_threads(t_philo *philo, pthread_t *id);
// exec.c // exec.c
void *philo_exec(void *arg); void *philo_exec(void *arg);
// utils.c // generic.c
void init_time(t_philo *philo); void init_time(t_philo *philo);
void update_time(t_philo *philo); void update_time(t_philo *philo);
int diff_time(t_time old, struct timeval new); int diff_time(t_time *old, struct timeval *new);
int print_message(t_philo *philo, char *clr, char *msg); int print_message(t_philo *philo, char *clr, char *msg);
// generic.c // utils.c
int ft_atoi(const char *str); int ft_atoi(const char *str);
#endif #endif

View File

@@ -32,11 +32,10 @@ typedef struct s_params
typedef struct s_global typedef struct s_global
{ {
int dead; int stop;
int n_eat; int satiated_count;
t_time t_start; t_time t_start;
t_mtx m_print; t_mtx m_print;
t_mtx m_dead;
} t_global; } t_global;
typedef struct s_philo typedef struct s_philo
@@ -46,7 +45,7 @@ typedef struct s_philo
int p_nbr; int p_nbr;
t_mtx m_fork; t_mtx m_fork;
t_time t_last_meal; t_time t_last_meal;
int eat; int eat_count;
struct s_philo *next; struct s_philo *next;
} t_philo; } t_philo;

View File

@@ -3,29 +3,29 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* exec.c :+: :+: :+: */ /* exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */ /* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:32:57 by hulamy ### ########.fr */ /* Updated: 2022/01/26 16:29:49 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
void action_delay(t_philo *philo, int action_time) static void action_delay(t_philo *philo, int action_time)
{ {
struct timeval stime; struct timeval stime;
int time_to_death; int death_time;
gettimeofday(&stime, NULL); gettimeofday(&stime, NULL);
time_to_death = philo->params->t_die - diff_time(philo->t_last_meal, stime); death_time = philo->params->t_die - diff_time(&philo->t_last_meal, &stime);
if (time_to_death > action_time) if (death_time > action_time)
usleep(action_time * 1000); usleep(action_time * 1000);
else if (time_to_death > 0) else if (death_time > 0)
usleep(philo->params->t_die * 1000); usleep(philo->params->t_die * 1000);
} }
int ret_err_unlock(t_philo *philo, int nbr_fork) static int ret_err_unlock(t_philo *philo, int nbr_fork)
{ {
pthread_mutex_unlock(&philo->m_fork); pthread_mutex_unlock(&philo->m_fork);
if (nbr_fork == 2) if (nbr_fork == 2)
@@ -33,7 +33,7 @@ int ret_err_unlock(t_philo *philo, int nbr_fork)
return (1); return (1);
} }
int eat(t_philo *philo) static int eat(t_philo *philo)
{ {
pthread_mutex_lock(&(philo->m_fork)); pthread_mutex_lock(&(philo->m_fork));
if (print_message(philo, WHITE, "has taken a fork")) if (print_message(philo, WHITE, "has taken a fork"))
@@ -44,9 +44,9 @@ int eat(t_philo *philo)
update_time(philo); update_time(philo);
if (print_message(philo, B_YELLOW, "is eating")) if (print_message(philo, B_YELLOW, "is eating"))
return (ret_err_unlock(philo, 2)); return (ret_err_unlock(philo, 2));
philo->eat++; philo->eat_count++;
if (philo->eat == philo->params->n_eat) if (philo->eat_count == philo->params->n_eat)
philo->global->n_eat++; philo->global->satiated_count++;
action_delay(philo, philo->params->t_eat); action_delay(philo, philo->params->t_eat);
pthread_mutex_unlock(&(philo->next->m_fork)); pthread_mutex_unlock(&(philo->next->m_fork));
pthread_mutex_unlock(&(philo->m_fork)); pthread_mutex_unlock(&(philo->m_fork));

View File

@@ -3,39 +3,58 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* generic.c :+: :+: :+: */ /* generic.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */ /* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:31:15 by hulamy ### ########.fr */ /* Updated: 2022/01/26 16:29:10 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
int ft_atoi(const char *str) void init_time(t_philo *philo)
{ {
int i; struct timeval stime;
int result;
int is_negative;
is_negative = 0; if (philo->global->t_start.ts == 0)
result = 0;
i = 0;
while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '+')
i++;
else if (str[i] == '-')
{ {
is_negative = 1; gettimeofday(&stime, NULL);
i++; philo->global->t_start.ts = stime.tv_sec;
philo->global->t_start.tu = stime.tv_usec;
} }
while (str[i] >= '0' && str[i] <= '9') philo->t_last_meal.ts = philo->global->t_start.ts;
{ philo->t_last_meal.tu = philo->global->t_start.tu;
result = (result * 10) + (str[i] - '0'); }
i++;
} void update_time(t_philo *philo)
if (is_negative) {
result = result * -1; struct timeval stime;
return (result);
gettimeofday(&stime, NULL);
philo->t_last_meal.ts = stime.tv_sec;
philo->t_last_meal.tu = stime.tv_usec;
}
int diff_time(t_time *old, struct timeval *new)
{
return ((new->tv_sec - old->ts) * 1000 + (new->tv_usec - old->tu) / 1000);
}
int print_message(t_philo *philo, char *clr, char *msg)
{
long int time_stamp;
struct timeval stime;
pthread_mutex_lock(&(philo->global->m_print));
if (philo->global->stop)
{
pthread_mutex_unlock(&(philo->global->m_print));
return (1);
}
gettimeofday(&stime, NULL);
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
printf("%s%li %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
pthread_mutex_unlock(&(philo->global->m_print));
return (0);
} }

View File

@@ -3,16 +3,16 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* init.c :+: :+: :+: */ /* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */ /* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:30:35 by hulamy ### ########.fr */ /* Updated: 2022/01/26 16:29:38 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
t_philo *lst_add_philo(t_params *params, t_global *global, int i) static t_philo *lst_add_philo(t_params *params, t_global *global, int i)
{ {
t_philo *new; t_philo *new;
@@ -26,12 +26,12 @@ t_philo *lst_add_philo(t_params *params, t_global *global, int i)
return (NULL); return (NULL);
new->t_last_meal.ts = 0; new->t_last_meal.ts = 0;
new->t_last_meal.tu = 0; new->t_last_meal.tu = 0;
new->eat = 0; new->eat_count = 0;
new->next = NULL; new->next = NULL;
return (new); return (new);
} }
t_philo *init_chain_philo(t_params *params, t_global *global) static t_philo *init_chain_philo(t_params *params, t_global *global)
{ {
t_philo *philo; t_philo *philo;
t_philo *tmp; t_philo *tmp;
@@ -54,7 +54,7 @@ t_philo *init_chain_philo(t_params *params, t_global *global)
return (philo); return (philo);
} }
t_params *init_params(int ac, char **av) static t_params *init_params(int ac, char **av)
{ {
t_params *params; t_params *params;
@@ -76,21 +76,19 @@ t_params *init_params(int ac, char **av)
return (params); return (params);
} }
t_global *init_global(void) static t_global *init_global(void)
{ {
t_global *global; t_global *global;
global = malloc(sizeof(t_global)); global = malloc(sizeof(t_global));
if (!global) if (!global)
return (NULL); return (NULL);
global->dead = 0; global->stop = 0;
global->n_eat = 0; global->satiated_count = 0;
global->t_start.ts = 0; global->t_start.ts = 0;
global->t_start.tu = 0; global->t_start.tu = 0;
if (pthread_mutex_init(&(global->m_print), NULL) != 0) if (pthread_mutex_init(&(global->m_print), NULL) != 0)
return (NULL); return (NULL);
if (pthread_mutex_init(&(global->m_dead), NULL) != 0)
return (NULL);
return (global); return (global);
} }

View File

@@ -1,36 +1,34 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* pere_fouettard.c :+: :+: :+: */ /* launch.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */ /* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:31:40 by hulamy ### ########.fr */ /* Updated: 2022/01/26 16:29:13 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
int is_dead(t_philo *philo) static int is_finished(t_philo *philo, struct timeval *stime)
{ {
struct timeval stime;
long int time_stamp; long int time_stamp;
int time; int time;
if (philo->global->n_eat == philo->params->n_phi) if (philo->global->satiated_count == philo->params->n_phi)
{ {
philo->global->dead = 1; philo->global->stop = 1;
return (1); return (1);
} }
gettimeofday(&stime, NULL); time = diff_time(&philo->t_last_meal, stime);
time = diff_time(philo->t_last_meal, stime);
if (time >= philo->params->t_die) if (time >= philo->params->t_die)
{ {
pthread_mutex_lock(&(philo->global->m_print)); pthread_mutex_lock(&(philo->global->m_print));
philo->global->dead = 1; philo->global->stop = 1;
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; time_stamp = (stime->tv_sec - philo->global->t_start.ts) * 1000;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; time_stamp += (stime->tv_usec - philo->global->t_start.tu) / 1000;
printf("%s%li %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET); printf("%s%li %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET);
pthread_mutex_unlock(&(philo->global->m_print)); pthread_mutex_unlock(&(philo->global->m_print));
return (1); return (1);
@@ -38,7 +36,27 @@ int is_dead(t_philo *philo)
return (0); return (0);
} }
void launch_threads(t_philo *philo, pthread_t *id) static void pere_fouettard(t_philo *philo)
{
struct timeval stime;
int i;
while (philo->global->stop == 0)
{
i = 0;
gettimeofday(&stime, NULL);
while (i < philo->params->n_phi)
{
if (is_finished(philo, &stime))
break ;
philo = philo->next;
i++;
}
usleep(1 * 1000);
}
}
void launch(t_philo *philo, pthread_t *id)
{ {
int i; int i;
@@ -49,11 +67,7 @@ void launch_threads(t_philo *philo, pthread_t *id)
philo = philo->next; philo = philo->next;
i++; i++;
} }
while (philo->global->dead == 0) pere_fouettard(philo);
{
is_dead(philo);
philo = philo->next;
}
i = 0; i = 0;
while (i < philo->params->n_phi) while (i < philo->params->n_phi)
{ {

View File

@@ -21,7 +21,7 @@ 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);
launch_threads(philo, id); launch(philo, id);
i = 0; i = 0;
while (i < philo->params->n_phi) while (i < philo->params->n_phi)
{ {

View File

@@ -1,60 +1,41 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* utils.c :+: :+: :+: */ /* generic.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */ /* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:30:55 by hulamy ### ########.fr */ /* Updated: 2022/01/26 15:31:15 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
void init_time(t_philo *philo) int ft_atoi(const char *str)
{ {
struct timeval stime; int i;
int result;
int is_negative;
if (philo->global->t_start.ts == 0) is_negative = 0;
result = 0;
i = 0;
while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '+')
i++;
else if (str[i] == '-')
{ {
gettimeofday(&stime, NULL); is_negative = 1;
philo->global->t_start.ts = stime.tv_sec; i++;
philo->global->t_start.tu = stime.tv_usec;
} }
philo->t_last_meal.ts = philo->global->t_start.ts; while (str[i] >= '0' && str[i] <= '9')
philo->t_last_meal.tu = philo->global->t_start.tu;
}
void update_time(t_philo *philo)
{
struct timeval stime;
gettimeofday(&stime, NULL);
philo->t_last_meal.ts = stime.tv_sec;
philo->t_last_meal.tu = stime.tv_usec;
}
int diff_time(t_time old, struct timeval new)
{
return ((new.tv_sec - old.ts) * 1000 + (new.tv_usec - old.tu) / 1000);
}
int print_message(t_philo *philo, char *clr, char *msg)
{
long int time_stamp;
struct timeval stime;
pthread_mutex_lock(&(philo->global->m_print));
if (philo->global->dead)
{ {
pthread_mutex_unlock(&(philo->global->m_print)); result = (result * 10) + (str[i] - '0');
return (1); i++;
} }
gettimeofday(&stime, NULL); if (is_negative)
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; result = result * -1;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; return (result);
printf("%s%li %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
pthread_mutex_unlock(&(philo->global->m_print));
return (0);
} }