/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */ /* Updated: 2021/01/02 12:07:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_three.h" static sem_t *st_sem_create(char *name, unsigned int value) { sem_unlink(name); return (sem_open(name, O_CREAT | O_EXCL, 0700, value)); } static int st_destroy(t_sems *sems, pid_t *pids, int philo_num) { int i; if (pids != NULL) { i = -1; while (++i < philo_num) kill(pids[i], SIGKILL); free(pids); } sem_close(sems->forks); sem_unlink(PHILO_SEM_NAME); sem_close(sems->sem_stdout); sem_unlink(PHILO_SEM_STDOUT_NAME); sem_close(sems->sem_finish); sem_unlink(PHILO_SEM_FINISH_NAME); return (1); } static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids) { t_philo philo; int i; sems->sem_stdout = SEM_FAILED; sems->sem_finish = SEM_FAILED; *pids = NULL; if ((sems->forks = st_sem_create(PHILO_SEM_NAME, args->philo_num)) == SEM_FAILED || (sems->sem_stdout = st_sem_create(PHILO_SEM_STDOUT_NAME, 1)) == SEM_FAILED || (sems->sem_finish = st_sem_create(PHILO_SEM_FINISH_NAME, args->meal_num == -1 ? 1 : args->philo_num)) == SEM_FAILED || (*pids = malloc(sizeof(pid_t) * args->philo_num)) == NULL) return (st_destroy(sems, *pids, 0)); i = -1; while (++i < args->philo_num) { philo.conf = args; philo.id = i + 1; if (((*pids)[i] = child_start(&philo)) == -1) return (st_destroy(sems, *pids, i)); usleep(200); } return (0); } static void st_wait(t_philo_args *args, t_sems *sems) { long int i; if (args->meal_num == -1) { sem_wait(sems->sem_finish); sem_wait(sems->sem_finish); } else { i = -1; while (++i < args->philo_num) sem_wait(sems->sem_finish); i = -1; while (++i < args->philo_num) sem_wait(sems->sem_finish); sem_wait(sems->sem_stdout); } } int main(int argc, char **argv) { t_philo_args args; t_sems sems; pid_t *pids; if (!parse_args(&args, argc, argv)) return (1); if (args.philo_num == 0 || args.meal_num == 0) return (0); if (st_setup(&args, &sems, &pids) != 0) return (1); st_wait(&args, &sems); st_destroy(&sems, pids, args.philo_num); return (0); }