From ca087270061a21359d64d2c6a7106e92542eb91a Mon Sep 17 00:00:00 2001 From: narnaud Date: Thu, 19 May 2022 01:42:16 +0200 Subject: [PATCH] fix final inversion and death --- philo/Makefile | 2 +- philo/philo.c | 10 ++++------ philo/philo_init.c | 3 +-- philo/philo_life.c | 6 +++--- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/philo/Makefile b/philo/Makefile index d65e4a9..9d48079 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -4,7 +4,7 @@ SRCS = philo.c philo_init.c philo_life.c philo_utils.c OBJS = ${SRCS:.c=.o} CC = clang -CFLAGS = -Werror -Wextra -Wall -g -D_REENTRANT +CFLAGS = -Werror -Wextra -Wall -D_REENTRANT RM = rm -rf %.o : %.c diff --git a/philo/philo.c b/philo/philo.c index 7dafe04..104ab72 100644 --- a/philo/philo.c +++ b/philo/philo.c @@ -6,7 +6,7 @@ /* By: narnaud@student.42nice.fr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/19 00:36:57 by narnaud@stude #+# #+# */ -/* Updated: 2022/05/19 00:47:15 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/19 01:41:03 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ int main(int argc, char **argv) pthread_mutex_lock(room->lock); room->running = 0; pthread_mutex_unlock(room->lock); - sleep(2); + sleep(1); return (clean_memory(room, philos)); } @@ -45,9 +45,9 @@ static int waiter(t_room *room, t_philo *philo) while (ret && philo->id >= 0) { pthread_mutex_lock(philo->lock); - if (philo->eat_time + room->param[DIE_TIME] < room_clock()) + if ((philo->eat_time + room->param[DIE_TIME]) < room_clock()) { - safe_print(philo, "%d %d died\n"); + printf("%d %d died\n", room_clock(), philo->id); ret = 0; } if (philo->eat_amount < starvest) @@ -65,7 +65,6 @@ static int clean_memory(t_room *room, t_philo *philos) { static int i = 0; - pthread_mutex_lock(room->lock); while (i < room->param[PHILO_AMOUNT]) { pthread_mutex_lock(philos[i].lock); @@ -78,7 +77,6 @@ static int clean_memory(t_room *room, t_philo *philos) free(philos[i].thd); i++; } - pthread_mutex_unlock(room->lock); pthread_mutex_destroy(room->lock); free(room->lock); free(room->forks); diff --git a/philo/philo_init.c b/philo/philo_init.c index 5272209..0b46f85 100644 --- a/philo/philo_init.c +++ b/philo/philo_init.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/24 15:44:04 by narnaud #+# #+# */ -/* Updated: 2022/05/19 00:47:38 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/19 01:33:29 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,7 +68,6 @@ void invit_philos(t_philo *philos) { pthread_create(philo->thd, NULL, philos_life, (void *)philo); pthread_detach(*philo->thd); - usleep(100); if ((i % 2) == 0) { i += 2; diff --git a/philo/philo_life.c b/philo/philo_life.c index 5abf433..404996a 100644 --- a/philo/philo_life.c +++ b/philo/philo_life.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/26 07:43:48 by narnaud #+# #+# */ -/* Updated: 2022/05/19 00:48:41 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/19 01:23:29 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,9 +52,9 @@ void *philos_life(void *ph) if (!safe_print(philo, "%d %d is thinking\n")) break ; } - if (stop--) + if (stop > 0) pthread_mutex_unlock(philo->fork); - if (stop--) + if (stop > 1) pthread_mutex_unlock(philo->fork_r); return (NULL); }