diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-05 15:06:03 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-05 15:06:03 +0200 |
| commit | 473cf5d7576744b679b7a504232b5ebd4b5d689f (patch) | |
| tree | 6518e4fd26bf4584e5a2d72b9e1289473bce76da /philo_two/src | |
| parent | a237321ee53f44793ebc8b9db26b743f092b5e40 (diff) | |
| download | philosophers-473cf5d7576744b679b7a504232b5ebd4b5d689f.tar.gz philosophers-473cf5d7576744b679b7a504232b5ebd4b5d689f.tar.bz2 philosophers-473cf5d7576744b679b7a504232b5ebd4b5d689f.zip | |
Norming
Diffstat (limited to 'philo_two/src')
| -rw-r--r-- | philo_two/src/main.c | 19 | ||||
| -rw-r--r-- | philo_two/src/philo_two.h | 20 | ||||
| -rw-r--r-- | philo_two/src/routine.c | 12 |
3 files changed, 27 insertions, 24 deletions
diff --git a/philo_two/src/main.c b/philo_two/src/main.c index c0c9fd6..f53f892 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: 2020/09/30 09:58:01 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 14:31:09 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ #define PHILO_SEM_NAME "semaphore_philo_two" -int main(int argc, char **argv) +int main(int argc, char **argv) { int i; t_philo_conf conf; @@ -22,10 +22,12 @@ int main(int argc, char **argv) sem_t *forks; pthread_t *threads; - if (!parse_args((t_philo_args*)&conf, argc, argv)) + if (!parse_args((t_philo_args *)&conf, argc, argv)) return (1); + if (conf.philo_num == 0) + return (0); sem_unlink(PHILO_SEM_NAME); - forks = sem_open(PHILO_SEM_NAME, O_CREAT | O_EXCL, 0600, conf.philo_num); + forks = sem_open(PHILO_SEM_NAME, O_CREAT | O_EXCL, 0700, conf.philo_num); if (forks == SEM_FAILED) return (1); if ((philos = routine_create_philos(&conf, forks)) == NULL) @@ -34,20 +36,21 @@ int main(int argc, char **argv) return (1); conf.all_alive = true; pthread_mutex_init(&conf.mutex_stdout, NULL); - i = -1; while (++i < conf.philo_num) - if (pthread_create(threads + i, NULL, (t_routine)routine_philo, philos + i) < 0) + if (pthread_create(threads + i, NULL, + (t_routine)routine_philo, philos + i) < 0) return (1); while (conf.all_alive) ; - i = -1; while (++i < conf.philo_num) pthread_detach(threads[i]); + i = -1; + while (++i < conf.philo_num) + sem_post(forks); sem_close(forks); sem_unlink(PHILO_SEM_NAME); - pthread_mutex_destroy(&conf.mutex_stdout); free(threads); free(philos); diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h index a697382..b3f08ed 100644 --- a/philo_two/src/philo_two.h +++ b/philo_two/src/philo_two.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:47:23 by cacharle #+# #+# */ -/* Updated: 2020/09/30 10:02:50 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 14:29:40 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ typedef struct { long int philo_num; - t_time timeout_death; + t_time timeout_death; t_time timeout_eat; t_time timeout_sleep; long int meal_num; @@ -43,18 +43,18 @@ typedef struct ** routine.c */ -void *routine_philo(t_philo *arg); -void *routine_death(t_philo *arg); -t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks); +void *routine_philo(t_philo *arg); +void *routine_death(t_philo *arg); +t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks); /* ** io.c */ -void event_take_fork(t_philo *arg); -void event_eat(t_philo *arg); -void event_think(t_philo *arg); -void event_sleep(t_philo *arg); -void event_die(t_philo *arg); +void event_take_fork(t_philo *arg); +void event_eat(t_philo *arg); +void event_think(t_philo *arg); +void event_sleep(t_philo *arg); +void event_die(t_philo *arg); #endif diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index 6554c89..8fe13a1 100644 --- a/philo_two/src/routine.c +++ b/philo_two/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */ -/* Updated: 2020/09/30 09:57:41 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 14:30:52 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ void *routine_philo(t_philo *arg) { - pthread_t thread_death; + pthread_t thread_death; event_think(arg); if (!arg->conf->all_alive) @@ -37,11 +37,11 @@ void *routine_philo(t_philo *arg) void *routine_death(t_philo *arg) { - t_time current; + t_time current; current = h_time_now(); - while (arg->conf->all_alive && - current - arg->time_last_eat < arg->conf->timeout_death) + while (arg->conf->all_alive + && current - arg->time_last_eat < arg->conf->timeout_death) current = h_time_now(); event_die(arg); return (NULL); @@ -49,7 +49,7 @@ void *routine_death(t_philo *arg) t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks) { - int i; + int i; t_philo *routine_conf; if ((routine_conf = malloc(sizeof(t_philo) * conf->philo_num)) == NULL) |
