ajout static, renames, pere fouettard tweaks
This commit is contained in:
24
srcs/exec.c
24
srcs/exec.c
@@ -3,29 +3,29 @@
|
||||
/* ::: :::::::: */
|
||||
/* exec.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
void action_delay(t_philo *philo, int action_time)
|
||||
static void action_delay(t_philo *philo, int action_time)
|
||||
{
|
||||
struct timeval stime;
|
||||
int time_to_death;
|
||||
int death_time;
|
||||
|
||||
gettimeofday(&stime, NULL);
|
||||
time_to_death = philo->params->t_die - diff_time(philo->t_last_meal, stime);
|
||||
if (time_to_death > action_time)
|
||||
death_time = philo->params->t_die - diff_time(&philo->t_last_meal, &stime);
|
||||
if (death_time > action_time)
|
||||
usleep(action_time * 1000);
|
||||
else if (time_to_death > 0)
|
||||
else if (death_time > 0)
|
||||
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);
|
||||
if (nbr_fork == 2)
|
||||
@@ -33,7 +33,7 @@ int ret_err_unlock(t_philo *philo, int nbr_fork)
|
||||
return (1);
|
||||
}
|
||||
|
||||
int eat(t_philo *philo)
|
||||
static int eat(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->m_fork));
|
||||
if (print_message(philo, WHITE, "has taken a fork"))
|
||||
@@ -44,9 +44,9 @@ int eat(t_philo *philo)
|
||||
update_time(philo);
|
||||
if (print_message(philo, B_YELLOW, "is eating"))
|
||||
return (ret_err_unlock(philo, 2));
|
||||
philo->eat++;
|
||||
if (philo->eat == philo->params->n_eat)
|
||||
philo->global->n_eat++;
|
||||
philo->eat_count++;
|
||||
if (philo->eat_count == philo->params->n_eat)
|
||||
philo->global->satiated_count++;
|
||||
action_delay(philo, philo->params->t_eat);
|
||||
pthread_mutex_unlock(&(philo->next->m_fork));
|
||||
pthread_mutex_unlock(&(philo->m_fork));
|
||||
|
||||
@@ -3,39 +3,58 @@
|
||||
/* ::: :::::::: */
|
||||
/* generic.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 15:31:15 by hulamy ### ########.fr */
|
||||
/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 16:29:10 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
void init_time(t_philo *philo)
|
||||
{
|
||||
int i;
|
||||
int result;
|
||||
int is_negative;
|
||||
struct timeval stime;
|
||||
|
||||
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] == '-')
|
||||
if (philo->global->t_start.ts == 0)
|
||||
{
|
||||
is_negative = 1;
|
||||
i++;
|
||||
gettimeofday(&stime, NULL);
|
||||
philo->global->t_start.ts = stime.tv_sec;
|
||||
philo->global->t_start.tu = stime.tv_usec;
|
||||
}
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
{
|
||||
result = (result * 10) + (str[i] - '0');
|
||||
i++;
|
||||
}
|
||||
if (is_negative)
|
||||
result = result * -1;
|
||||
return (result);
|
||||
philo->t_last_meal.ts = philo->global->t_start.ts;
|
||||
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->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);
|
||||
}
|
||||
|
||||
20
srcs/init.c
20
srcs/init.c
@@ -3,16 +3,16 @@
|
||||
/* ::: :::::::: */
|
||||
/* init.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
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;
|
||||
|
||||
@@ -26,12 +26,12 @@ t_philo *lst_add_philo(t_params *params, t_global *global, int i)
|
||||
return (NULL);
|
||||
new->t_last_meal.ts = 0;
|
||||
new->t_last_meal.tu = 0;
|
||||
new->eat = 0;
|
||||
new->eat_count = 0;
|
||||
new->next = NULL;
|
||||
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 *tmp;
|
||||
@@ -54,7 +54,7 @@ t_philo *init_chain_philo(t_params *params, t_global *global)
|
||||
return (philo);
|
||||
}
|
||||
|
||||
t_params *init_params(int ac, char **av)
|
||||
static t_params *init_params(int ac, char **av)
|
||||
{
|
||||
t_params *params;
|
||||
|
||||
@@ -76,21 +76,19 @@ t_params *init_params(int ac, char **av)
|
||||
return (params);
|
||||
}
|
||||
|
||||
t_global *init_global(void)
|
||||
static t_global *init_global(void)
|
||||
{
|
||||
t_global *global;
|
||||
|
||||
global = malloc(sizeof(t_global));
|
||||
if (!global)
|
||||
return (NULL);
|
||||
global->dead = 0;
|
||||
global->n_eat = 0;
|
||||
global->stop = 0;
|
||||
global->satiated_count = 0;
|
||||
global->t_start.ts = 0;
|
||||
global->t_start.tu = 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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 #+# #+# */
|
||||
/* Updated: 2022/01/26 15:31:40 by hulamy ### ########.fr */
|
||||
/* Updated: 2022/01/26 16:29:13 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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;
|
||||
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);
|
||||
}
|
||||
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)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->global->m_print));
|
||||
philo->global->dead = 1;
|
||||
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
|
||||
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
|
||||
philo->global->stop = 1;
|
||||
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 died%s\n", B_RED, time_stamp, philo->p_nbr, RESET);
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
return (1);
|
||||
@@ -38,7 +36,27 @@ int is_dead(t_philo *philo)
|
||||
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;
|
||||
|
||||
@@ -49,11 +67,7 @@ void launch_threads(t_philo *philo, pthread_t *id)
|
||||
philo = philo->next;
|
||||
i++;
|
||||
}
|
||||
while (philo->global->dead == 0)
|
||||
{
|
||||
is_dead(philo);
|
||||
philo = philo->next;
|
||||
}
|
||||
pere_fouettard(philo);
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
@@ -21,7 +21,7 @@ int main(int ac, char **av)
|
||||
philo = init(ac, av, &id);
|
||||
if (philo == NULL)
|
||||
return (0);
|
||||
launch_threads(philo, id);
|
||||
launch(philo, id);
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
|
||||
65
srcs/utils.c
65
srcs/utils.c
@@ -1,60 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* generic.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 15:30:55 by hulamy ### ########.fr */
|
||||
/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 15:31:15 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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);
|
||||
philo->global->t_start.ts = stime.tv_sec;
|
||||
philo->global->t_start.tu = stime.tv_usec;
|
||||
is_negative = 1;
|
||||
i++;
|
||||
}
|
||||
philo->t_last_meal.ts = philo->global->t_start.ts;
|
||||
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)
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
{
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
return (1);
|
||||
result = (result * 10) + (str[i] - '0');
|
||||
i++;
|
||||
}
|
||||
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);
|
||||
if (is_negative)
|
||||
result = result * -1;
|
||||
return (result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user