aboutsummaryrefslogtreecommitdiff
path: root/philo_one/src/philo.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-29 14:56:27 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-29 14:56:27 +0200
commit6bfcd3c6f921e7717e61167b291bb27bd3f66386 (patch)
tree67b2aa4b8a036cb355f90b1f94438b7eb46441f1 /philo_one/src/philo.c
parentac4278405b7a258010219499cccc0dd978201caf (diff)
downloadphilosophers-6bfcd3c6f921e7717e61167b291bb27bd3f66386.tar.gz
philosophers-6bfcd3c6f921e7717e61167b291bb27bd3f66386.tar.bz2
philosophers-6bfcd3c6f921e7717e61167b291bb27bd3f66386.zip
Fixing taking none existing fork in logs
Diffstat (limited to 'philo_one/src/philo.c')
-rw-r--r--philo_one/src/philo.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c
new file mode 100644
index 0000000..e0ecfec
--- /dev/null
+++ b/philo_one/src/philo.c
@@ -0,0 +1,61 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* philo.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */
+/* Updated: 2020/09/29 13:29:49 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "philo_one.h"
+
+t_philo *philos_new(int num)
+{
+ int i;
+ t_philo *philos;
+
+ if (num < 0)
+ return (NULL);
+ if ((philos = malloc(num * sizeof(t_philo))) == NULL)
+ return (NULL);
+ i = -1;
+ while (++i < num)
+ {
+ philos[i].id = i + 1;
+ philos[i].alive = false;
+ }
+ return (philos);
+}
+
+bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num)
+{
+ int i;
+
+ i = -1;
+ while (++i < num)
+ {
+ philos[i].alive = true;
+ if (pthread_create(&philos[i].thread, NULL,
+ (void *(*)(void*))routine_philo, (void*)(routine_args + i)) == -1)
+ return (false);
+ }
+ return (true);
+}
+
+void philos_join(t_philo *philos, int num)
+{
+ int i;
+
+ i = -1;
+ while (++i < num)
+ {
+ if (philos[i].alive)
+ {
+ philos[i].alive = false;
+ pthread_detach(philos[i].thread);
+ }
+ }
+}