「🏗️」 wip: testing things, might broke.
This commit is contained in:
25
philo/init_fork.c
Normal file
25
philo/init_fork.c
Normal 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);
|
||||
}
|
@ -6,30 +6,30 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stdio.h>
|
||||
void init_philo(t_pdata data)
|
||||
{
|
||||
t_init init[PHILO_MAX];
|
||||
pthread_mutex_t *print;
|
||||
int ret;
|
||||
int r;
|
||||
size_t i;
|
||||
|
||||
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);
|
||||
if (ret != 0)
|
||||
init[i].data.nbr = i;
|
||||
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 ;
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <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};
|
||||
|
||||
timestamp = get_time_in_ms(philo.t0, t1);
|
||||
pthread_mutex_lock(&print);
|
||||
if (philo.state == EAT)
|
||||
printf("%u %hu %s", timestamp, philo.nbr, EATING_MSG);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <sys/time.h>
|
||||
#include <limits.h>
|
||||
#include "utils.h"
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_pdata data;
|
||||
struct timeval t0;
|
||||
struct timeval t1;
|
||||
|
||||
gettimeofday(&t0, NULL);
|
||||
data = philo_parse(av, ac);
|
||||
gettimeofday(&t1, NULL);
|
||||
if (data.error == true)
|
||||
return (EXIT_FAILURE);
|
||||
print_philo_data(data);
|
||||
log_philo(get_time_in_ms(t0, t1), 0, DIED);
|
||||
init_philo(data);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <sys/time.h>
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
|
||||
# define PHILO_MAX 200
|
||||
|
||||
@ -64,16 +65,20 @@ typedef struct s_init
|
||||
/**
|
||||
* Utils
|
||||
*/
|
||||
void log_philo(uint32_t timestamp, t_philo philo);
|
||||
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);
|
||||
void log_philo(struct timeval t1, t_philo philo);
|
||||
void sleep_phil(uint32_t sleep_time);
|
||||
|
||||
/**
|
||||
* Main path
|
||||
* 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
|
||||
|
31
philo/philo_routine.c
Normal file
31
philo/philo_routine.c
Normal 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
30
philo/sleep.c
Normal 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);
|
||||
}
|
Reference in New Issue
Block a user