diff options
Diffstat (limited to 'philo_three/src')
| -rw-r--r-- | philo_three/src/main.c | 94 | ||||
| -rw-r--r-- | philo_three/src/philo_three.h | 9 |
2 files changed, 66 insertions, 37 deletions
diff --git a/philo_three/src/main.c b/philo_three/src/main.c index 04b1069..d6749d8 100644 --- a/philo_three/src/main.c +++ b/philo_three/src/main.c @@ -6,62 +6,84 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */ -/* Updated: 2020/10/05 14:31:39 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 16:59:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_three.h" -sem_t *philo_sem_create(char *name, int value) +static sem_t *st_sem_create(char *name, int value) { sem_t *sem; sem_unlink(name); sem = sem_open(name, O_CREAT | O_EXCL, 0700, value); if (sem == SEM_FAILED) - return (NULL); + return (SEM_FAILED); return (sem); } -int main(int argc, char **argv) +static int st_destroy(t_sems *sems, pid_t *pids, int philo_num) { - t_philo_args args; - t_philo philo; - sem_t *forks; - sem_t *sem_stdout; - sem_t *sem_dead; - pid_t *pids; - int i; + int i; - if (!parse_args(&args, argc, argv)) - return (1); - if ((forks = philo_sem_create(PHILO_SEM_NAME, args.philo_num)) == NULL - || (sem_stdout = philo_sem_create(PHILO_SEM_STDOUT_NAME, 1)) == NULL - || (sem_dead = philo_sem_create(PHILO_SEM_DEAD_NAME, 0)) == NULL) - return (1); - if ((pids = malloc(sizeof(pid_t) * args.philo_num)) == NULL) - return (1); - i = -1; - while (++i < args.philo_num) + if (pids != NULL) { - philo.conf = &args; - philo.id = i + 1; - if ((pids[i] = child_start(&philo)) == -1) - { - free(pids); - return (1); - } + i = -1; + while (++i < philo_num) + kill(pids[i], SIGKILL); + free(pids); } - sem_wait(sem_dead); - i = -1; - while (++i < args.philo_num) - kill(pids[i], SIGKILL); - free(pids); - sem_close(forks); - sem_close(sem_stdout); - sem_close(sem_dead); + sem_close(sems->forks); + sem_close(sems->sem_stdout); + sem_close(sems->sem_dead); sem_unlink(PHILO_SEM_NAME); sem_unlink(PHILO_SEM_STDOUT_NAME); sem_unlink(PHILO_SEM_DEAD_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_dead = 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_dead = + st_sem_create(PHILO_SEM_DEAD_NAME, 1)) == 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)); + } + return (0); +} + +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) + return (0); + if (st_setup(&args, &sems, &pids) != 0) + return (1); + sem_wait(sems.sem_dead); + sem_wait(sems.sem_dead); + st_destroy(&sems, pids, args.philo_num); return (0); } diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h index e30a857..1c1b8af 100644 --- a/philo_three/src/philo_three.h +++ b/philo_three/src/philo_three.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:46:26 by cacharle #+# #+# */ -/* Updated: 2020/10/05 15:05:26 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 16:54:29 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,13 @@ typedef struct s_philo sem_t *sem_dead; } t_philo; +typedef struct s_sems +{ + sem_t *forks; + sem_t *sem_stdout; + sem_t *sem_dead; +} t_sems; + pid_t child_start(t_philo *arg); void event_take_fork(t_philo *arg); |
