1
0

」 feat: parsing protection working

This commit is contained in:
2024-08-06 19:51:13 +02:00
parent 074ce180b7
commit dde531f8dd
8 changed files with 156 additions and 86 deletions

View File

@ -1,31 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* eat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/30 02:29:39 by adjoly #+# #+# */
/* Updated: 2024/07/31 21:53:13 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
bool eat(t_philo *philo)
{
bool death;
philo->state = FORK_TAKEN;
take_fork(&philo->fork, philo->id);
log_philo(*philo);
if (&(philo->fork.left) == philo->fork.right)
return (true);
take_fork(&philo->fork, philo->id + 1);
log_philo(*philo);
philo->state = EAT;
gettimeofday(&(philo->eat), NULL);
log_philo(*philo);
death = sleep_phil(*philo);
return (death);
}

View File

@ -6,34 +6,90 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 15:30:46 by adjoly #+# #+# */ /* Created: 2024/07/08 15:30:46 by adjoly #+# #+# */
/* Updated: 2024/07/31 17:52:08 by adjoly ### ########.fr */ /* Updated: 2024/08/06 19:48:54 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
#include "philo_msg.h"
bool print_err(uint8_t error)
{
if (error == 0)
printf(ERR_MAX_PHIL);
else if (error == 1)
printf(ERR_MAX_DIE_TIME);
else if (error == 2)
printf(ERR_MAX_EAT_TIME);
else if (error == 3)
printf(ERR_MAX_SLEEP_TIME);
else if (error == 4)
printf(ERR_MAX_MEAL);
else if (error == 5)
printf(ERR_NB_ARG);
return (true);
}
bool check_av(char **av)
{
if (!av)
return (print_err(5));
if (ft_strlen(av[0]) > 3)
return (print_err(0));
if (ft_strlen(av[1]) > 11)
return (print_err(1));
if (ft_strlen(av[2]) > 11)
return (print_err(2));
if (ft_strlen(av[3]) > 11)
return (print_err(3));
if (av[4] && ft_strlen(av[4]) > 4)
return (print_err(4));
return (false);
}
t_pdata ret_err(t_pdata data, uint8_t error)
{
print_err(error);
data.error = true;
return (data);
}
t_pdata fill_pdata(char **av) t_pdata fill_pdata(char **av)
{ {
t_pdata data; t_pdata data;
if (!av) data.philo_nbr = 0;
if (!av && check_av(av))
return (ret_err(data, 255));
data.philo_nbr = ft_atoll(av[0]);
if (data.philo_nbr > 200)
return (ret_err(data, 0));
data.die_time = ft_atoll(av[1]);
if (data.die_time > 2147483647)
return (ret_err(data, 1));
data.eat_time = ft_atoll(av[2]);
if (data.eat_time > 2147483647)
return (ret_err(data, 2));
data.sleep_time = ft_atoll(av[3]);
if (data.sleep_time > 2147483647)
return (ret_err(data, 3));
if (av[4])
{ {
data.error = true; data.meal_nbr = ft_atoll(av[4]);
return (data); if (data.meal_nbr > 1000)
return (ret_err(data, 4));
} }
data.philo_nbr = ft_atoll(av[1]);
data.die_time = ft_atoll(av[2]);
data.eat_time = ft_atoll(av[3]);
data.sleep_time = ft_atoll(av[4]);
if (av[5])
data.meal_nbr = ft_atoll(av[5]);
data.error = false; data.error = false;
return (data); return (data);
} }
t_pdata philo_parse(char **av, int ac) t_pdata philo_parse(char **av, int ac)
{ {
t_pdata data;
data.philo_nbr = 0;
if (!(ac > 4 && ac <= 6)) if (!(ac > 4 && ac <= 6))
return (fill_pdata(NULL)); return (ret_err(data, 5));
return (fill_pdata(av)); return (fill_pdata(av));
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 15:10:29 by adjoly #+# #+# */ /* Created: 2024/07/07 15:10:29 by adjoly #+# #+# */
/* Updated: 2024/07/31 15:59:39 by adjoly ### ########.fr */ /* Updated: 2024/08/06 19:46:32 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,7 +20,7 @@ int main(int ac, char **av)
{ {
t_pdata data; t_pdata data;
data = philo_parse(av, ac); data = philo_parse(av + 1, ac);
if (data.error == true) if (data.error == true)
return (EXIT_FAILURE); return (EXIT_FAILURE);
init_fork(data); init_fork(data);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 15:11:02 by adjoly #+# #+# */ /* Created: 2024/07/07 15:11:02 by adjoly #+# #+# */
/* Updated: 2024/07/31 21:55:13 by adjoly ### ########.fr */ /* Updated: 2024/08/06 19:22:09 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -64,14 +64,15 @@ typedef struct s_philo
/** /**
* Utils * Utils
*/ */
t_pdata philo_parse(char **argv, int ac); size_t ft_strlen(char *s);
uint16_t get_meal_nb(uint16_t meal_nbr, bool no_meal);
bool get_death(bool in, bool ret);
long long ft_atoll(const char *nptr); long long ft_atoll(const char *nptr);
uint32_t get_time_in_ms(struct timeval t0, struct timeval t1); uint32_t get_time_in_ms(struct timeval t0, struct timeval t1);
void log_philo(t_philo philo);
bool get_death(bool in, bool ret);
bool sleep_phil(t_philo philo);
uint16_t get_meal_nb(uint16_t meal_nbr, bool no_meal);
void log_philo(t_philo philo);
bool sleep_phil(t_philo *philo);
t_pdata philo_parse(char **argv, int ac);
/** /**
* Main path * Main path
* by order of call * by order of call

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 17:50:46 by adjoly #+# #+# */ /* Created: 2024/07/07 17:50:46 by adjoly #+# #+# */
/* Updated: 2024/07/07 17:53:53 by adjoly ### ########.fr */ /* Updated: 2024/08/06 19:47:17 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,4 +19,11 @@
# define DIED_MSG "died\n" # define DIED_MSG "died\n"
# define FORK_MSG "has taken a fork\n" # define FORK_MSG "has taken a fork\n"
# define ERR_MAX_PHIL "Too much philo < 200\n" // err 0
# define ERR_MAX_DIE_TIME "Time to die too long < INT_MAX\n" // err 1
# define ERR_MAX_EAT_TIME "Time to eat too long < INT_MAX\n" // err 2
# define ERR_MAX_SLEEP_TIME "Time to sleep too long < INT_MAX\n" // err 3
# define ERR_MAX_MEAL "Too much meal < 1000\n" // err 4
# define ERR_NB_ARG "Invalid number of args 4 or 5\n" // err 5
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/22 21:24:53 by adjoly #+# #+# */ /* Created: 2024/07/22 21:24:53 by adjoly #+# #+# */
/* Updated: 2024/07/31 22:10:31 by adjoly ### ########.fr */ /* Updated: 2024/08/06 18:55:55 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,23 +32,46 @@ void *philo_routine(void *content)
philo->meal_left = get_meal_nb(philo->data.meal_nbr, philo->data.no_meal); philo->meal_left = get_meal_nb(philo->data.meal_nbr, philo->data.no_meal);
while (1) while (1)
{ {
if (eat(philo) == true) philo->state = FORK_TAKEN;
take_fork(&philo->fork, philo->id);
pthread_mutex_lock(philo->check);
death = get_death(false, true);
pthread_mutex_unlock(philo->check);
if (death == true)
break ;
log_philo(*philo);
if (&(philo->fork.left) == philo->fork.right)
{
philo->state = DEAD;
sleep_phil(philo);
log_philo(*philo);
return (NULL);
}
take_fork(&philo->fork, philo->id + 1);
pthread_mutex_lock(philo->check);
death = get_death(false, true);
pthread_mutex_unlock(philo->check);
if (death == true)
break ;
log_philo(*philo);
philo->state = EAT;
gettimeofday(&(philo->eat), NULL);
log_philo(*philo);
if (sleep_phil(philo) == true)
break ; break ;
// unlock pas les fourchet si po la
pthread_mutex_unlock(&philo->fork.left); pthread_mutex_unlock(&philo->fork.left);
pthread_mutex_unlock(philo->fork.right); pthread_mutex_unlock(philo->fork.right);
if (philo->data.no_meal == false)
philo->meal_left--; philo->meal_left--;
philo->state = SLEEP; philo->state = SLEEP;
log_philo(*philo); log_philo(*philo);
death = sleep_phil(*philo); sleep_phil(philo);
if (death == true)
break ;
if (!philo->meal_left)
return (NULL);
philo->state = THINK; philo->state = THINK;
log_philo(*philo); log_philo(*philo);
if (philo->meal_left == 0)
return (NULL);
} }
pthread_mutex_unlock(&philo->fork.left);
pthread_mutex_unlock(philo->fork.right);
philo->state = DEAD; philo->state = DEAD;
log_philo(*philo); log_philo(*philo);
return (NULL); return (NULL);

View File

@ -6,46 +6,50 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */ /* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */
/* Updated: 2024/07/30 02:48:34 by adjoly ### ########.fr */ /* Updated: 2024/08/06 18:45:19 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
#define SLEEP_SLICE_MS 10 #define SLEEP_SLICE_MS 2
bool sleep_phil(t_philo philo) uint32_t get_current_time(void)
{
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000 + time.tv_usec / 1000);
}
bool sleep_phil(t_philo *philo)
{ {
struct timeval t1; struct timeval t1;
uint32_t sleep_time; uint32_t sleep_time;
bool death;
sleep_time = 0; sleep_time = 0;
if (philo.state == EAT) if (philo->state == EAT)
sleep_time = philo.data.eat_time; sleep_time = philo->data.eat_time + get_current_time();
else if (philo.state == SLEEP) else if (philo->state == SLEEP)
sleep_time = philo.data.sleep_time; sleep_time = philo->data.sleep_time + get_current_time();
while (sleep_time) else if (philo->state == DEAD)
sleep_time = philo->data.die_time + get_current_time();
while (get_current_time() < sleep_time)
{ {
pthread_mutex_lock(philo.check); pthread_mutex_lock(philo->check);
if (get_death(false, true) == true) death = get_death(false, true);
{ pthread_mutex_unlock(philo->check);
pthread_mutex_unlock(philo.check); if (death == true)
return (true); return (true);
}
pthread_mutex_unlock(philo.check);
gettimeofday(&t1, NULL); gettimeofday(&t1, NULL);
if (get_time_in_ms(philo.eat, t1) > philo.data.die_time) if (get_time_in_ms(philo->eat, t1) > philo->data.die_time)
{ {
pthread_mutex_lock(philo.check); pthread_mutex_lock(philo->check);
get_death(true, false); get_death(true, false);
pthread_mutex_unlock(philo.check); pthread_mutex_unlock(philo->check);
pthread_mutex_unlock(&philo.fork.left);
pthread_mutex_unlock(philo.fork.right);
philo.state = DEAD;
log_philo(philo);
return (true); return (true);
} }
usleep(SLEEP_SLICE_MS * 1000); usleep(SLEEP_SLICE_MS * 1000);
sleep_time -= SLEEP_SLICE_MS;
} }
return (false); return (false);
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/31 21:20:49 by adjoly #+# #+# */ /* Created: 2024/07/31 21:20:49 by adjoly #+# #+# */
/* Updated: 2024/07/31 21:55:16 by adjoly ### ########.fr */ /* Updated: 2024/08/06 19:16:42 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,3 +19,13 @@ uint16_t get_meal_nb(uint16_t meal_nbr, bool no_meal)
else else
return (meal_nbr); return (meal_nbr);
} }
size_t ft_strlen(char *s)
{
char *tmp;
tmp = s;
while(*tmp)
tmp++;
return (tmp - s);
}