Finished and normed

This commit is contained in:
Raphaël 2024-07-24 14:46:47 +02:00
parent 6f7b3a44d2
commit 9285a20e35
5 changed files with 35 additions and 23 deletions

View file

@ -6,7 +6,7 @@
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ # # By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/07/18 20:55:39 by rparodi ### ########.fr # # Updated: 2024/07/24 14:12:44 by rparodi ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -23,7 +23,8 @@ RM = rm -rf
# Flags # Flags
CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD
CFLAGS += -fsanitize=thread # CFLAGS += -fsanitize=address
# CFLAGS += -fsanitize=thread
INCLUDES = ./includes/philo.h \ INCLUDES = ./includes/philo.h \
./includes/types.h ./includes/types.h

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */ /* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */
/* Updated: 2024/07/18 21:11:30 by rparodi ### ########.fr */ /* Updated: 2024/07/24 14:16:00 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,6 +38,13 @@ t_error ft_thinking(t_philo *philo)
t_error ft_start_eating(t_philo *philo) t_error ft_start_eating(t_philo *philo)
{ {
if (philo->nb_philo == 1)
{
ft_logs("has taken a fork\n", philo);
while (!dead_loop(philo))
usleep(50);
return (NO_ERROR);
}
if (philo->id % 2 == 0) if (philo->id % 2 == 0)
{ {
pthread_mutex_lock(philo->r_fork); pthread_mutex_lock(philo->r_fork);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */ /* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */
/* Updated: 2024/07/23 19:51:07 by rparodi ### ########.fr */ /* Updated: 2024/07/24 14:07:27 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,13 +19,15 @@ void *ft_routine(void *ptr)
philo = (t_philo *) ptr; philo = (t_philo *) ptr;
if (philo == NULL) if (philo == NULL)
return (NULL); return (NULL);
while (philo->start_time > ft_time())
usleep(500);
while (!dead_loop(philo)) while (!dead_loop(philo))
{ {
if (check_dead(philo) == false) if (dead_loop(philo) == false)
ft_start_eating(philo); ft_start_eating(philo);
if (check_dead(philo) == false) if (dead_loop(philo) == false)
ft_sleeping(philo); ft_sleeping(philo);
if (check_dead(philo) == false) if (dead_loop(philo) == false)
ft_thinking(philo); ft_thinking(philo);
} }
return (philo); return (philo);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */ /* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */
/* Updated: 2024/07/23 21:07:50 by rparodi ### ########.fr */ /* Updated: 2024/07/24 14:23:03 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,14 +23,15 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo)
ft_atou(argv[1], &tmp); ft_atou(argv[1], &tmp);
if (tmp == 0) if (tmp == 0)
return (ERROR); return (ERROR);
philo->nb_philo = tmp;
philo->nb_eat = ~0u;
if (argc == 6) if (argc == 6)
{ {
ft_atou(argv[5], &tmp); ft_atou(argv[5], &tmp);
if (tmp == 0)
return (ERROR);
philo->nb_eat = tmp; philo->nb_eat = tmp;
} }
else
philo->nb_eat = ~0u;
philo->nb_philo = tmp;
ft_atou(argv[2], &tmp); ft_atou(argv[2], &tmp);
philo->t_death = tmp; philo->t_death = tmp;
ft_atou(argv[3], &tmp); ft_atou(argv[3], &tmp);
@ -47,7 +48,6 @@ t_error ft_init_fork(t_mutex *fork, t_usize nb_fork)
i = 0; i = 0;
while (i < nb_fork) while (i < nb_fork)
{ {
printf("\tforks[%zu]\n", i + 1);
fork[i] = (t_mutex)PTHREAD_MUTEX_INITIALIZER; fork[i] = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
i++; i++;
} }
@ -65,13 +65,15 @@ void ft_pause(size_t milliseconds)
t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks) t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks)
{ {
t_usize i; t_usize i;
t_usize start_time;
start_time = ft_time() + 500;
i = 0; i = 0;
while (i < ft_atou_return(argv[1])) while (i < ft_atou_return(argv[1]))
{ {
prog->philos[i].id = i + 1; prog->philos[i].id = i + 1;
ft_parsing_args(argc, argv, &prog->philos[i]); ft_parsing_args(argc, argv, &prog->philos[i]);
prog->philos[i].start_time = ft_time(); prog->philos[i].start_time = start_time;
prog->philos[i].t_last_eat = 0; prog->philos[i].t_last_eat = 0;
prog->philos[i].print_lock = &prog->print_lock; prog->philos[i].print_lock = &prog->print_lock;
prog->philos[i].dead_lock = &prog->dead_lock; prog->philos[i].dead_lock = &prog->dead_lock;
@ -83,12 +85,9 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks)
prog->philos[i].r_fork = &forks[prog->philos[i].nb_philo - 1]; prog->philos[i].r_fork = &forks[prog->philos[i].nb_philo - 1];
else else
prog->philos[i].r_fork = &forks[i - 1]; prog->philos[i].r_fork = &forks[i - 1];
printf("\tphilo[%zu]\n", i + 1);
i++; i++;
} }
if (ft_parsing_args(argc, argv, prog->philos)) return (ft_parsing_args(argc, argv, prog->philos));
return (ERROR);
return (NO_ERROR);
} }
t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo) t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo)

View file

@ -6,16 +6,17 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */ /* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */
/* Updated: 2024/07/23 19:49:21 by rparodi ### ########.fr */ /* Updated: 2024/07/24 14:07:27 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../includes/philo.h" #include "../includes/philo.h"
#include <stdio.h>
t_bool dead_loop(t_philo *philo) t_bool dead_loop(t_philo *philo)
{ {
pthread_mutex_lock(philo->dead_lock); pthread_mutex_lock(philo->dead_lock);
if (philo[0].dead_check == true) if (philo->dead_check == true)
return (pthread_mutex_unlock(philo->dead_lock), true); return (pthread_mutex_unlock(philo->dead_lock), true);
pthread_mutex_unlock(philo->dead_lock); pthread_mutex_unlock(philo->dead_lock);
return (false); return (false);
@ -80,6 +81,8 @@ void *ft_watch_dogs(void *ptr)
philo = (t_philo *) ptr; philo = (t_philo *) ptr;
if (philo == NULL) if (philo == NULL)
return (NULL); return (NULL);
while (philo[0].start_time > ft_time())
usleep(500);
i = philo[0].nb_philo; i = philo[0].nb_philo;
while (true) while (true)
{ {