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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
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)
{
pthread_mutex_lock(philo->r_fork);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
if (philo == NULL)
return (NULL);
while (philo->start_time > ft_time())
usleep(500);
while (!dead_loop(philo))
{
if (check_dead(philo) == false)
if (dead_loop(philo) == false)
ft_start_eating(philo);
if (check_dead(philo) == false)
if (dead_loop(philo) == false)
ft_sleeping(philo);
if (check_dead(philo) == false)
if (dead_loop(philo) == false)
ft_thinking(philo);
}
return (philo);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
if (tmp == 0)
return (ERROR);
philo->nb_philo = tmp;
philo->nb_eat = ~0u;
if (argc == 6)
{
ft_atou(argv[5], &tmp);
if (tmp == 0)
return (ERROR);
philo->nb_eat = tmp;
}
else
philo->nb_eat = ~0u;
philo->nb_philo = tmp;
ft_atou(argv[2], &tmp);
philo->t_death = tmp;
ft_atou(argv[3], &tmp);
@ -47,7 +48,6 @@ t_error ft_init_fork(t_mutex *fork, t_usize nb_fork)
i = 0;
while (i < nb_fork)
{
printf("\tforks[%zu]\n", i + 1);
fork[i] = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
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_usize i;
t_usize start_time;
start_time = ft_time() + 500;
i = 0;
while (i < ft_atou_return(argv[1]))
{
prog->philos[i].id = i + 1;
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].print_lock = &prog->print_lock;
prog->philos[i].dead_lock = &prog->dead_lock;
@ -83,22 +85,19 @@ 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];
else
prog->philos[i].r_fork = &forks[i - 1];
printf("\tphilo[%zu]\n", i + 1);
i++;
}
if (ft_parsing_args(argc, argv, prog->philos))
return (ERROR);
return (NO_ERROR);
return (ft_parsing_args(argc, argv, prog->philos));
}
t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo)
{
static t_mutex forks[MAXSUPPORT] = {0};
prog->print_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
prog->dead_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
prog->meal_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
prog->check_eating_count = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
prog->print_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
prog->dead_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
prog->meal_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
prog->check_eating_count = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
prog->philos = philo;
prog->dead_flag = false;
ft_init_fork(forks, ft_atou_return(argv[1]));

View file

@ -6,16 +6,17 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdio.h>
t_bool dead_loop(t_philo *philo)
{
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);
pthread_mutex_unlock(philo->dead_lock);
return (false);
@ -80,6 +81,8 @@ void *ft_watch_dogs(void *ptr)
philo = (t_philo *) ptr;
if (philo == NULL)
return (NULL);
while (philo[0].start_time > ft_time())
usleep(500);
i = philo[0].nb_philo;
while (true)
{