diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-05 17:00:51 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-05 17:00:51 +0200 |
| commit | 47e1a7b4af69e1998182126310e42af83cf214ed (patch) | |
| tree | aedb74e78e9fdbd7a6593ea773d47364bd158e61 /philo_two | |
| parent | 473cf5d7576744b679b7a504232b5ebd4b5d689f (diff) | |
| download | philosophers-47e1a7b4af69e1998182126310e42af83cf214ed.tar.gz philosophers-47e1a7b4af69e1998182126310e42af83cf214ed.tar.bz2 philosophers-47e1a7b4af69e1998182126310e42af83cf214ed.zip | |
Norming and destroy on error
Diffstat (limited to 'philo_two')
| -rw-r--r-- | philo_two/Makefile | 4 | ||||
| -rw-r--r-- | philo_two/src/main.c | 76 | ||||
| -rw-r--r-- | philo_two/src/routine.c | 14 |
3 files changed, 61 insertions, 33 deletions
diff --git a/philo_two/Makefile b/philo_two/Makefile index c4084a7..33ae4ea 100644 --- a/philo_two/Makefile +++ b/philo_two/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/09/30 10:37:38 by cacharle ### ########.fr # +# Updated: 2020/10/05 16:21:27 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -16,7 +16,7 @@ MAKE = make --no-print-directory COMMONDIR = ../common CC = gcc -CCFLAGS = -Wall -Wextra -I$(COMMONDIR) #-Werror +CCFLAGS = -g -Wall -Wextra -I$(COMMONDIR) #-Werror LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon NAME = philo_two diff --git a/philo_two/src/main.c b/philo_two/src/main.c index f53f892..b86dc51 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/10/05 14:31:09 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 16:23:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,55 @@ #define PHILO_SEM_NAME "semaphore_philo_two" -int main(int argc, char **argv) +static int st_destroy( + sem_t *forks, + t_philo *philos, + pthread_t *threads, + int philo_num) +{ + int i; + + i = -1; + while (++i < philo_num) + sem_post(forks); + sem_close(forks); + sem_unlink(PHILO_SEM_NAME); + free(philos); + free(threads); + return (1); +} + +static int st_setup( + t_philo_conf *conf, + t_philo **philos, + sem_t **forks, + pthread_t **threads) +{ + int i; + + sem_unlink(PHILO_SEM_NAME); + *forks = sem_open(PHILO_SEM_NAME, O_CREAT | O_EXCL, 0700, conf->philo_num); + if (*forks == SEM_FAILED) + return (1); + *threads = NULL; + if ((*philos = routine_create_philos(conf, *forks)) == NULL || + (*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL || + pthread_mutex_init(&conf->mutex_stdout, NULL) != 0) + return (st_destroy(*forks, *philos, *threads, conf->philo_num)); + conf->all_alive = true; + i = -1; + while (++i < conf->philo_num) + if (pthread_create(*threads + i, NULL, + (t_routine)routine_philo, *philos + i) != 0) + { + while (--i >= 0) + pthread_detach((*threads)[i]); + return (st_destroy(*forks, *philos, *threads, conf->philo_num)); + } + return (0); +} + +int main(int argc, char **argv) { int i; t_philo_conf conf; @@ -26,33 +74,13 @@ int main(int argc, char **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, 0700, conf.philo_num); - if (forks == SEM_FAILED) + if (st_setup(&conf, &philos, &forks, &threads) != 0) return (1); - if ((philos = routine_create_philos(&conf, forks)) == NULL) - return (1); - if ((threads = malloc(sizeof(pthread_t) * conf.philo_num)) == NULL) - 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) - 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); + st_destroy(forks, philos, threads, conf.philo_num); return (0); } diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index 8fe13a1..fd31ca2 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/10/05 14:30:52 by cacharle ### ########.fr */ +/* Updated: 2020/10/05 16:03:38 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,16 +50,16 @@ void *routine_death(t_philo *arg) t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks) { int i; - t_philo *routine_conf; + t_philo *philos; - if ((routine_conf = malloc(sizeof(t_philo) * conf->philo_num)) == NULL) + if ((philos = malloc(sizeof(t_philo) * conf->philo_num)) == NULL) return (NULL); i = -1; while (++i < conf->philo_num) { - routine_conf[i].id = i + 1; - routine_conf[i].forks = forks; - routine_conf[i].conf = conf; + philos[i].id = i + 1; + philos[i].forks = forks; + philos[i].conf = conf; } - return (routine_conf); + return (philos); } |
