diff --git a/.Makefile.swp b/.Makefile.swp deleted file mode 100644 index 8fc8cc0..0000000 Binary files a/.Makefile.swp and /dev/null differ diff --git a/mandatory/.philo.c.swp b/mandatory/.philo.c.swp deleted file mode 100644 index d67e480..0000000 Binary files a/mandatory/.philo.c.swp and /dev/null differ diff --git a/mandatory/.threads.c.swo b/mandatory/.threads.c.swo deleted file mode 100644 index 17f0d84..0000000 Binary files a/mandatory/.threads.c.swo and /dev/null differ diff --git a/mandatory/.threads.c.swp b/mandatory/.threads.c.swp deleted file mode 100644 index 341ed2b..0000000 Binary files a/mandatory/.threads.c.swp and /dev/null differ diff --git a/mandatory/philo.h b/mandatory/philo.h index e11d6ab..fe0fdb2 100644 --- a/mandatory/philo.h +++ b/mandatory/philo.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */ -/* Updated: 2023/03/31 17:47:02 by erey-bet ### ########.fr */ +/* Updated: 2023/04/01 11:58:47 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,7 +66,8 @@ long long get_utime(void); int verif_die_eating(t_philo *philo, t_only *only, t_config *config); int verif_finish(t_philo *philo, t_only *only); int finish(t_philo *philo, long long last_eat_time); -void message(long long time, int id, char *msg); +void message_die(t_philo *philo, char *msg); +void message(t_philo *philo, char *msg); void init_mutex(pthread_mutex_t *mutex, int nbr); void destroy_mutex(pthread_mutex_t *mutex, int nbr); void free_all(pthread_t *threads, t_same *same, t_philo **philo); diff --git a/mandatory/threads.c b/mandatory/threads.c index 4c4b9ec..e0fd093 100644 --- a/mandatory/threads.c +++ b/mandatory/threads.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */ -/* Updated: 2023/03/31 18:24:55 by erey-bet ### ########.fr */ +/* Updated: 2023/04/01 11:57:59 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ void init_philo(t_philo *philo, t_only *only, if (only->id % 2 == 1 || (*nbr_philo % 2 == 1 && only->id == *nbr_philo - 1)) { - message(only->time, only->id, "is thinking\n"); + message(philo, "is thinking\n"); if (get_utime() - only->last_eat_time + config->time_eat >= config->time_die) usleep(config->time_die - (get_utime() - only->last_eat_time)); @@ -70,7 +70,7 @@ int in_loop_eat_sleep(t_philo *philo, t_only *only, *philo->same->all_eat += 1; pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]); } - message(only->time, only->id, "is sleeping\n"); + message(philo, "is sleeping\n"); pthread_mutex_unlock(&philo->same->mutex[only->next_id]); pthread_mutex_unlock(&philo->same->mutex[only->id]); if (get_utime() - only->last_eat_time + config->time_sleep @@ -92,7 +92,7 @@ void loop_philosopher(t_philo *philo, t_only *only, if (!finish(philo, only->last_eat_time) && !pthread_mutex_lock(&philo->same->mutex[only->id])) { - message(only->time, only->id, "has taken a fork\n"); + message(philo, "has taken a fork\n"); while (only->id == only->next_id && !finish(philo, only->last_eat_time)) ; @@ -101,8 +101,8 @@ void loop_philosopher(t_philo *philo, t_only *only, { if (verif_finish(philo, only)) break ; - message(only->time, only->id, "has taken a fork\n"); - message(only->time, only->id, "is eating\n"); + message(philo, "has taken a fork\n"); + message(philo, "is eating\n"); if (!in_loop_eat_sleep(philo, only, config, nbr_philo)) break ; } @@ -110,7 +110,7 @@ void loop_philosopher(t_philo *philo, t_only *only, pthread_mutex_unlock(&philo->same->mutex[only->id]); } if (!finish(philo, only->last_eat_time)) - message(only->time, only->id, "is thinking\n"); + message(philo, "is thinking\n"); } } @@ -130,7 +130,7 @@ void *philosopher(t_philo *philo) || (config->must_eat > -1 && only->eat < config->must_eat))) { *philo->same->death = 1; - message(only->time, only->id, "died\n"); + message_die(philo, "died\n"); } pthread_mutex_unlock(&philo->same->mutex[nbr_philo]); pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]); diff --git a/mandatory/utils_threads.c b/mandatory/utils_threads.c index 9edffde..50df9d9 100644 --- a/mandatory/utils_threads.c +++ b/mandatory/utils_threads.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/29 20:44:51 by erey-bet #+# #+# */ -/* Updated: 2023/03/30 18:10:31 by erey-bet ### ########.fr */ +/* Updated: 2023/04/01 12:08:51 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,20 +20,35 @@ long long get_utime(void) return (time.tv_sec * 1000000 + time.tv_usec); } -void message(long long time, int id, char *msg) +void message_die(t_philo *philo, char *msg) { char send[1024]; int i; - i = ft_lltos(send, (get_utime() - time) / 1000); + i = ft_lltos(send, (get_utime() - philo->only->time) / 1000); send[i++] = ' '; - i += ft_lltos(send + i, id + 1); + i += ft_lltos(send + i, philo->only->id + 1); send[i++] = ' '; while (*msg) send[i++] = *msg++; write(1, send, i); } +void message(t_philo *philo, char *msg) +{ + char send[1024]; + int i; + + i = ft_lltos(send, (get_utime() - philo->only->time) / 1000); + send[i++] = ' '; + i += ft_lltos(send + i, philo->only->id + 1); + send[i++] = ' '; + while (*msg) + send[i++] = *msg++; + if (!finish(philo, philo->only->last_eat_time)) + write(1, send, i); +} + int verif_finish(t_philo *philo, t_only *only) { if (finish(philo, only->last_eat_time))