「🏗️」 wip: testing things, might broke.
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
31
philo/eat.c
Normal file
31
philo/eat.c
Normal 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);
|
||||
}
|
@ -1,18 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* start_philo.c :+: :+: :+: */
|
||||
/* end_philo.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
void start_philo(t_init *init, uint16_t philo_nbr)
|
||||
void end_philo(pthread_t *thread, uint16_t philo_nbr)
|
||||
{
|
||||
uint16_t i;
|
||||
int r;
|
||||
@ -20,7 +20,7 @@ void start_philo(t_init *init, uint16_t philo_nbr)
|
||||
i = 0;
|
||||
while (i < philo_nbr)
|
||||
{
|
||||
r = pthread_join(init[i].thread, NULL);
|
||||
r = pthread_join(thread[i], NULL);
|
||||
if (r != 0)
|
||||
return ;
|
||||
i++;
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "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 + \
|
||||
t1.tv_usec - t0.tv_usec) / 1000);
|
||||
|
@ -6,24 +6,24 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stdlib.h>
|
||||
|
||||
void init_fork(t_pdata data)
|
||||
{
|
||||
uint16_t i;
|
||||
t_init init[PHILO_MAX];
|
||||
|
||||
uint8_t i;
|
||||
t_philo philo[PHILO_MAX];
|
||||
|
||||
i = 0;
|
||||
while (i < data.philo_nbr)
|
||||
{
|
||||
pthread_mutex_init(&(init[i].data.fork.left), NULL);
|
||||
init[i].data.fork.right = &init[(i - 1) % data.philo_nbr].data.fork.left;
|
||||
pthread_mutex_init(&(philo[i].fork.left), NULL);
|
||||
philo[i].fork.right = &philo[(i + 1) % data.philo_nbr].fork.left;
|
||||
i++;
|
||||
}
|
||||
init_philo(data, init);
|
||||
init_philo(data, philo);
|
||||
}
|
||||
|
@ -6,33 +6,35 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stdlib.h>
|
||||
|
||||
void init_philo(t_pdata data, t_init *init)
|
||||
void init_philo(t_pdata data, t_philo *philo)
|
||||
{
|
||||
int r;
|
||||
pthread_mutex_t check;
|
||||
uint16_t i;
|
||||
pthread_t thread[PHILO_MAX];
|
||||
struct timeval time;
|
||||
uint8_t i;
|
||||
|
||||
i = 0;
|
||||
pthread_mutex_init(&check, NULL);
|
||||
get_death(false, false);
|
||||
gettimeofday(&time, NULL);
|
||||
while (i < data.philo_nbr)
|
||||
{
|
||||
init[i].data.id = i + 1;
|
||||
init[i].data.state = SLEEP;
|
||||
init[i].data.data = data;
|
||||
init[i].data.check = ✓
|
||||
r = pthread_create(&init[i].thread, NULL,
|
||||
philo_routine, &(init[i].data));
|
||||
philo[i].id = i + 1;
|
||||
philo[i].data = data;
|
||||
philo[i].check = ✓
|
||||
philo[i].t0 = time;
|
||||
r = pthread_create(&thread[i], NULL, philo_routine, &philo[i]);
|
||||
if (r != 0)
|
||||
break ;
|
||||
i++;
|
||||
}
|
||||
start_philo(init, i);
|
||||
end_philo(thread, data.philo_nbr);
|
||||
}
|
||||
|
15
philo/note
15
philo/note
@ -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
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 sleep_time;
|
||||
uint32_t eat_time;
|
||||
uint16_t philo_nbr;
|
||||
uint8_t meal_nbr;
|
||||
uint8_t philo_nbr;
|
||||
uint16_t meal_nbr;
|
||||
bool no_meal;
|
||||
bool error;
|
||||
} t_pdata;
|
||||
|
||||
@ -52,38 +53,41 @@ typedef struct s_philo
|
||||
{
|
||||
uint16_t id;
|
||||
struct timeval t0;
|
||||
struct timeval eat;
|
||||
uint16_t meal_left;
|
||||
t_fork fork;
|
||||
t_pstate state;
|
||||
t_pdata data;
|
||||
pthread_mutex_t *check;
|
||||
} t_philo;
|
||||
|
||||
typedef struct s_init
|
||||
{
|
||||
pthread_t thread;
|
||||
t_philo data;
|
||||
} t_init;
|
||||
|
||||
/**
|
||||
* Utils
|
||||
*/
|
||||
t_pdata philo_parse(char **argv, int ac);
|
||||
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);
|
||||
bool sleep_phil(uint32_t sleep_time, pthread_mutex_t *death);
|
||||
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
|
||||
* by order of call
|
||||
*/
|
||||
void init_fork(t_pdata data);
|
||||
void init_philo(t_pdata data, t_init *init);
|
||||
void start_philo(t_init *init, uint16_t philo_nbr);
|
||||
void init_philo(t_pdata data, t_philo *philo);
|
||||
void end_philo(pthread_t *thread, uint16_t philo_nbr);
|
||||
|
||||
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
|
||||
*/
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
{
|
||||
if (id % 2)
|
||||
{
|
||||
if (id % 2 && !(&fork->left == fork->right))
|
||||
pthread_mutex_lock(&fork->left);
|
||||
}
|
||||
else
|
||||
{
|
||||
else if (!(id % 2))
|
||||
pthread_mutex_lock(fork->right);
|
||||
}
|
||||
}
|
||||
|
||||
void *philo_routine(void *content)
|
||||
{
|
||||
t_philo philo;
|
||||
t_philo *philo;
|
||||
bool death;
|
||||
int i = 0;
|
||||
|
||||
philo = *(t_philo *)content;
|
||||
gettimeofday(&(philo.t0), NULL);
|
||||
while (i < philo.data.meal_nbr)
|
||||
philo = content;
|
||||
gettimeofday(&(philo->eat), NULL);
|
||||
philo->meal_left = get_meal_nb(philo->data.meal_nbr, philo->data.no_meal);
|
||||
while (1)
|
||||
{
|
||||
philo.state = FORK_TAKEN;
|
||||
take_fork(&philo.fork, philo.id);
|
||||
log_philo(philo);
|
||||
take_fork(&philo.fork, philo.id + 1);
|
||||
log_philo(philo);
|
||||
philo.state = EAT;
|
||||
log_philo(philo);
|
||||
death = sleep_phil(philo.data.eat_time, philo.check);
|
||||
if (eat(philo) == true)
|
||||
break ;
|
||||
// unlock pas les fourchet si po la
|
||||
pthread_mutex_unlock(&philo->fork.left);
|
||||
pthread_mutex_unlock(philo->fork.right);
|
||||
if (philo->data.no_meal == false)
|
||||
philo->meal_left--;
|
||||
philo->state = SLEEP;
|
||||
log_philo(*philo);
|
||||
death = sleep_phil(*philo);
|
||||
if (death == true)
|
||||
break ;
|
||||
if (!philo->meal_left)
|
||||
return (NULL);
|
||||
pthread_mutex_unlock(philo.fork.right);
|
||||
pthread_mutex_unlock(&philo.fork.left);
|
||||
i++;
|
||||
philo->state = THINK;
|
||||
log_philo(*philo);
|
||||
}
|
||||
philo->state = DEAD;
|
||||
log_philo(*philo);
|
||||
return (NULL);
|
||||
}
|
||||
|
@ -6,31 +6,44 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
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
|
||||
|
||||
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)
|
||||
{
|
||||
pthread_mutex_lock(death);
|
||||
pthread_mutex_lock(philo.check);
|
||||
if (get_death(false, true) == true)
|
||||
{
|
||||
pthread_mutex_unlock(philo.check);
|
||||
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);
|
||||
sleep_time -= SLEEP_SLICE_MS;
|
||||
}
|
||||
|
21
philo/utils.c
Normal file
21
philo/utils.c
Normal 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);
|
||||
}
|
Reference in New Issue
Block a user