Passing the norm on all these files

This commit is contained in:
Raphaël 2024-07-06 18:43:51 +02:00
parent 124064556e
commit af63bafed0
7 changed files with 102 additions and 73 deletions

2
philo/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
objects/
philo

View file

@ -6,7 +6,7 @@
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/06/26 17:25:08 by rparodi ### ########.fr #
# Updated: 2024/07/04 15:20:22 by rparodi ### ########.fr #
# #
# **************************************************************************** #
@ -120,4 +120,3 @@ footer:
.PHONY: all bonus clean fclean re
-include ${OBJ:.o=.d}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 11:34:04 by rparodi #+# #+# */
/* Updated: 2024/06/26 17:15:16 by rparodi ### ########.fr */
/* Updated: 2024/07/04 14:11:50 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -70,20 +70,28 @@ typedef struct s_program
t_usize ft_time(void);
void ft_free(void *ptr);
t_usize ft_strlen(t_str str);
void *ft_routine(void *ptr);
void *ft_watch_dogs(void *ptr);
t_bool dead_loop(t_philo *philo);
t_bool check_eat(t_philo *philo);
t_usize ft_atou_return(t_str str);
t_error ft_sleeping(t_philo *philo);
t_bool check_dead(t_philo *philo);
t_error ft_thinking(t_philo *philo);
t_error ft_sleeping(t_philo *philo);
t_i32 main(t_i32 argc, t_str *argv);
void ft_pause(size_t milliseconds);
t_error ft_start_eating(t_philo *philo);
void ft_exit(t_str msg, t_u8 status);
t_error ft_start_eating(t_philo *philo);
t_error ft_ending_eating(t_philo *philo);
void ft_putchar_fd(t_i32 fd, t_str str);
void ft_logs(t_str msg, t_philo *philo);
t_error ft_atou(const char *nptr, t_usize *value);
t_error ft_init_fork(t_philo *philo, t_mutex *fork);
void counting_launch(t_philo *philo, t_u8 *counter);
t_error ft_init_thread(t_program *prog, t_mutex *forks);
t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo);
t_error ft_init(int argc, t_str *argv, t_program *program, t_philo *philo);
t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo);
t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks);
void ft_destroy_exit(t_str msg, t_u8 status, t_program *prog, \
t_mutex *forks);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */
/* Updated: 2024/06/26 17:24:47 by rparodi ### ########.fr */
/* Updated: 2024/07/04 11:48:33 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,8 @@ void ft_logs(t_str msg, t_philo *philo)
const t_usize time = ft_time() - philo->start_time;
if (msg != NULL)
printf("%s%zu %s%i %s%s%s", BLUE, time, GOLD, philo->id, GREEN, msg, END);
printf("%s%zu %s%i %s%s%s", BLUE, time, GOLD, philo->id, \
GREEN, msg, END);
}
t_error ft_thinking(t_philo *philo)
@ -28,8 +29,6 @@ t_error ft_thinking(t_philo *philo)
t_error ft_start_eating(t_philo *philo)
{
const t_usize time = ft_time() - philo->start_time;
if (philo->id % 2 == 0)
{
pthread_mutex_lock(philo->l_fork);
@ -44,6 +43,13 @@ t_error ft_start_eating(t_philo *philo)
pthread_mutex_lock(philo->l_fork);
ft_logs("has taken a fork\n", philo);
}
return (ft_ending_eating(philo));
}
t_error ft_ending_eating(t_philo *philo)
{
const t_usize time = ft_time() - philo->start_time;
philo->eating = true;
ft_logs("is eating\n", philo);
philo->t_last_eat = time;

View file

@ -6,12 +6,60 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */
/* Updated: 2024/06/16 12:22:40 by rparodi ### ########.fr */
/* Updated: 2024/07/04 15:27:12 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo.h"
void *ft_routine(void *ptr)
{
t_philo *philo;
philo = (t_philo *) ptr;
if (philo == NULL)
return (NULL);
while (!dead_loop(philo))
{
ft_start_eating(philo);
ft_sleeping(philo);
ft_thinking(philo);
}
return (philo);
}
t_error ft_init_thread(t_program *prog, t_mutex *forks)
{
t_thread o_block;
t_usize i;
i = 0;
if (pthread_create(&o_block, NULL, &ft_watch_dogs, prog->philos))
ft_destroy_exit("Allocation of watch_dogs failed\n", \
ERROR, prog, forks);
while (i < prog->philos[0].nb_philo)
{
if (pthread_create(&prog->philos[i].thread, NULL, &ft_routine, \
&prog->philos[i]))
ft_destroy_exit("Allocation of one philo thread failed !\n", \
ERROR, prog, forks);
i++;
}
while (--i > 0)
pthread_join(prog->philos[i].thread, NULL);
pthread_join(o_block, NULL);
return (NO_ERROR);
}
t_usize ft_time(void)
{
struct timeval time;
if (gettimeofday(&time, NULL) == -1)
ft_exit("Error of during ft_time !\n", 1);
return (time.tv_sec * 1000 + time.tv_usec * 0.001);
}
t_i32 main(t_i32 argc, t_str *argv)
{
t_philo philo[MAXSUPPORT];
@ -19,6 +67,8 @@ t_i32 main(t_i32 argc, t_str *argv)
if (argc != 6 && argc != 5)
ft_exit(ARGS, 1);
if (ft_atou_return(argv[1]) > MAXSUPPORT)
ft_exit("Please update the max support !\n", 1);
if (ft_init(argc, argv, &prog, philo))
ft_exit("Issues during initialisation\n", ERROR);
return (0);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */
/* Updated: 2024/06/26 17:16:09 by rparodi ### ########.fr */
/* Updated: 2024/07/04 11:52:27 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -48,15 +48,6 @@ t_error ft_init_fork(t_philo *philo, t_mutex *fork)
return (NO_ERROR);
}
t_usize ft_time(void)
{
struct timeval time;
if (gettimeofday(&time, NULL) == -1)
ft_exit("Error of during ft_time !\n", 1);
return (time.tv_sec * 1000 + time.tv_usec * 0.001);
}
void ft_pause(size_t milliseconds)
{
const t_usize start = ft_time();
@ -96,7 +87,7 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks)
t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo)
{
static t_mutex forks[MAXSUPPORT] = { 0 };
static t_mutex forks[MAXSUPPORT] = {0};
pthread_mutex_init(&prog->print_lock, NULL);
pthread_mutex_init(&prog->dead_lock, NULL);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */
/* Updated: 2024/06/26 17:23:11 by rparodi ### ########.fr */
/* Updated: 2024/07/04 13:29:39 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,7 +24,7 @@ t_bool dead_loop(t_philo *philo)
t_bool check_dead(t_philo *philo)
{
t_usize i;
t_usize timecheck;
t_usize timecheck;
i = 0;
while (i < philo[0].nb_philo)
@ -32,7 +32,10 @@ t_bool check_dead(t_philo *philo)
timecheck = ft_time() - philo[i].start_time - philo[i].t_last_eat;
if (philo[i].t_last_eat != 0 && timecheck >= philo[i].t_death)
{
pthread_mutex_unlock(philo[i].dead_lock);
ft_logs("died\n", &philo[i]);
philo[i].dead_check = true;
pthread_mutex_lock(philo[i].dead_lock);
return (true);
}
i++;
@ -40,7 +43,22 @@ t_bool check_dead(t_philo *philo)
return (false);
}
t_bool check_eat(t_philo *philo)
void counting_launch(t_philo *philo, t_u8 *counter)
{
t_usize i;
i = 0;
while (i < philo[0].nb_philo)
{
pthread_mutex_lock(philo[i].check_eating_count);
if (philo[i].eating_count >= philo[i].nb_eat)
(*counter)++;
pthread_mutex_unlock(philo[i].check_eating_count);
i++;
}
}
t_bool check_eat(t_philo *philo)
{
t_usize i;
t_u8 check;
@ -49,21 +67,15 @@ t_bool check_eat(t_philo *philo)
check = 0;
if (philo[0].nb_eat == -1)
return (false);
while (i < philo[0].nb_philo)
{
pthread_mutex_lock(philo[i].check_eating_count);
if (philo[i].eating_count >= philo[i].nb_eat)
check++;
pthread_mutex_unlock(philo[i].check_eating_count);
i++;
}
i = 0;
counting_launch(philo, &check);
if (check == philo[0].nb_philo)
{
pthread_mutex_lock(philo[0].dead_lock);
while (i < philo[0].nb_philo)
{
pthread_mutex_unlock(philo[i].dead_lock);
philo[i].dead_check = true;
pthread_mutex_lock(philo[i].dead_lock);
i++;
}
pthread_mutex_unlock(philo[0].dead_lock);
@ -88,42 +100,3 @@ void *ft_watch_dogs(void *ptr)
}
return (philo);
}
void *ft_routine(void *ptr)
{
t_philo *philo;
philo = (t_philo *) ptr;
if (philo == NULL)
return (NULL);
while (!dead_loop(philo))
{
ft_start_eating(philo);
ft_sleeping(philo);
ft_thinking(philo);
}
return (philo);
}
t_error ft_init_thread(t_program *prog, t_mutex *forks)
{
t_thread o_block;
t_usize i;
i = 0;
if (pthread_create(&o_block, NULL, &ft_watch_dogs, prog->philos))
ft_destroy_exit("Allocation of watch_dogs failed\n", \
ERROR, prog, forks);
while (i < prog->philos[0].nb_philo)
{
if (pthread_create(&prog->philos[i].thread, NULL, &ft_routine, \
&prog->philos[i]))
ft_destroy_exit("Allocation of one philo thread failed !\n", \
ERROR, prog, forks);
i++;
}
while (--i > 0)
pthread_join(prog->philos[i].thread, NULL);
pthread_join(o_block, NULL);
return (NO_ERROR);
}