This commit is contained in:
Raphaël 2024-07-24 16:18:49 +02:00
parent 9285a20e35
commit 07dae4049c
3 changed files with 17 additions and 7 deletions

View file

@ -6,11 +6,12 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */
/* Updated: 2024/07/24 14:16:00 by rparodi ### ########.fr */
/* Updated: 2024/07/24 16:09:21 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo.h"
#include <unistd.h>
void ft_logs(t_str msg, t_philo *philo)
{
@ -45,7 +46,7 @@ t_error ft_start_eating(t_philo *philo)
usleep(50);
return (NO_ERROR);
}
if (philo->id % 2 == 0)
if (philo->id % 2 == 1)
{
pthread_mutex_lock(philo->r_fork);
ft_logs("has taken a fork\n", philo);
@ -56,8 +57,6 @@ t_error ft_start_eating(t_philo *philo)
{
pthread_mutex_lock(philo->l_fork);
ft_logs("has taken a fork\n", philo);
if (philo->nb_philo == 1)
return ((void)pthread_mutex_unlock(philo->l_fork), NO_ERROR);
pthread_mutex_lock(philo->r_fork);
ft_logs("has taken a fork\n", philo);
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */
/* Updated: 2024/07/24 14:07:27 by rparodi ### ########.fr */
/* Updated: 2024/07/24 16:15:16 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,12 +15,18 @@
void *ft_routine(void *ptr)
{
t_philo *philo;
t_usize nb_group;
nb_group = 2;
philo = (t_philo *) ptr;
if (philo == NULL)
return (NULL);
if (philo->nb_philo % 2)
nb_group = 3;
while (philo->start_time > ft_time())
usleep(500);
if (philo->id % nb_group)
usleep(philo->t_eat * 800);
while (!dead_loop(philo))
{
if (dead_loop(philo) == false)

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */
/* Updated: 2024/07/24 14:23:03 by rparodi ### ########.fr */
/* Updated: 2024/07/24 16:05:12 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -80,11 +80,16 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks)
prog->philos[i].meal_lock = &prog->meal_lock;
prog->philos[i].dead_check = false;
prog->philos[i].check_eating_count = &prog->check_eating_count;
prog->philos[i].l_fork = &forks[i];
if (i == 0)
prog->philos[i].r_fork = &forks[prog->philos[i].nb_philo - 1];
else
prog->philos[i].r_fork = &forks[i - 1];
if (prog->philos[0].nb_philo % 2 == 1 && i == prog->philos[0].nb_philo)
{
prog->philos[i].l_fork = &forks[0];
prog->philos[i].r_fork = &forks[i];
}
prog->philos[i].l_fork = &forks[i];
i++;
}
return (ft_parsing_args(argc, argv, prog->philos));