1
0

」 feat: nearly working to_do: implement all philo eat

This commit is contained in:
2024-08-08 18:48:47 +02:00
parent dde531f8dd
commit ec9ee12c29
12 changed files with 149 additions and 174 deletions

View File

@ -1,18 +0,0 @@
#include "philo.h"
#include <stdio.h>
void print_philo_data(t_pdata data)
{
if (data.error == true)
{
printf("error = true\n");
return ;
}
else
printf("error = false\n");
printf("die_time = %u\n", data.die_time);
printf("sleep_time = %u\n", data.sleep_time);
printf("eat_time = %u\n", data.eat_time);
printf("philo_nbr = %u\n", data.philo_nbr);
printf("meal_nbr = %u\n", data.meal_nbr);
}

View File

@ -1,22 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_death.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/24 23:03:08 by adjoly #+# #+# */
/* Updated: 2024/07/26 17:32:52 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
bool get_death(bool in, bool ret)
{
static bool death;
if (ret == false)
death = in;
return (death);
}

37
philo/eat.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* eat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/08 17:29:51 by adjoly #+# #+# */
/* Updated: 2024/08/08 18:05:07 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
bool philo_eat(t_philo *philo)
{
philo->state = FORK_TAKEN;
take_fork(&philo->fork, philo->id);
if (get_death(false, true, philo))
return (true);
log_philo(philo);
if (&(philo->fork.left) == philo->fork.right)
return (true);
take_fork(&philo->fork, philo->id + 1);
if (get_death(false, true, philo))
return (true);
log_philo(philo);
philo->state = EAT;
gettimeofday(&(philo->eat), NULL);
log_philo(philo);
if (sleep_phil(philo) == true)
return (true);
pthread_mutex_unlock(&philo->fork.left);
pthread_mutex_unlock(philo->fork.right);
philo->meal_left--;
return (false);
}

View File

@ -1,35 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoll.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 17:29:13 by adjoly #+# #+# */
/* Updated: 2024/07/08 17:29:15 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
long long ft_atoll(const char *nptr)
{
char sign;
long long nbr;
sign = 1;
nbr = 0;
while ((*nptr >= 7 && *nptr <= 13) || *nptr == 32)
nptr++;
if (*nptr == '-')
{
sign *= -1;
nptr++;
}
else if (*nptr == '+')
nptr++;
while (*nptr >= '0' && *nptr <= '9')
{
nbr = nbr * 10 + (*nptr - '0');
nptr++;
}
return (nbr * sign);
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 14:36:59 by adjoly #+# #+# */ /* Created: 2024/07/11 14:36:59 by adjoly #+# #+# */
/* Updated: 2024/07/31 19:00:58 by adjoly ### ########.fr */ /* Updated: 2024/08/08 18:07:47 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,7 +23,8 @@ void init_philo(t_pdata data, t_philo *philo)
i = 0; i = 0;
pthread_mutex_init(&check, NULL); pthread_mutex_init(&check, NULL);
get_death(false, false); philo[i].check = &check;
get_death(false, false, philo);
gettimeofday(&time, NULL); gettimeofday(&time, NULL);
while (i < data.philo_nbr) while (i < data.philo_nbr)
{ {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */ /* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */
/* Updated: 2024/07/24 23:56:07 by adjoly ### ########.fr */ /* Updated: 2024/08/08 18:01:52 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,24 +14,24 @@
#include "philo_msg.h" #include "philo_msg.h"
#include <stdio.h> #include <stdio.h>
void log_philo(t_philo philo) void log_philo(t_philo *philo)
{ {
uint32_t timestamp; uint32_t timestamp;
struct timeval t1; struct timeval t1;
static pthread_mutex_t print = {0}; static pthread_mutex_t print = {0};
gettimeofday(&t1, NULL); gettimeofday(&t1, NULL);
timestamp = get_time_in_ms(philo.t0, t1); timestamp = get_time_in_ms(philo->t0, t1);
pthread_mutex_lock(&print); pthread_mutex_lock(&print);
if (philo.state == EAT) if (philo->state == EAT)
printf("%u %hu %s", timestamp, philo.id, EATING_MSG); printf("%u %hu %s", timestamp, philo->id, EATING_MSG);
else if (philo.state == THINK) else if (philo->state == THINK)
printf("%u %hu %s", timestamp, philo.id, THINK_MSG); printf("%u %hu %s", timestamp, philo->id, THINK_MSG);
else if (philo.state == SLEEP) else if (philo->state == SLEEP)
printf("%u %hu %s", timestamp, philo.id, SLEEP_MSG); printf("%u %hu %s", timestamp, philo->id, SLEEP_MSG);
else if (philo.state == DEAD) else if (philo->state == DEAD)
printf("%u %hu %s", timestamp, philo.id, DIED_MSG); printf("%u %hu %s", timestamp, philo->id, DIED_MSG);
else if (philo.state == FORK_TAKEN) else if (philo->state == FORK_TAKEN)
printf("%u %hu %s", timestamp, philo.id, FORK_MSG); printf("%u %hu %s", timestamp, philo->id, FORK_MSG);
pthread_mutex_unlock(&print); pthread_mutex_unlock(&print);
} }

View File

@ -6,7 +6,7 @@
/* 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/08/06 19:48:54 by adjoly ### ########.fr */ /* Updated: 2024/08/08 17:26:05 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,7 +47,6 @@ bool check_av(char **av)
return (false); return (false);
} }
t_pdata ret_err(t_pdata data, uint8_t error) t_pdata ret_err(t_pdata data, uint8_t error)
{ {
print_err(error); print_err(error);

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/08/06 19:22:09 by adjoly ### ########.fr */ /* Updated: 2024/08/08 18:12:39 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -66,13 +66,16 @@ typedef struct s_philo
*/ */
size_t ft_strlen(char *s); size_t ft_strlen(char *s);
uint16_t get_meal_nb(uint16_t meal_nbr, bool no_meal); uint16_t get_meal_nb(uint16_t meal_nbr, bool no_meal);
bool get_death(bool in, bool ret); bool get_death(bool in, bool ret, t_philo *philo);
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);
uint32_t get_current_time(void);
void log_philo(t_philo philo); void log_philo(t_philo *philo);
bool sleep_phil(t_philo *philo); bool sleep_phil(t_philo *philo);
t_pdata philo_parse(char **argv, int ac); t_pdata philo_parse(char **argv, int ac);
bool print_death(t_philo *philo);
bool philo_eat(t_philo *philo);
/** /**
* Main path * Main path
* by order of call * by order of call

View File

@ -6,15 +6,14 @@
/* 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/08/06 18:55:55 by adjoly ### ########.fr */ /* Updated: 2024/08/08 18:37:39 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
#include <stdio.h> #include <stdio.h>
void take_fork(t_fork *fork, int id)
void take_fork(t_fork *fork, int id)
{ {
if (id % 2 && !(&fork->left == fork->right)) if (id % 2 && !(&fork->left == fork->right))
pthread_mutex_lock(&fork->left); pthread_mutex_lock(&fork->left);
@ -25,54 +24,26 @@ void take_fork(t_fork *fork, int id)
void *philo_routine(void *content) void *philo_routine(void *content)
{ {
t_philo *philo; t_philo *philo;
bool death;
philo = content; philo = content;
gettimeofday(&(philo->eat), NULL); gettimeofday(&(philo->eat), NULL);
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);
if (!(philo->id % 2))
usleep(10);
while (1) while (1)
{ {
philo->state = FORK_TAKEN; if (philo_eat(philo) == true)
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 ; 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 ;
pthread_mutex_unlock(&philo->fork.left);
pthread_mutex_unlock(philo->fork.right);
philo->meal_left--;
philo->state = SLEEP;
log_philo(*philo);
sleep_phil(philo);
philo->state = THINK;
log_philo(*philo);
if (philo->meal_left == 0) if (philo->meal_left == 0)
return (NULL); return (NULL);
philo->state = SLEEP;
log_philo(philo);
if (sleep_phil(philo))
break ;
philo->state = THINK;
log_philo(philo);
} }
pthread_mutex_unlock(&philo->fork.left); pthread_mutex_unlock(&philo->fork.left);
pthread_mutex_unlock(philo->fork.right); pthread_mutex_unlock(philo->fork.right);
philo->state = DEAD;
log_philo(*philo);
return (NULL); return (NULL);
} }

View File

@ -6,48 +6,39 @@
/* 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/08/06 18:45:19 by adjoly ### ########.fr */ /* Updated: 2024/08/08 18:06:32 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
#define SLEEP_SLICE_MS 2 #define SLEEP_SLICE_MS 5
uint32_t get_current_time(void) uint32_t get_sleep_time(t_philo *philo)
{ {
struct timeval time; if (philo->state == EAT)
return (philo->data.eat_time + get_current_time());
gettimeofday(&time, NULL); else if (philo->state == SLEEP)
return (time.tv_sec * 1000 + time.tv_usec / 1000); return (philo->data.sleep_time + get_current_time());
else if (philo->state == DEAD)
return (philo->data.die_time + get_current_time());
return (0);
} }
bool sleep_phil(t_philo *philo) 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 = get_sleep_time(philo);
if (philo->state == EAT)
sleep_time = philo->data.eat_time + get_current_time();
else if (philo->state == SLEEP)
sleep_time = philo->data.sleep_time + get_current_time();
else if (philo->state == DEAD)
sleep_time = philo->data.die_time + get_current_time();
while (get_current_time() < sleep_time) while (get_current_time() < sleep_time)
{ {
pthread_mutex_lock(philo->check); if (get_death(false, true, philo))
death = get_death(false, true);
pthread_mutex_unlock(philo->check);
if (death == true)
return (true); return (true);
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); get_death(true, false, philo);
get_death(true, false); return (print_death(philo));
pthread_mutex_unlock(philo->check);
return (true);
} }
usleep(SLEEP_SLICE_MS * 1000); usleep(SLEEP_SLICE_MS * 1000);
} }

View File

@ -1,21 +1,53 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* get_time_in_ms.c :+: :+: :+: */ /* util_philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/09 12:02:24 by adjoly #+# #+# */ /* Created: 2024/08/08 18:16:45 by adjoly #+# #+# */
/* Updated: 2024/07/31 20:35:51 by adjoly ### ########.fr */ /* Updated: 2024/08/08 18:16:53 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <sys/time.h>
#include <stdint.h>
#include "philo.h" #include "philo.h"
uint16_t get_meal_nb(uint16_t meal_nbr, bool no_meal)
{
if (no_meal == true)
return (1);
else
return (meal_nbr);
}
uint32_t get_current_time(void)
{
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000 + time.tv_usec / 1000);
}
bool get_death(bool in, bool ret, t_philo *philo)
{
static bool death;
pthread_mutex_lock(philo->check);
if (ret == false)
death = in;
pthread_mutex_unlock(philo->check);
return (death);
}
uint32_t get_time_in_ms(struct timeval t0, struct timeval t1) uint32_t get_time_in_ms(struct timeval t0, struct timeval t1)
{ {
return (((t1.tv_sec - t0.tv_sec) * 1000000 + \ return (((t1.tv_sec - t0.tv_sec) * 1000000 + \
t1.tv_usec - t0.tv_usec) / 1000); t1.tv_usec - t0.tv_usec) / 1000);
}
bool print_death(t_philo *philo)
{
philo->state = DEAD;
log_philo(philo);
return (true);
} }

View File

@ -6,26 +6,42 @@
/* 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/08/06 19:16:42 by adjoly ### ########.fr */ /* Updated: 2024/08/08 18:17:09 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
uint16_t get_meal_nb(uint16_t meal_nbr, bool no_meal)
{
if (no_meal == true)
return (1);
else
return (meal_nbr);
}
size_t ft_strlen(char *s) size_t ft_strlen(char *s)
{ {
char *tmp; char *tmp;
tmp = s; tmp = s;
while(*tmp) while (*tmp)
tmp++; tmp++;
return (tmp - s); return (tmp - s);
} }
long long ft_atoll(const char *nptr)
{
char sign;
long long nbr;
sign = 1;
nbr = 0;
while ((*nptr >= 7 && *nptr <= 13) || *nptr == 32)
nptr++;
if (*nptr == '-')
{
sign *= -1;
nptr++;
}
else if (*nptr == '+')
nptr++;
while (*nptr >= '0' && *nptr <= '9')
{
nbr = nbr * 10 + (*nptr - '0');
nptr++;
}
return (nbr * sign);
}