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 | |
| parent | a237321ee53f44793ebc8b9db26b743f092b5e40 (diff) | |
| download | philosophers-473cf5d7576744b679b7a504232b5ebd4b5d689f.tar.gz philosophers-473cf5d7576744b679b7a504232b5ebd4b5d689f.tar.bz2 philosophers-473cf5d7576744b679b7a504232b5ebd4b5d689f.zip | |
Norming
| -rw-r--r-- | common/args.c | 7 | ||||
| -rw-r--r-- | common/common.h | 6 | ||||
| -rw-r--r-- | common/io.c | 4 | ||||
| -rw-r--r-- | philo_one/src/event.c | 9 | ||||
| -rw-r--r-- | philo_one/src/philo.c | 9 | ||||
| -rw-r--r-- | philo_one/src/philo_one.h | 33 | ||||
| -rw-r--r-- | philo_three/Makefile | 2 | ||||
| -rw-r--r-- | philo_three/src/child.c | 9 | ||||
| -rw-r--r-- | philo_three/src/main.c | 26 | ||||
| -rw-r--r-- | philo_three/src/philo_three.h | 16 | ||||
| -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 |
13 files changed, 90 insertions, 82 deletions
diff --git a/common/args.c b/common/args.c index 6392f22..e55f004 100644 --- a/common/args.c +++ b/common/args.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ -/* Updated: 2020/09/30 10:14:24 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 14:26:40 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,10 @@ bool parse_args(t_philo_args *args, int argc, char **argv) { if (argc != 5 && argc != 6) - return h_err(false, "Usage: %s philosophers_num death_timeout eat_timeout sleep_timeout [meal_num]", argv[0]); + { + return (h_err(false, "Usage: %s philosophers_num death_timeout" + "eat_timeout sleep_timeout [meal_num]", argv[0])); + } if ((args->philo_num = h_atou_strict(argv[1])) == -1 || (args->timeout_death = h_atou_strict(argv[2])) == -1 || (args->timeout_eat = h_atou_strict(argv[3])) == -1 diff --git a/common/common.h b/common/common.h index f810b6f..2415e73 100644 --- a/common/common.h +++ b/common/common.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2020/09/30 10:36:03 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 14:30:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ typedef long int t_time; -typedef enum +typedef enum e_philo_event { EVENT_FORK, EVENT_EAT, @@ -34,7 +34,7 @@ typedef enum 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; diff --git a/common/io.c b/common/io.c index 7989575..355135a 100644 --- a/common/io.c +++ b/common/io.c @@ -6,7 +6,7 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ -/* Updated: 2020/09/30 10:32:41 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 14:27:13 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ static char *st_nbrcpy(char *dst, long long int num) dst = st_nbrcpy(dst, num / 10); dst[0] = num % 10 + '0'; dst[1] = '\0'; - return dst + 1; + return (dst + 1); } static void st_strcat(char *dst, char *str) diff --git a/philo_one/src/event.c b/philo_one/src/event.c index 8952212..5261f4c 100644 --- a/philo_one/src/event.c +++ b/philo_one/src/event.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* io.c :+: :+: :+: */ +/* event.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2020/09/30 09:43:31 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 14:27:39 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,7 +48,10 @@ void event_think(t_philo *arg) pthread_mutex_unlock(&arg->conf->mutex_stdout); } -void event_sleep(t_philo *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left) +void event_sleep( + t_philo *arg, + pthread_mutex_t *fork_right, + pthread_mutex_t *fork_left) { pthread_mutex_lock(&arg->conf->mutex_stdout); if (!arg->conf->all_alive) diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index 45a8a17..6ea1024 100644 --- a/philo_one/src/philo.c +++ b/philo_one/src/philo.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2020/09/30 09:48:12 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 14:29:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,8 +39,11 @@ bool philos_start(t_philo *philos, int num) i = -1; while (++i < num) { - if (pthread_create(&philos[i].thread, NULL, - (t_routine)routine_philo, (void*)(philos + i)) == -1) + if (pthread_create( + &philos[i].thread, + NULL, + (t_routine)routine_philo, + (void*)(philos + i)) == -1) return (false); } return (true); diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index cb41b77..ae7d040 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2020/09/30 09:50:02 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 14:28:24 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,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; @@ -53,33 +53,36 @@ typedef struct s_philo ** forks.c */ -pthread_mutex_t *forks_new(int num); -void forks_destroy(pthread_mutex_t *forks, int num); +pthread_mutex_t *forks_new(int num); +void forks_destroy(pthread_mutex_t *forks, int num); /* ** philo.c */ -t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks); -void philos_destroy(t_philo *philos, int num); -bool philos_start(t_philo *philos, int num); -void philos_detach(t_philo *philos, int num); +t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks); +void philos_destroy(t_philo *philos, int num); +bool philos_start(t_philo *philos, int num); +void philos_detach(t_philo *philos, int num); /* ** routine.c */ -void *routine_philo(t_philo *arg); -void *routine_death(t_philo *arg); +void *routine_philo(t_philo *arg); +void *routine_death(t_philo *arg); /* ** io.c */ -void event_take_fork(t_philo *arg, pthread_mutex_t *fork); -void event_eat(t_philo *arg); -void event_think(t_philo *arg); -void event_sleep(t_philo *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left); -void event_die(t_philo *arg); +void event_take_fork(t_philo *arg, pthread_mutex_t *fork); +void event_eat(t_philo *arg); +void event_think(t_philo *arg); +void event_sleep( + t_philo *arg, + pthread_mutex_t *fork_right, + pthread_mutex_t *fork_left); +void event_die(t_philo *arg); #endif diff --git a/philo_three/Makefile b/philo_three/Makefile index a9f4640..3687670 100644 --- a/philo_three/Makefile +++ b/philo_three/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/10/01 09:10:44 by cacharle ### ########.fr # +# Updated: 2020/10/05 10:31:22 by cacharle ### ########.fr # # # # **************************************************************************** # diff --git a/philo_three/src/child.c b/philo_three/src/child.c index 5bc0129..2e9b566 100644 --- a/philo_three/src/child.c +++ b/philo_three/src/child.c @@ -6,7 +6,7 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 14:36:16 by cacharle #+# #+# */ -/* Updated: 2020/10/01 09:11:29 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 15:02:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,11 +25,10 @@ void *routine_death(t_philo *philo) pid_t child_start(t_philo *philo) { - pid_t pid; pthread_t thread_death; + pid_t pid; - pid = fork(); - if (pid == -1) + if ((pid = fork()) == -1) return (-1); if (pid == 0) { @@ -50,5 +49,5 @@ pid_t child_start(t_philo *philo) } exit(0); } - return pid; + return (pid); } diff --git a/philo_three/src/main.c b/philo_three/src/main.c index a6c6370..04b1069 100644 --- a/philo_three/src/main.c +++ b/philo_three/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */ -/* Updated: 2020/10/01 09:34:55 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 14:31:39 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,27 +23,25 @@ sem_t *philo_sem_create(char *name, int value) return (sem); } -int main(int argc, char **argv) +int main(int argc, char **argv) { t_philo_args args; t_philo philo; sem_t *forks; sem_t *sem_stdout; sem_t *sem_dead; + pid_t *pids; + 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, 1)) == NULL) + 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); - - pid_t *pids; - pids = malloc(sizeof(pid_t) * args.philo_num); - - int i = -1; + if ((pids = malloc(sizeof(pid_t) * args.philo_num)) == NULL) + return (1); + i = -1; while (++i < args.philo_num) { philo.conf = &args; @@ -54,15 +52,11 @@ int main(int argc, char **argv) return (1); } } - - sem_wait(sem_dead); 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); diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h index c0afe78..e30a857 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/01 09:10:30 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 15:05:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ # define PHILO_SEM_STDOUT_NAME "semaphore_philo_three_stdout" # define PHILO_SEM_DEAD_NAME "semaphore_philo_three_dead" -typedef struct +typedef struct s_philo { t_philo_args *conf; int id; @@ -35,12 +35,12 @@ typedef struct sem_t *sem_dead; } t_philo; -pid_t child_start(t_philo *arg); +pid_t child_start(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); +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/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) |
