「🏗️」 wip: testing things, might broke.
This commit is contained in:
22
philo/check_death.c
Normal file
22
philo/check_death.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* check_death.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* 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 */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "philo.h"
|
||||||
|
|
||||||
|
bool get_death(bool in, bool ret)
|
||||||
|
{
|
||||||
|
static bool death;
|
||||||
|
|
||||||
|
if (ret == false)
|
||||||
|
death = in;
|
||||||
|
return (death);
|
||||||
|
}
|
@ -6,17 +6,16 @@
|
|||||||
/* 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/16 21:12:05 by adjoly ### ########.fr */
|
/* Updated: 2024/07/24 23:51:20 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include "philo.h"
|
||||||
|
|
||||||
uint16_t get_time_in_ms(struct timeval t0, struct timeval t1)
|
uint16_t get_time_in_ms(struct timeval t0, struct timeval t1)
|
||||||
{
|
{
|
||||||
int diff;
|
return (((t1.tv_sec - t0.tv_sec) * 1000000 + \
|
||||||
|
t1.tv_usec - t0.tv_usec) / 1000);
|
||||||
diff = t1.tv_sec - t0.tv_sec;
|
|
||||||
return (((diff) * 1000000 + diff) / 1000);
|
|
||||||
}
|
}
|
||||||
|
@ -6,20 +6,27 @@
|
|||||||
/* 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/22 17:37:26 by adjoly ### ########.fr */
|
/* Updated: 2024/07/24 18:47:29 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
|
||||||
t_fork init_fork(t_init *init, uint16_t i)
|
void init_fork(t_init *init, uint16_t philo_nbr)
|
||||||
{
|
{
|
||||||
t_fork fork;
|
uint16_t i;
|
||||||
|
|
||||||
if (i != 0)
|
i = 0;
|
||||||
fork.right = &(init[i - 1].data.fork.left);
|
while (i < philo_nbr)
|
||||||
else
|
{
|
||||||
fork.right = &(init[init[i].data.data.philo_nbr - 1].data.fork.left);
|
pthread_mutex_init(&(init[i].data.fork.left), NULL);
|
||||||
pthread_mutex_init(&(init[i].data.fork.left), NULL);
|
i++;
|
||||||
return (fork);
|
}
|
||||||
|
init[0].data.fork.right = &(init[philo_nbr - 1].data.fork.left);
|
||||||
|
i = 1;
|
||||||
|
while (i < philo_nbr)
|
||||||
|
{
|
||||||
|
init[i].data.fork.right = &(init[i - 1].data.fork.left);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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/23 17:53:14 by adjoly ### ########.fr */
|
/* Updated: 2024/07/25 15:14:45 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -15,21 +15,25 @@
|
|||||||
#include <stdio.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];
|
||||||
int r;
|
int r;
|
||||||
size_t i;
|
pthread_mutex_t check;
|
||||||
|
uint16_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
pthread_mutex_init(&check, NULL);
|
||||||
|
init_fork(init, init[0].data.data.philo_nbr);
|
||||||
while (i < data.philo_nbr)
|
while (i < data.philo_nbr)
|
||||||
{
|
{
|
||||||
init[i].data.nbr = i;
|
init[i].data.id = i;
|
||||||
init[i].data.data = data;
|
init[i].data.data = data;
|
||||||
init[i].data.state = EAT;
|
init[i].data.state = EAT;
|
||||||
init[i].data.fork = init_fork(init, i);
|
init[i].data.check = ✓
|
||||||
r = pthread_create(&(init[i].thread), NULL,
|
r = pthread_create(&init[i].thread, NULL,
|
||||||
philo_routine, &init[i].data);
|
philo_routine, &(init[i].data));
|
||||||
if (r != 0)
|
if (r != 0)
|
||||||
return ;
|
break ;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
start_philo(init, i);
|
||||||
}
|
}
|
||||||
|
18
philo/log.c
18
philo/log.c
@ -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/22 21:21:21 by adjoly ### ########.fr */
|
/* Updated: 2024/07/24 23:56:07 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,22 +14,24 @@
|
|||||||
#include "philo_msg.h"
|
#include "philo_msg.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void log_philo(struct timeval t1, t_philo philo)
|
void log_philo(t_philo philo)
|
||||||
{
|
{
|
||||||
uint32_t timestamp;
|
uint32_t timestamp;
|
||||||
|
struct timeval t1;
|
||||||
static pthread_mutex_t print = {0};
|
static pthread_mutex_t print = {0};
|
||||||
|
|
||||||
|
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.nbr, 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.nbr, 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.nbr, SLEEP_MSG);
|
printf("%u %hu %s", timestamp, philo.id, SLEEP_MSG);
|
||||||
else if (philo.state == DIED)
|
else if (philo.state == DEAD)
|
||||||
printf("%u %hu %s", timestamp, philo.nbr, 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.nbr, FORK_MSG);
|
printf("%u %hu %s", timestamp, philo.id, FORK_MSG);
|
||||||
pthread_mutex_unlock(&print);
|
pthread_mutex_unlock(&print);
|
||||||
}
|
}
|
||||||
|
15
philo/note
Normal file
15
philo/note
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
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> +#+ +:+ +#+ */
|
/* 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/23 17:23:38 by adjoly ### ########.fr */
|
/* Updated: 2024/07/25 00:03:53 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,12 +19,13 @@
|
|||||||
# include <sys/time.h>
|
# include <sys/time.h>
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
|
||||||
# define PHILO_MAX 200
|
# define PHILO_MAX 200
|
||||||
|
|
||||||
typedef enum s_pstate
|
typedef enum s_pstate
|
||||||
{
|
{
|
||||||
DIED = -1,
|
DEAD = -1,
|
||||||
EAT,
|
EAT,
|
||||||
THINK,
|
THINK,
|
||||||
SLEEP,
|
SLEEP,
|
||||||
@ -49,11 +50,12 @@ typedef struct s_fork
|
|||||||
|
|
||||||
typedef struct s_philo
|
typedef struct s_philo
|
||||||
{
|
{
|
||||||
uint16_t nbr;
|
uint16_t id;
|
||||||
struct timeval t0;
|
struct timeval t0;
|
||||||
t_fork fork;
|
t_fork fork;
|
||||||
t_pstate state;
|
t_pstate state;
|
||||||
t_pdata data;
|
t_pdata data;
|
||||||
|
pthread_mutex_t *check;
|
||||||
} t_philo;
|
} t_philo;
|
||||||
|
|
||||||
typedef struct s_init
|
typedef struct s_init
|
||||||
@ -68,15 +70,17 @@ typedef struct s_init
|
|||||||
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 log_philo(t_philo philo);
|
||||||
void sleep_phil(uint32_t sleep_time);
|
bool sleep_phil(uint32_t sleep_time, pthread_mutex_t *death);
|
||||||
|
bool get_death(bool in, bool ret);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main path
|
* Main path
|
||||||
* by order of call
|
* by order of call
|
||||||
*/
|
*/
|
||||||
void init_philo(t_pdata data);
|
void init_philo(t_pdata data);
|
||||||
t_fork init_fork(t_init *init, uint16_t nbr);
|
void init_fork(t_init *init, uint16_t philo_nbr);
|
||||||
|
void start_philo(t_init *init, uint16_t philo_nbr);
|
||||||
|
|
||||||
void *philo_routine(void *content);
|
void *philo_routine(void *content);
|
||||||
|
|
||||||
|
@ -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/23 18:07:56 by adjoly ### ########.fr */
|
/* Updated: 2024/07/25 16:12:00 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,16 +16,28 @@
|
|||||||
void *philo_routine(void *content)
|
void *philo_routine(void *content)
|
||||||
{
|
{
|
||||||
t_philo philo;
|
t_philo philo;
|
||||||
// struct timeval t1;
|
bool death;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
philo = *(t_philo *)content;
|
philo = *(t_philo *)content;
|
||||||
printf("\t\t%hu\n", philo.nbr);
|
gettimeofday(&(philo.t0), NULL);
|
||||||
//print_philo_data(philo.data);
|
while (i < philo.data.meal_nbr)
|
||||||
// while (1)
|
{
|
||||||
// {
|
philo.state = EAT;
|
||||||
// gettimeofday(&t1, NULL);
|
pthread_mutex_lock(&philo.fork.left);
|
||||||
// log_philo(t1, philo);
|
pthread_mutex_lock(philo.fork.right);
|
||||||
// sleep_phil(philo.data.eat_time);
|
log_philo(philo);
|
||||||
// }
|
death = sleep_phil(philo.data.eat_time, philo.check);
|
||||||
|
if (death == true)
|
||||||
|
return (NULL);
|
||||||
|
philo.state = SLEEP;
|
||||||
|
log_philo(philo);
|
||||||
|
death = sleep_phil(philo.data.sleep_time, philo.check);
|
||||||
|
if (death == true)
|
||||||
|
return (NULL);
|
||||||
|
philo.state = THINK;
|
||||||
|
log_philo(philo);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* 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/23 17:24:30 by adjoly ### ########.fr */
|
/* Updated: 2024/07/25 16:08:44 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -20,11 +20,18 @@ uint32_t time_to_ms(void)
|
|||||||
return (time.tv_sec * 1000 + time.tv_usec / 1000);
|
return (time.tv_sec * 1000 + time.tv_usec / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sleep_phil(uint32_t sleep_time)
|
bool sleep_phil(uint32_t sleep_time, pthread_mutex_t *death)
|
||||||
{
|
{
|
||||||
uint32_t t0;
|
uint32_t t0;
|
||||||
|
|
||||||
t0 = time_to_ms() + sleep_time;
|
t0 = time_to_ms() + sleep_time;
|
||||||
while (time_to_ms() < t0)
|
while (time_to_ms() < t0)
|
||||||
usleep(100);
|
{
|
||||||
|
pthread_mutex_lock(death);
|
||||||
|
if (get_death(false, true) == true)
|
||||||
|
return (true);
|
||||||
|
pthread_mutex_unlock(death);
|
||||||
|
usleep(1000);
|
||||||
|
}
|
||||||
|
return (false);
|
||||||
}
|
}
|
||||||
|
28
philo/start_philo.c
Normal file
28
philo/start_philo.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* start_philo.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/24 18:19:14 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2024/07/24 23:01:29 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "philo.h"
|
||||||
|
|
||||||
|
void start_philo(t_init *init, uint16_t philo_nbr)
|
||||||
|
{
|
||||||
|
uint16_t i;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < philo_nbr)
|
||||||
|
{
|
||||||
|
r = pthread_join(init[i].thread, NULL);
|
||||||
|
if (r != 0)
|
||||||
|
return ;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user