Files
42_INT_08_philosophers/srcs/launch.c
2022-01-26 16:31:36 +01:00

79 lines
2.2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* launch.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */
/* Updated: 2022/01/26 16:29:13 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
static int is_finished(t_philo *philo, struct timeval *stime)
{
long int time_stamp;
int time;
if (philo->global->satiated_count == philo->params->n_phi)
{
philo->global->stop = 1;
return (1);
}
time = diff_time(&philo->t_last_meal, stime);
if (time >= philo->params->t_die)
{
pthread_mutex_lock(&(philo->global->m_print));
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);
}
return (0);
}
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;
i = 0;
while (i < philo->params->n_phi)
{
pthread_create(&id[i], NULL, &philo_exec, philo);
philo = philo->next;
i++;
}
pere_fouettard(philo);
i = 0;
while (i < philo->params->n_phi)
{
pthread_mutex_unlock(&(philo->m_fork));
philo = philo->next;
i++;
}
}