1
0

🏗️」 wip: testing things, might broke.

This commit is contained in:
2024-07-23 18:11:44 +02:00
parent 962eb97c3d
commit a663216595
7 changed files with 113 additions and 26 deletions

25
philo/init_fork.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_fork.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/22 15:29:08 by adjoly #+# #+# */
/* Updated: 2024/07/22 17:37:26 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
t_fork init_fork(t_init *init, uint16_t i)
{
t_fork fork;
if (i != 0)
fork.right = &(init[i - 1].data.fork.left);
else
fork.right = &(init[init[i].data.data.philo_nbr - 1].data.fork.left);
pthread_mutex_init(&(init[i].data.fork.left), NULL);
return (fork);
}

View File

@ -6,30 +6,30 @@
/* 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/22 14:05:52 by adjoly ### ########.fr */ /* Updated: 2024/07/23 17:53:14 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
#include <stdio.h>
void init_philo(t_pdata data) void init_philo(t_pdata data)
{ {
t_init init[PHILO_MAX]; t_init init[PHILO_MAX];
pthread_mutex_t *print; int r;
int ret; size_t i;
i = 0; i = 0;
init_fork(philo, data); while (i < data.philo_nbr)
while (i < data->philo_nbr)
{ {
ret = pthread_create(&(init[i]->thread), NULL, philo_routine, tmp); init[i].data.nbr = i;
if (ret != 0) init[i].data.data = data;
init[i].data.state = EAT;
init[i].data.fork = init_fork(init, i);
r = pthread_create(&(init[i].thread), NULL,
philo_routine, &init[i].data);
if (r != 0)
return ; return ;
init[i]->philo->nbr = i;
gettimeofday(&(init[i]->philo->t0), NULL)
init[i]->philo->data = data;
init[i]->philo->state = EAT;
init[i]->philo->fork = init_fork(init, i, data->philo_nbr);
i++; i++;
} }
} }

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/21 21:31:44 by adjoly ### ########.fr */ /* Updated: 2024/07/22 21:21:21 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,10 +14,12 @@
#include "philo_msg.h" #include "philo_msg.h"
#include <stdio.h> #include <stdio.h>
void log_philo(uint32_t timestamp, t_philo philo) void log_philo(struct timeval t1, t_philo philo)
{ {
uint32_t timestamp;
static pthread_mutex_t print = {0}; static pthread_mutex_t print = {0};
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.nbr, EATING_MSG); printf("%u %hu %s", timestamp, philo.nbr, EATING_MSG);

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/10 18:47:00 by adjoly ### ########.fr */ /* Updated: 2024/07/23 17:42:16 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,19 +15,13 @@
#include <stdio.h> #include <stdio.h>
#include <sys/time.h> #include <sys/time.h>
#include <limits.h> #include <limits.h>
#include "utils.h"
int main(int ac, char **av) int main(int ac, char **av)
{ {
t_pdata data; t_pdata data;
struct timeval t0;
struct timeval t1;
gettimeofday(&t0, NULL);
data = philo_parse(av, ac); data = philo_parse(av, ac);
gettimeofday(&t1, NULL);
if (data.error == true) if (data.error == true)
return (EXIT_FAILURE); return (EXIT_FAILURE);
print_philo_data(data); init_philo(data);
log_philo(get_time_in_ms(t0, t1), 0, DIED);
} }

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/22 14:01:27 by adjoly ### ########.fr */ /* Updated: 2024/07/23 17:23:38 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,6 +18,7 @@
# include <stdbool.h> # include <stdbool.h>
# include <sys/time.h> # include <sys/time.h>
# include <sys/types.h> # include <sys/types.h>
# include <unistd.h>
# define PHILO_MAX 200 # define PHILO_MAX 200
@ -64,16 +65,20 @@ typedef struct s_init
/** /**
* Utils * Utils
*/ */
void log_philo(uint32_t timestamp, t_philo philo);
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); uint16_t get_time_in_ms(struct timeval t0, struct timeval t1);
void log_philo(struct timeval t1, t_philo philo);
void sleep_phil(uint32_t sleep_time);
/** /**
* Main path * Main path
* by order of call * by order of call
*/ */
t_philo *init_philo(t_pdata data, uint16_t philo_nbr); void init_philo(t_pdata data);
t_fork init_fork(t_init *init, uint16_t nbr);
void *philo_routine(void *content);
/** /**
* For debug purpose to be REMOVED * For debug purpose to be REMOVED

31
philo/philo_routine.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_routine.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/22 21:24:53 by adjoly #+# #+# */
/* Updated: 2024/07/23 18:07:56 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
#include <stdio.h>
void *philo_routine(void *content)
{
t_philo philo;
// struct timeval t1;
philo = *(t_philo *)content;
printf("\t\t%hu\n", philo.nbr);
//print_philo_data(philo.data);
// while (1)
// {
// gettimeofday(&t1, NULL);
// log_philo(t1, philo);
// sleep_phil(philo.data.eat_time);
// }
return (NULL);
}

30
philo/sleep.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sleep.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */
/* Updated: 2024/07/23 17:24:30 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);
}
void sleep_phil(uint32_t sleep_time)
{
uint32_t t0;
t0 = time_to_ms() + sleep_time;
while (time_to_ms() < t0)
usleep(100);
}