/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 08:49:46 by ljiriste #+# #+# */
-/* Updated: 2024/05/24 15:33:10 by ljiriste ### ########.fr */
+/* Updated: 2024/05/30 10:00:31 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
sem_wait(philo->philo_sem);
time = time_till_death(philo);
}
+ sem_wait(philo->semaphores.term);
+ print_timestamp(philo, "died");
philo->alive = 0;
sem_post(philo->semaphores.death);
sem_post(philo->philo_sem);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */
-/* Updated: 2024/05/24 15:32:12 by ljiriste ### ########.fr */
+/* Updated: 2024/05/30 10:15:37 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
}
}
-void handle_philosophers(__attribute__((unused)) t_sems *semaphores)
+void *check_fed(void *input)
{
- sleep(5);
+ unsigned int philo_count;
+ int *end;
+ t_sems *semaphores;
+
+ philo_count = ((t_check *)input)->philo_count;
+ semaphores = ((t_check *)input)->semaphores;
+ end = ((t_check *)input)->end;
+ while (philo_count > 0)
+ {
+ sem_wait(semaphores->fed);
+ --philo_count;
+ }
+ sem_wait(semaphores->check_end);
+ if (!*end)
+ {
+ sem_wait(semaphores->term);
+ printf("All philosophers are well fed\n");
+ *end = 1;
+ }
+ sem_post(semaphores->check_end);
+ sem_post(semaphores->death);
+ return (NULL);
+}
+
+void wait_for_end(unsigned int philo_count, t_sems *semaphores)
+{
+ pthread_t thread_fed;
+ int end;
+ t_check check_input;
+
+ check_input.end = &end;
+ check_input.philo_count = philo_count;
+ check_input.semaphores = semaphores;
+ pthread_create(&thread_fed, NULL, check_fed, &check_input);
+ sem_wait(semaphores->death);
+ sem_wait(semaphores->check_end);
+ end = 1;
+ sem_post(semaphores->check_end);
+ while (philo_count > 0)
+ {
+ sem_wait(semaphores->end);
+ sem_post(semaphores->fed);
+ --philo_count;
+ }
+ sem_post(semaphores->term);
+ pthread_join(thread_fed, NULL);
return ;
}
return (3);
}
sem_post(semaphores.term);
- handle_philosophers(&semaphores);
+ wait_for_end(settings.philo_count, &semaphores);
cleanup(philo_pids, &semaphores);
return (0);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 09:39:55 by ljiriste #+# #+# */
-/* Updated: 2024/05/24 15:22:19 by ljiriste ### ########.fr */
+/* Updated: 2024/05/30 11:18:25 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
S_IRUSR | S_IWUSR, 0);
semaphores->end = sem_open(END_SEM, O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR, 0);
- if (semaphores->forks == SEM_FAILED || semaphores->fed == SEM_FAILED
- || semaphores->death == SEM_FAILED || semaphores->term == SEM_FAILED
- || semaphores->end == SEM_FAILED)
+ semaphores->check_end = sem_open(CHECK_END_SEM, O_CREAT | O_EXCL,
+ S_IRUSR | S_IWUSR, 1);
+ if (semaphores->forks == SEM_FAILED
+ || semaphores->fed == SEM_FAILED
+ || semaphores->death == SEM_FAILED
+ || semaphores->term == SEM_FAILED
+ || semaphores->end == SEM_FAILED
+ || semaphores->check_end == SEM_FAILED)
return (1);
return (0);
}
sem_close(semaphores->death);
sem_close(semaphores->term);
sem_close(semaphores->end);
+ sem_close(semaphores->check_end);
return ;
}
sem_unlink(DEATH_SEM);
sem_unlink(TERM_SEM);
sem_unlink(END_SEM);
+ sem_unlink(CHECK_END_SEM);
free(philo_pids);
return ;
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */
-/* Updated: 2024/05/24 15:23:21 by ljiriste ### ########.fr */
+/* Updated: 2024/05/30 10:10:11 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
# define DEATH_SEM "philosophers_death"
# define TERM_SEM "philosophers_terminal"
# define END_SEM "philosophers_end"
+# define CHECK_END_SEM "philosophers_check_end"
// The .start member holds the result of gettimeofday
// Every other member that holds time info counts
sem_t *death;
sem_t *term;
sem_t *end;
+ sem_t *check_end;
} t_sems;
// As mutex is not available in bonus, the idea is to have
t_settings settings;
} t_philo;
+typedef struct s_check
+{
+ unsigned int philo_count;
+ int *end;
+ t_sems *semaphores;
+} t_check;
+
int parse_input(t_settings *settings, int argc, char **argv);
int init(unsigned int count, pid_t **philo_pids,