diff --git a/philo_bonus/philo.c b/philo_bonus/philo.c index e73aca8..142bc1b 100644 --- a/philo_bonus/philo.c +++ b/philo_bonus/philo.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/16 14:03:42 by narnaud #+# #+# */ -/* Updated: 2022/05/20 10:34:10 by narnaud ### ########.fr */ +/* Updated: 2022/05/20 11:58:25 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,10 +24,11 @@ void *check_halt(void *p) time = gettime(); if (time - philo->last_meal_time > philo->table->param[DIE_TIME]) { - printf("%i %d died\n", time, philo->id); + safe_print(philo, "%i %d died\n"); exit(EXIT_SUCCESS); } - if (philo->meals_done == philo->table->param[MEALS_AMOUNT]) + if (philo->meals_done == philo->table->param[MEALS_AMOUNT] + && philo->id == philo->table->param[PHILO_AMOUNT] - 1) exit(EXIT_SUCCESS); } } @@ -40,18 +41,18 @@ void philo_life(t_philo *philo) while (1) { sem_wait(table->sptr); - printf("%i %d has taken a fork\n", gettime(), philo->id); + safe_print(philo, "%i %d has taken a fork\n"); sem_wait(table->sptr); - printf("%i %d has taken a fork\n", gettime(), philo->id); + safe_print(philo, "%i %d has taken a fork\n"); philo->last_meal_time = gettime(); - printf("%i %d is eating\n", gettime(), philo->id); + safe_print(philo, "%i %d is eating\n"); usleep(table->param[EAT_DURATION] * 1000); philo->meals_done++; sem_post(table->sptr); sem_post(table->sptr); - printf("%i %d is sleeping\n", gettime(), philo->id); + safe_print(philo, "%i %d is sleeping\n"); usleep(table->param[SLEEP_DURATION] * 1000); - printf("%i %d is thinking\n", gettime(), philo->id); + safe_print(philo, "%i %d is thinking\n"); } } @@ -91,9 +92,10 @@ int main(int argc, char **argv) if (argc < 5 || argc > 6) return (1); table = parsing(argv + 1); + table->talk = sem_open("/talk", O_CREAT, 0664, + 1); table->sptr = sem_open("/chopsticks", O_CREAT, 0664, table->param[PHILO_AMOUNT]); - table->death = sem_open("/death", O_CREAT, 0664, 1); init_philos(table); waitpid(-1, &status, 0); while (i < table->param[PHILO_AMOUNT]) @@ -102,6 +104,6 @@ int main(int argc, char **argv) i++; } sem_unlink("/chopsticks"); - sem_unlink("/deaths"); + sem_unlink("/talk"); return (0); } diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index 090baee..20e7704 100644 --- a/philo_bonus/philo.h +++ b/philo_bonus/philo.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/16 14:03:01 by narnaud #+# #+# */ -/* Updated: 2022/05/20 10:38:02 by narnaud ### ########.fr */ +/* Updated: 2022/05/20 11:51:03 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,7 +50,7 @@ typedef struct s_table int param[5]; int i; sem_t *sptr; - sem_t *death; + sem_t *talk; t_philo *philos; int time; int *pid; @@ -60,5 +60,6 @@ typedef struct s_table t_table *parsing(char **argv); int gettime(void); +void safe_print(t_philo *philo, char *str); #endif diff --git a/philo_bonus/philo_utils.c b/philo_bonus/philo_utils.c index 23ce292..1e0e463 100644 --- a/philo_bonus/philo_utils.c +++ b/philo_bonus/philo_utils.c @@ -6,12 +6,19 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/16 14:04:06 by narnaud #+# #+# */ -/* Updated: 2022/05/20 10:36:06 by narnaud ### ########.fr */ +/* Updated: 2022/05/20 11:58:51 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" +void safe_print(t_philo *philo, char *str) +{ + sem_wait(philo->table->talk); + printf(str, gettime(), philo->id); + sem_post(philo->table->talk); +} + static int mini_atoi(char *nbr) { int ret;