1
0

🏗️」 wip: testing things, might broke.

This commit is contained in:
2024-08-06 17:42:36 +02:00
parent 930867803d
commit 074ce180b7
13 changed files with 149 additions and 92 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/24 23:03:08 by adjoly #+# #+# */ /* Created: 2024/07/24 23:03:08 by adjoly #+# #+# */
/* Updated: 2024/07/24 23:05:15 by adjoly ### ########.fr */ /* Updated: 2024/07/26 17:32:52 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
bool get_death(bool in, bool ret) bool get_death(bool in, bool ret)
{ {
static bool death; static bool death;
if (ret == false) if (ret == false)
death = in; death = in;
return (death); return (death);

31
philo/eat.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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

@ -1,18 +1,18 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* start_philo.c :+: :+: :+: */ /* end_philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/24 18:19:14 by adjoly #+# #+# */ /* Created: 2024/07/24 18:19:14 by adjoly #+# #+# */
/* Updated: 2024/07/25 16:46:57 by adjoly ### ########.fr */ /* Updated: 2024/07/26 16:50:54 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
void start_philo(t_init *init, uint16_t philo_nbr) void end_philo(pthread_t *thread, uint16_t philo_nbr)
{ {
uint16_t i; uint16_t i;
int r; int r;
@ -20,7 +20,7 @@ void start_philo(t_init *init, uint16_t philo_nbr)
i = 0; i = 0;
while (i < philo_nbr) while (i < philo_nbr)
{ {
r = pthread_join(init[i].thread, NULL); r = pthread_join(thread[i], NULL);
if (r != 0) if (r != 0)
return ; return ;
i++; i++;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/09 12:02:24 by adjoly #+# #+# */ /* Created: 2024/07/09 12:02:24 by adjoly #+# #+# */
/* Updated: 2024/07/24 23:51:20 by adjoly ### ########.fr */ /* Updated: 2024/07/31 20:35:51 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
#include <stdint.h> #include <stdint.h>
#include "philo.h" #include "philo.h"
uint16_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);

View File

@ -6,24 +6,24 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/22 15:29:08 by adjoly #+# #+# */ /* Created: 2024/07/22 15:29:08 by adjoly #+# #+# */
/* Updated: 2024/07/26 15:24:07 by adjoly ### ########.fr */ /* Updated: 2024/07/31 17:40:57 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
#include <stdlib.h>
void init_fork(t_pdata data) void init_fork(t_pdata data)
{ {
uint16_t i; uint8_t i;
t_init init[PHILO_MAX]; t_philo philo[PHILO_MAX];
i = 0; i = 0;
while (i < data.philo_nbr) while (i < data.philo_nbr)
{ {
pthread_mutex_init(&(init[i].data.fork.left), NULL); pthread_mutex_init(&(philo[i].fork.left), NULL);
init[i].data.fork.right = &init[(i - 1) % data.philo_nbr].data.fork.left; philo[i].fork.right = &philo[(i + 1) % data.philo_nbr].fork.left;
i++; i++;
} }
init_philo(data, init); init_philo(data, philo);
} }

View File

@ -6,33 +6,35 @@
/* 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/26 15:25:58 by adjoly ### ########.fr */ /* Updated: 2024/07/31 19:00:58 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
#include <stdlib.h> #include <stdlib.h>
void init_philo(t_pdata data, t_init *init) void init_philo(t_pdata data, t_philo *philo)
{ {
int r; int r;
pthread_mutex_t check; pthread_mutex_t check;
uint16_t i; pthread_t thread[PHILO_MAX];
struct timeval time;
uint8_t i;
i = 0; i = 0;
pthread_mutex_init(&check, NULL); pthread_mutex_init(&check, NULL);
get_death(false, false); get_death(false, false);
gettimeofday(&time, NULL);
while (i < data.philo_nbr) while (i < data.philo_nbr)
{ {
init[i].data.id = i + 1; philo[i].id = i + 1;
init[i].data.state = SLEEP; philo[i].data = data;
init[i].data.data = data; philo[i].check = &check;
init[i].data.check = &check; philo[i].t0 = time;
r = pthread_create(&init[i].thread, NULL, r = pthread_create(&thread[i], NULL, philo_routine, &philo[i]);
philo_routine, &(init[i].data));
if (r != 0) if (r != 0)
break ; break ;
i++; i++;
} }
start_philo(init, i); end_philo(thread, data.philo_nbr);
} }

View File

@ -1,15 +0,0 @@
EAT
lock left
lock right
geteattime
print
sleep time_to_eat ms
SLEEP
print
sleep time_to_sleep ms
THINK
print
REPEAT

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/07/21 21:23:35 by adjoly ### ########.fr */ /* Updated: 2024/07/31 17:52:08 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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/26 15:23:45 by adjoly ### ########.fr */ /* Updated: 2024/07/31 15:59:39 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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/26 15:25:57 by adjoly ### ########.fr */ /* Updated: 2024/07/31 21:55:13 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,8 +37,9 @@ typedef struct s_pdata
uint32_t die_time; uint32_t die_time;
uint32_t sleep_time; uint32_t sleep_time;
uint32_t eat_time; uint32_t eat_time;
uint16_t philo_nbr; uint8_t philo_nbr;
uint8_t meal_nbr; uint16_t meal_nbr;
bool no_meal;
bool error; bool error;
} t_pdata; } t_pdata;
@ -52,38 +53,41 @@ typedef struct s_philo
{ {
uint16_t id; uint16_t id;
struct timeval t0; struct timeval t0;
struct timeval eat;
uint16_t meal_left;
t_fork fork; t_fork fork;
t_pstate state; t_pstate state;
t_pdata data; t_pdata data;
pthread_mutex_t *check; pthread_mutex_t *check;
} t_philo; } t_philo;
typedef struct s_init
{
pthread_t thread;
t_philo data;
} t_init;
/** /**
* Utils * Utils
*/ */
t_pdata philo_parse(char **argv, int ac); t_pdata philo_parse(char **argv, int ac);
long long ft_atoll(const char *nptr); long long ft_atoll(const char *nptr);
uint16_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); void log_philo(t_philo philo);
bool sleep_phil(uint32_t sleep_time, pthread_mutex_t *death);
bool get_death(bool in, bool ret); 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);
/** /**
* Main path * Main path
* by order of call * by order of call
*/ */
void init_fork(t_pdata data); void init_fork(t_pdata data);
void init_philo(t_pdata data, t_init *init); void init_philo(t_pdata data, t_philo *philo);
void start_philo(t_init *init, uint16_t philo_nbr); void end_philo(pthread_t *thread, uint16_t philo_nbr);
void *philo_routine(void *content); void *philo_routine(void *content);
/**
* Routine func
*/
void take_fork(t_fork *fork, int id);
bool eat(t_philo *philo);
/** /**
* For debug purpose to be REMOVED * For debug purpose to be REMOVED
*/ */

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/26 15:15:13 by adjoly ### ########.fr */ /* Updated: 2024/07/31 22:10:31 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,39 +16,40 @@
void take_fork(t_fork *fork, int id) void take_fork(t_fork *fork, int id)
{ {
if (id % 2) if (id % 2 && !(&fork->left == fork->right))
{
pthread_mutex_lock(&fork->left); pthread_mutex_lock(&fork->left);
} else if (!(id % 2))
else
{
pthread_mutex_lock(fork->right); pthread_mutex_lock(fork->right);
}
} }
void *philo_routine(void *content) void *philo_routine(void *content)
{ {
t_philo philo; t_philo *philo;
bool death; bool death;
int i = 0;
philo = *(t_philo *)content; philo = content;
gettimeofday(&(philo.t0), NULL); gettimeofday(&(philo->eat), NULL);
while (i < philo.data.meal_nbr) philo->meal_left = get_meal_nb(philo->data.meal_nbr, philo->data.no_meal);
while (1)
{ {
philo.state = FORK_TAKEN; if (eat(philo) == true)
take_fork(&philo.fork, philo.id); break ;
log_philo(philo); // unlock pas les fourchet si po la
take_fork(&philo.fork, philo.id + 1); pthread_mutex_unlock(&philo->fork.left);
log_philo(philo); pthread_mutex_unlock(philo->fork.right);
philo.state = EAT; if (philo->data.no_meal == false)
log_philo(philo); philo->meal_left--;
death = sleep_phil(philo.data.eat_time, philo.check); philo->state = SLEEP;
log_philo(*philo);
death = sleep_phil(*philo);
if (death == true) if (death == true)
break ;
if (!philo->meal_left)
return (NULL); return (NULL);
pthread_mutex_unlock(philo.fork.right); philo->state = THINK;
pthread_mutex_unlock(&philo.fork.left); log_philo(*philo);
i++;
} }
philo->state = DEAD;
log_philo(*philo);
return (NULL); return (NULL);
} }

View File

@ -6,31 +6,44 @@
/* 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/25 19:57:03 by adjoly ### ########.fr */ /* Updated: 2024/07/30 02:48:34 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
uint32_t time_to_ms(void)
{
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000 + time.tv_usec / 1000);
}
#define SLEEP_SLICE_MS 10 #define SLEEP_SLICE_MS 10
bool sleep_phil(uint32_t sleep_time, pthread_mutex_t *death) bool sleep_phil(t_philo philo)
{ {
struct timeval t1;
uint32_t sleep_time;
sleep_time = 0;
if (philo.state == EAT)
sleep_time = philo.data.eat_time;
else if (philo.state == SLEEP)
sleep_time = philo.data.sleep_time;
while (sleep_time) while (sleep_time)
{ {
pthread_mutex_lock(death); pthread_mutex_lock(philo.check);
if (get_death(false, true) == true) if (get_death(false, true) == true)
{
pthread_mutex_unlock(philo.check);
return (true); return (true);
pthread_mutex_unlock(death); }
pthread_mutex_unlock(philo.check);
gettimeofday(&t1, NULL);
if (get_time_in_ms(philo.eat, t1) > philo.data.die_time)
{
pthread_mutex_lock(philo.check);
get_death(true, false);
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);
}
usleep(SLEEP_SLICE_MS * 1000); usleep(SLEEP_SLICE_MS * 1000);
sleep_time -= SLEEP_SLICE_MS; sleep_time -= SLEEP_SLICE_MS;
} }

21
philo/utils.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/31 21:20:49 by adjoly #+# #+# */
/* Updated: 2024/07/31 21:55:16 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#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);
}