Browse Source

fix final inversion and death

master
narnaud 3 years ago
parent
commit
ca08727006
  1. 2
      philo/Makefile
  2. 10
      philo/philo.c
  3. 3
      philo/philo_init.c
  4. 6
      philo/philo_life.c

2
philo/Makefile

@ -4,7 +4,7 @@ SRCS = philo.c philo_init.c philo_life.c philo_utils.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
CC = clang CC = clang
CFLAGS = -Werror -Wextra -Wall -g -D_REENTRANT CFLAGS = -Werror -Wextra -Wall -D_REENTRANT
RM = rm -rf RM = rm -rf
%.o : %.c %.o : %.c

10
philo/philo.c

@ -6,7 +6,7 @@
/* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */ /* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/19 00:36:57 by narnaud@stude #+# #+# */ /* 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); pthread_mutex_lock(room->lock);
room->running = 0; room->running = 0;
pthread_mutex_unlock(room->lock); pthread_mutex_unlock(room->lock);
sleep(2); sleep(1);
return (clean_memory(room, philos)); return (clean_memory(room, philos));
} }
@ -45,9 +45,9 @@ static int waiter(t_room *room, t_philo *philo)
while (ret && philo->id >= 0) while (ret && philo->id >= 0)
{ {
pthread_mutex_lock(philo->lock); 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; ret = 0;
} }
if (philo->eat_amount < starvest) if (philo->eat_amount < starvest)
@ -65,7 +65,6 @@ static int clean_memory(t_room *room, t_philo *philos)
{ {
static int i = 0; static int i = 0;
pthread_mutex_lock(room->lock);
while (i < room->param[PHILO_AMOUNT]) while (i < room->param[PHILO_AMOUNT])
{ {
pthread_mutex_lock(philos[i].lock); pthread_mutex_lock(philos[i].lock);
@ -78,7 +77,6 @@ static int clean_memory(t_room *room, t_philo *philos)
free(philos[i].thd); free(philos[i].thd);
i++; i++;
} }
pthread_mutex_unlock(room->lock);
pthread_mutex_destroy(room->lock); pthread_mutex_destroy(room->lock);
free(room->lock); free(room->lock);
free(room->forks); free(room->forks);

3
philo/philo_init.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/24 15:44:04 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_create(philo->thd, NULL, philos_life, (void *)philo);
pthread_detach(*philo->thd); pthread_detach(*philo->thd);
usleep(100);
if ((i % 2) == 0) if ((i % 2) == 0)
{ {
i += 2; i += 2;

6
philo/philo_life.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/26 07:43:48 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")) if (!safe_print(philo, "%d %d is thinking\n"))
break ; break ;
} }
if (stop--) if (stop > 0)
pthread_mutex_unlock(philo->fork); pthread_mutex_unlock(philo->fork);
if (stop--) if (stop > 1)
pthread_mutex_unlock(philo->fork_r); pthread_mutex_unlock(philo->fork_r);
return (NULL); return (NULL);
} }

Loading…
Cancel
Save