mise a la norme et renommer fonctions

This commit is contained in:
Hugo LAMY
2022-01-26 15:33:29 +01:00
parent ab0c4efa35
commit b192efa4a9
13 changed files with 213 additions and 77 deletions

View File

@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:32:57 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void action_delay(t_philo *philo, int action_time)
@@ -45,7 +57,7 @@ void *philo_exec(void *arg)
{
t_philo *philo;
philo = (t_philo*)arg;
philo = (t_philo *)arg;
init_time(philo);
if (philo->p_nbr % 2 == 0)
usleep(10 * 1000);

View File

@@ -1,48 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* generic.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.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;
ft_printf("%s%i %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);
}

View File

@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:30:35 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
t_philo *lst_add_philo(t_params *params, t_global *global, int i)

View File

@@ -1,14 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:40 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:32:30 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int main(int ac, char **av)
{
pthread_t *id;
t_philo *philo;
int i;
int i;
philo = init(ac, av, &id);
if (philo == NULL)
return (0);
return (0);
launch_threads(philo, id);
i = 0;
while (i < philo->params->n_phi)
@@ -16,5 +28,5 @@ int main(int ac, char **av)
pthread_join(id[i], NULL);
i++;
}
return (0);
return (0);
}

View File

@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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)
@@ -17,10 +29,9 @@ int is_dead(t_philo *philo)
{
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;
ft_printf("%s%i %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));
return (1);
}

60
srcs/utils.c Normal file
View File

@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.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 */
/* */
/* ************************************************************************** */
#include "philo.h"
void init_time(t_philo *philo)
{
struct timeval stime;
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;
}
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)
{
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);
}