/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* philo_threads.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/26 07:43:48 by narnaud #+# #+# */ /* Updated: 2022/05/18 18:26:14 by narnaud@stude ### ########.fr */ /* */ /* ************************************************************************** */ #include "philosophers.h" int waiter(t_room *room) { int *param; int i; int starvest; t_philo *philo; int ret; ret = 1; pthread_mutex_lock(room->lock); i = 0; param = room->param; starvest = param[MEALS_AMOUNT]; while (ret && i < param[PHILO_AMOUNT]) { philo = room->philos[i]; pthread_mutex_lock(philo->lock); if (philo->eat_time + param[DIE_TIME] < room_clock()) { printf("%d %d died\n", room_clock(), i); ret = 0; } if (philo->eat_amount < starvest) starvest = philo->eat_amount; pthread_mutex_unlock(philo->lock); i++; } pthread_mutex_unlock(room->lock); if (starvest == param[MEALS_AMOUNT]) ret = 0; return (ret); } static int philo_life(t_room *room, t_philo *philo, int id, int id_p, int *param) { pthread_mutex_lock(philo->fork); if (!safe_print("%d %d has taken a fork\n", room, id)) return (1); pthread_mutex_lock(room->philos[id_p]->fork); safe_print("%d %d has taken a fork\n", room, id); if(!safe_print("%d %d is eating\n", room, id)) return (2); pthread_mutex_lock(philo->lock); philo->eat_time = room_clock(); pthread_mutex_unlock(philo->lock); if (!safe_wait(room, room_clock(), param[EAT_DURATION])) return (2); pthread_mutex_unlock(philo->fork); pthread_mutex_unlock(room->philos[id_p]->fork); if(!safe_print("%d %d is sleeping\n", room, id)) return (-1); pthread_mutex_lock(philo->lock); philo->eat_amount++; pthread_mutex_unlock(philo->lock); if (!safe_wait(room, room_clock(), param[SLEEP_DURATION])) return (-1); if (!safe_print("%d %d is thinking\n", room, id)) return (-1); return (0); } void *philos_birth(void *r) { t_room *room; int *param; t_philo *philo; int id; int id_p; int stop; stop = 0; room = (t_room *)r; param = room->param; pthread_mutex_lock(room->lock); id = room->i; printf("Philo %d: \n", id); philo = room->philos[id]; philo->eat_time = room_clock(); id_p = (id + 1) % param[PHILO_AMOUNT]; pthread_mutex_unlock(room->lock); while (!stop) stop = philo_life(room, philo, id, id_p, param); if (stop-- > 0) pthread_mutex_unlock(philo->fork); if (stop-- > 0) pthread_mutex_unlock(room->philos[id_p]->fork); return (NULL); }