mise a la norme et renommer fonctions
This commit is contained in:
64
srcs/pere_fouettard.c
Normal file
64
srcs/pere_fouettard.c
Normal file
@@ -0,0 +1,64 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pere_fouettard.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 15:31:40 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
int is_dead(t_philo *philo)
|
||||
{
|
||||
struct timeval stime;
|
||||
long int time_stamp;
|
||||
int time;
|
||||
|
||||
if (philo->global->n_eat == philo->params->n_phi)
|
||||
{
|
||||
philo->global->dead = 1;
|
||||
return (1);
|
||||
}
|
||||
gettimeofday(&stime, NULL);
|
||||
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;
|
||||
printf("%s%li %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET);
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void launch_threads(t_philo *philo, pthread_t *id)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
pthread_create(&id[i], NULL, &philo_exec, philo);
|
||||
philo = philo->next;
|
||||
i++;
|
||||
}
|
||||
while (philo->global->dead == 0)
|
||||
{
|
||||
is_dead(philo);
|
||||
philo = philo->next;
|
||||
}
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
pthread_mutex_unlock(&(philo->m_fork));
|
||||
philo = philo->next;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user