possible race conflicts resolut

This commit is contained in:
hugogogo
2022-01-30 15:53:45 +01:00
parent d9142b9af6
commit 7dc4c5a84f
7 changed files with 245 additions and 11 deletions

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */
/* Updated: 2022/01/26 18:51:44 by hulamy ### ########.fr */
/* Updated: 2022/01/30 15:43:19 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,7 +18,7 @@ static void action_delay(t_philo *philo, int action_time)
int death_time;
gettimeofday(&stime, NULL);
death_time = philo->params->t_die - diff_time(&philo->t_last_meal, &stime);
death_time = philo->params->t_die - diff_time(philo, &stime);
if (death_time > action_time)
usleep(action_time * 1000);
else if (death_time > 0)
@@ -45,7 +45,9 @@ static int eat(t_philo *philo)
if (print_message(philo, B_YELLOW, "is eating"))
return (ret_err_unlock(philo, 2));
action_delay(philo, philo->params->t_eat);
pthread_mutex_lock(&(philo->m_eat));
philo->eat_count++;
pthread_mutex_unlock(&(philo->m_eat));
pthread_mutex_unlock(&(philo->next->m_fork));
pthread_mutex_unlock(&(philo->m_fork));
return (0);

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */
/* Updated: 2022/01/26 16:29:10 by hulamy ### ########.fr */
/* Updated: 2022/01/30 15:47:44 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,28 +16,41 @@ void init_time(t_philo *philo)
{
struct timeval stime;
pthread_mutex_lock(&(philo->global->m_init_time));
if (philo->global->t_start.ts == 0)
{
gettimeofday(&stime, NULL);
philo->global->t_start.ts = stime.tv_sec;
philo->global->t_start.tu = stime.tv_usec;
}
pthread_mutex_lock(&(philo->m_time));
philo->t_last_meal.ts = philo->global->t_start.ts;
philo->t_last_meal.tu = philo->global->t_start.tu;
pthread_mutex_unlock(&(philo->m_time));
pthread_mutex_unlock(&(philo->global->m_init_time));
}
void update_time(t_philo *philo)
{
struct timeval stime;
pthread_mutex_lock(&(philo->m_time));
gettimeofday(&stime, NULL);
philo->t_last_meal.ts = stime.tv_sec;
philo->t_last_meal.tu = stime.tv_usec;
pthread_mutex_unlock(&(philo->m_time));
}
int diff_time(t_time *old, struct timeval *new)
int diff_time(t_philo *philo, struct timeval *new)
{
return ((new->tv_sec - old->ts) * 1000 + (new->tv_usec - old->tu) / 1000);
int t_diff;
t_time old;
pthread_mutex_lock(&philo->m_time);
old = philo->t_last_meal;
t_diff = (new->tv_sec - old.ts) * 1000 + (new->tv_usec - old.tu) / 1000;
pthread_mutex_unlock(&philo->m_time);
return (t_diff);
}
int print_message(t_philo *philo, char *clr, char *msg)
@@ -46,11 +59,14 @@ int print_message(t_philo *philo, char *clr, char *msg)
struct timeval stime;
pthread_mutex_lock(&(philo->global->m_print));
pthread_mutex_lock(&(philo->global->m_stop));
if (philo->global->stop)
{
pthread_mutex_unlock(&(philo->global->m_stop));
pthread_mutex_unlock(&(philo->global->m_print));
return (1);
}
pthread_mutex_unlock(&(philo->global->m_stop));
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;

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */
/* Updated: 2022/01/27 15:01:30 by hulamy ### ########.fr */
/* Updated: 2022/01/30 15:48:11 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,6 +24,10 @@ static t_philo *lst_add_philo(t_params *params, t_global *global, int i)
new->p_nbr = i + 1;
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
return (NULL);
if (pthread_mutex_init(&(new->m_time), NULL) != 0)
return (NULL);
if (pthread_mutex_init(&(new->m_eat), NULL) != 0)
return (NULL);
new->t_last_meal.ts = 0;
new->t_last_meal.tu = 0;
new->eat_count = 0;
@@ -97,6 +101,10 @@ static t_global *init_global(void)
global->t_start.tu = 0;
if (pthread_mutex_init(&(global->m_print), NULL) != 0)
return (NULL);
if (pthread_mutex_init(&(global->m_init_time), NULL) != 0)
return (NULL);
if (pthread_mutex_init(&(global->m_stop), NULL) != 0)
return (NULL);
return (global);
}

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */
/* Updated: 2022/01/26 18:52:32 by hulamy ### ########.fr */
/* Updated: 2022/01/30 15:50:27 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,10 +19,12 @@ static int is_finished(t_philo *philo, struct timeval *stime)
if (philo->global->satiated_count == philo->params->n_phi)
{
pthread_mutex_lock(&(philo->global->m_stop));
philo->global->stop = 1;
pthread_mutex_unlock(&(philo->global->m_stop));
return (1);
}
time = diff_time(&philo->t_last_meal, stime);
time = diff_time(philo, stime);
if (time >= philo->params->t_die)
{
pthread_mutex_lock(&(philo->global->m_print));
@@ -38,11 +40,14 @@ static int is_finished(t_philo *philo, struct timeval *stime)
int is_satiated(t_philo *philo)
{
pthread_mutex_lock(&(philo->m_eat));
if (philo->eat_count == philo->params->n_eat)
{
philo->global->satiated_count++;
pthread_mutex_unlock(&(philo->m_eat));
return (1);
}
pthread_mutex_unlock(&(philo->m_eat));
return (0);
}