Plus de message après la mort, plus de .swp

This commit is contained in:
Etienne Rey-bethbeder 2023-04-01 12:16:42 +02:00
parent 4d97d3801e
commit 9f5f416a77
7 changed files with 30 additions and 14 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 14:51:16 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_die_eating(t_philo *philo, t_only *only, t_config *config);
int verif_finish(t_philo *philo, t_only *only); int verif_finish(t_philo *philo, t_only *only);
int finish(t_philo *philo, long long last_eat_time); 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 init_mutex(pthread_mutex_t *mutex, int nbr);
void destroy_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); void free_all(pthread_t *threads, t_same *same, t_philo **philo);

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 20:08:33 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 if (only->id % 2 == 1 || (*nbr_philo % 2 == 1
&& only->id == *nbr_philo - 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 if (get_utime() - only->last_eat_time + config->time_eat
>= config->time_die) >= config->time_die)
usleep(config->time_die - (get_utime() - only->last_eat_time)); 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; *philo->same->all_eat += 1;
pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 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->next_id]);
pthread_mutex_unlock(&philo->same->mutex[only->id]); pthread_mutex_unlock(&philo->same->mutex[only->id]);
if (get_utime() - only->last_eat_time + config->time_sleep 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) if (!finish(philo, only->last_eat_time)
&& !pthread_mutex_lock(&philo->same->mutex[only->id])) && !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 while (only->id == only->next_id
&& !finish(philo, only->last_eat_time)) && !finish(philo, only->last_eat_time))
; ;
@ -101,8 +101,8 @@ void loop_philosopher(t_philo *philo, t_only *only,
{ {
if (verif_finish(philo, only)) if (verif_finish(philo, only))
break ; break ;
message(only->time, only->id, "has taken a fork\n"); message(philo, "has taken a fork\n");
message(only->time, only->id, "is eating\n"); message(philo, "is eating\n");
if (!in_loop_eat_sleep(philo, only, config, nbr_philo)) if (!in_loop_eat_sleep(philo, only, config, nbr_philo))
break ; break ;
} }
@ -110,7 +110,7 @@ void loop_philosopher(t_philo *philo, t_only *only,
pthread_mutex_unlock(&philo->same->mutex[only->id]); pthread_mutex_unlock(&philo->same->mutex[only->id]);
} }
if (!finish(philo, only->last_eat_time)) 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))) || (config->must_eat > -1 && only->eat < config->must_eat)))
{ {
*philo->same->death = 1; *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]);
pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]); pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]);

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 20:44:51 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); 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]; char send[1024];
int i; int i;
i = ft_lltos(send, (get_utime() - time) / 1000); i = ft_lltos(send, (get_utime() - philo->only->time) / 1000);
send[i++] = ' '; send[i++] = ' ';
i += ft_lltos(send + i, id + 1); i += ft_lltos(send + i, philo->only->id + 1);
send[i++] = ' '; send[i++] = ' ';
while (*msg) while (*msg)
send[i++] = *msg++; send[i++] = *msg++;
write(1, send, i); 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) int verif_finish(t_philo *philo, t_only *only)
{ {
if (finish(philo, only->last_eat_time)) if (finish(philo, only->last_eat_time))