diff options
Diffstat (limited to 'philo_two/src')
| -rw-r--r-- | philo_two/src/event.c | 18 | ||||
| -rw-r--r-- | philo_two/src/main.c | 26 |
2 files changed, 31 insertions, 13 deletions
diff --git a/philo_two/src/event.c b/philo_two/src/event.c index faf963b..0eaad11 100644 --- a/philo_two/src/event.c +++ b/philo_two/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/01 14:26:02 by charles ### ########.fr */ +/* Updated: 2021/01/01 15:57:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,8 @@ void event_take_fork(t_philo *arg) if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_FORK); + if (philo_finished(arg->conf)) + return ; sem_post(arg->conf->sem_stdout); } @@ -34,6 +36,8 @@ void event_eat(t_philo *arg) if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_EAT); + if (philo_finished(arg->conf)) + return ; sem_post(arg->conf->sem_stdout); usleep(arg->conf->timeout_eat * 1000); } @@ -46,6 +50,8 @@ void event_think(t_philo *arg) if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_THINK); + if (philo_finished(arg->conf)) + return ; sem_post(arg->conf->sem_stdout); } @@ -57,9 +63,17 @@ void event_sleep(t_philo *arg) if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_SLEEP); + if (philo_finished(arg->conf)) + return ; sem_post(arg->conf->sem_stdout); + if (philo_finished(arg->conf)) + return ; sem_post(arg->forks); + if (philo_finished(arg->conf)) + return ; sem_post(arg->forks); + if (philo_finished(arg->conf)) + return ; usleep(arg->conf->timeout_sleep * 1000); } @@ -72,5 +86,7 @@ void event_die(t_philo *arg) return ; philo_put(arg->id, EVENT_DIE); arg->conf->all_alive = false; + if (philo_finished(arg->conf)) + return ; sem_post(arg->conf->sem_stdout); } diff --git a/philo_two/src/main.c b/philo_two/src/main.c index 46ec474..85994da 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2021/01/01 14:44:21 by charles ### ########.fr */ +/* Updated: 2021/01/01 15:57:40 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,6 @@ #define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout" #define PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME "semaphore_philo_two_meal_num" - static int st_destroy( sem_t *forks, t_philo *philos, @@ -39,25 +38,27 @@ static int st_destroy( return (1); } +static sem_t *st_sem_create(const char *name, unsigned int value) +{ + sem_unlink(name); + return (sem_open(name, O_CREAT | O_EXCL, 0700, value)); +} + static int st_setup( t_philo_conf *conf, t_philo **philos, sem_t **forks, pthread_t **threads) { - int i; + long int i; - sem_unlink(PHILO_SEM_NAME); - *forks = sem_open(PHILO_SEM_NAME, O_CREAT | O_EXCL, 0700, conf->philo_num); + *forks = st_sem_create(PHILO_SEM_NAME, conf->philo_num); if (*forks == SEM_FAILED) return (1); - sem_unlink(PHILO_SEM_STDOUT_NAME); - conf->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, O_CREAT | O_EXCL, 0700, 1); + conf->sem_stdout = st_sem_create(PHILO_SEM_STDOUT_NAME, 1); if (conf->sem_stdout == SEM_FAILED) return (1); - sem_unlink(PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME); - conf->sem_meal_num_finished_counter = sem_open( - PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME, O_CREAT | O_EXCL, 0700, 1); + conf->sem_meal_num_finished_counter = st_sem_create(PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME, 1); if (conf->sem_meal_num_finished_counter == SEM_FAILED) return (1); *threads = NULL; @@ -83,7 +84,7 @@ static int st_setup( int main(int argc, char **argv) { - int i; + long int i; t_philo_conf conf; t_philo *philos; sem_t *forks; @@ -91,12 +92,13 @@ int main(int argc, char **argv) if (!parse_args((t_philo_args *)&conf, argc, argv)) return (1); - if (conf.philo_num == 0) + if (conf.philo_num == 0 || conf.meal_num == 0) return (0); if (st_setup(&conf, &philos, &forks, &threads) != 0) return (1); while (!philo_finished(&conf)) ; + conf.all_alive = false; i = -1; while (++i < conf.philo_num) pthread_detach(threads[i]); |
