pars_arg.c \
inner_pars_arg.c \
mem_management.c \
- hunger_watcher.c \
+ watchers.c \
libft_helpers.c \
SRCS := $(addprefix $(SRCDIR)/, $(SRCS))
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */
-/* Updated: 2024/05/30 10:15:37 by ljiriste ### ########.fr */
+/* Updated: 2024/05/30 11:41:40 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
check_input.semaphores = semaphores;
pthread_create(&thread_fed, NULL, check_fed, &check_input);
sem_wait(semaphores->death);
+ sem_post(semaphores->death);
sem_wait(semaphores->check_end);
end = 1;
sem_post(semaphores->check_end);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 08:45:21 by ljiriste #+# #+# */
-/* Updated: 2024/05/24 15:22:54 by ljiriste ### ########.fr */
+/* Updated: 2024/05/30 11:40:22 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
philo->semaphores.fed = sem_open(WELL_FED_SEM, 0);
philo->semaphores.death = sem_open(DEATH_SEM, 0);
philo->semaphores.term = sem_open(TERM_SEM, 0);
+ philo->semaphores.end = sem_open(END_SEM, 0);
sem_name = create_philo_sem_name(philo->id);
if (!sem_name)
return (NULL);
void be_a_philosopher(t_philo *philo)
{
- pthread_t hunger_watcher;
+ pthread_t watchers[2];
char *philo_sem_name;
philo_sem_name = open_semaphores(philo);
if (!philo_sem_name)
exit(2);
- if (create_hunger_watcher(philo, &hunger_watcher))
+ if (create_watchers(philo, watchers))
exit(3);
while (is_alive(philo))
{
philo_eat(philo);
philo_sleep(philo);
}
- destroy_hunger_watcher(hunger_watcher);
+ destroy_watchers(watchers);
close_semaphores(&philo->semaphores);
sem_unlink(philo_sem_name);
free(philo_sem_name);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */
-/* Updated: 2024/05/30 10:10:11 by ljiriste ### ########.fr */
+/* Updated: 2024/05/30 10:40:40 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
void print_timestamp(t_philo *philo, const char *str);
char *create_philo_sem_name(unsigned int id);
-int create_hunger_watcher(t_philo *philo, pthread_t *hunger_watcher);
-void destroy_hunger_watcher(pthread_t hunger_watcher);
+int create_watchers(t_philo *philo, pthread_t watchers[2]);
+void destroy_watchers(pthread_t watchers[2]);
size_t ft_strlen(const char *str);
char *ft_uitoa_base(uintmax_t n, const char *base);
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* hunger_watcher.c :+: :+: :+: */
+/* watchers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 08:49:46 by ljiriste #+# #+# */
-/* Updated: 2024/05/30 10:00:31 by ljiriste ### ########.fr */
+/* Updated: 2024/05/30 11:39:51 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
time = time_till_death(philo);
while (time > 0)
{
+ if (!philo->alive)
+ {
+ sem_post(philo->philo_sem);
+ return (NULL);
+ }
sem_post(philo->philo_sem);
usleep(time / 2);
sem_wait(philo->philo_sem);
return (NULL);
}
-int create_hunger_watcher(t_philo *philo, pthread_t *hunger_watcher)
+void *watch_for_end(void *philo_void)
{
- return (pthread_create(hunger_watcher, NULL, watch_for_starvation, philo));
+ t_philo *philo;
+
+ philo = (t_philo *)philo_void;
+ sem_wait(philo->semaphores.death);
+ sem_post(philo->semaphores.death);
+ sem_wait(philo->philo_sem);
+ philo->alive = 0;
+ sem_post(philo->philo_sem);
+ sem_post(philo->semaphores.end);
+ return (NULL);
+}
+
+int create_watchers(t_philo *philo, pthread_t watchers[2])
+{
+ if (pthread_create(&watchers[0], NULL, watch_for_starvation, philo))
+ return (1);
+ if (pthread_create(&watchers[1], NULL, watch_for_end, philo))
+ {
+ sem_wait(philo->philo_sem);
+ philo->alive = 0;
+ sem_post(philo->philo_sem);
+ pthread_join(watchers[0], NULL);
+ return (1);
+ }
+ return (0);
}
-void destroy_hunger_watcher(pthread_t hunger_watcher)
+void destroy_watchers(pthread_t watchers[2])
{
- pthread_join(hunger_watcher, NULL);
+ pthread_join(watchers[0], NULL);
+ pthread_join(watchers[1], NULL);
return ;
}