aboutsummaryrefslogtreecommitdiff
path: root/philo_two/src/main.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-29 16:16:20 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-29 16:16:20 +0200
commitb6f4db572d7d40c178ec286373422faa2172f135 (patch)
tree03fd9601de99de8111994f487cfdacc7127ce6d0 /philo_two/src/main.c
parent6bfcd3c6f921e7717e61167b291bb27bd3f66386 (diff)
downloadphilosophers-b6f4db572d7d40c178ec286373422faa2172f135.tar.gz
philosophers-b6f4db572d7d40c178ec286373422faa2172f135.tar.bz2
philosophers-b6f4db572d7d40c178ec286373422faa2172f135.zip
philo_two file restructuration
Diffstat (limited to 'philo_two/src/main.c')
-rw-r--r--philo_two/src/main.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
new file mode 100644
index 0000000..5442d1a
--- /dev/null
+++ b/philo_two/src/main.c
@@ -0,0 +1,55 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* main.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */
+/* Updated: 2020/09/29 15:27:05 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "philo_two.h"
+
+int main(int argc, char **argv)
+{
+ int i;
+ t_philo_args philo_args;
+ t_routine_arg *routine_args;
+ sem_t *forks;
+ pthread_t *threads;
+
+ if (!parse_args(&philo_args, argc, argv))
+ return (1);
+ sem_unlink("philo_two");
+ forks = sem_open("philo_two", O_CREAT, 0700, philo_args.philo_num);
+ if (forks == SEM_FAILED)
+ return (1);
+ if ((routine_args = routine_args_create(&philo_args, forks)) == NULL)
+ return (1);
+ if ((threads = malloc(sizeof(pthread_t) * philo_args.philo_num)) == NULL)
+ return (1);
+ philo_args.all_alive = TRUE;
+ pthread_mutex_init(&philo_args.mutex_all_alive, NULL);
+ pthread_mutex_init(&philo_args.mutex_stdout, NULL);
+
+ i = -1;
+ while (++i < philo_args.philo_num)
+ if (pthread_create(threads + i, NULL, routine_philo, routine_args + i) < 0)
+ return (1);
+ while (philo_args.all_alive)
+ ;
+
+ i = -1;
+ while (++i < philo_args.philo_num)
+ pthread_join(threads[i], NULL);
+ sem_close(forks);
+ sem_unlink("philo_two");
+
+ pthread_mutex_destroy(&philo_args.mutex_stdout);
+ pthread_mutex_destroy(&philo_args.mutex_all_alive);
+ free(threads);
+ free(routine_args);
+ return (0);
+}