aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/helper.c10
-rw-r--r--common/io.c12
-rw-r--r--philo_one/src/main.c36
-rw-r--r--philo_one/src/philo.c12
-rw-r--r--philo_three/src/main.c94
-rw-r--r--philo_three/src/philo_three.h9
-rw-r--r--philo_two/Makefile4
-rw-r--r--philo_two/src/main.c76
-rw-r--r--philo_two/src/routine.c14
9 files changed, 177 insertions, 90 deletions
diff --git a/common/helper.c b/common/helper.c
index b2107ae..9f29ad9 100644
--- a/common/helper.c
+++ b/common/helper.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */
-/* Updated: 2020/09/30 10:15:14 by cacharle ### ########.fr */
+/* Updated: 2020/10/05 15:28:35 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -36,11 +36,15 @@ long int h_atou_strict(char *s)
return (num);
}
+/*
+** No need to check error of gettimeofday
+** (only for settimeofday and passed pointer validity)
+*/
+
t_time h_time_now(void)
{
struct timeval tv;
- if (gettimeofday(&tv, NULL) == -1)
- return (-1);
+ gettimeofday(&tv, NULL);
return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
diff --git a/common/io.c b/common/io.c
index 355135a..48bb8cc 100644
--- a/common/io.c
+++ b/common/io.c
@@ -6,13 +6,13 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */
-/* Updated: 2020/10/05 14:27:13 by cacharle ### ########.fr */
+/* Updated: 2020/10/05 15:29:09 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "common.h"
-size_t st_strlen(char *s)
+static size_t st_strlen(char *s)
{
int counter;
@@ -22,7 +22,7 @@ size_t st_strlen(char *s)
return (counter);
}
-static char *st_nbrcpy(char *dst, long long int num)
+static char *st_nbrcpy(char *dst, long long int num)
{
if (num > 9)
dst = st_nbrcpy(dst, num / 10);
@@ -31,7 +31,7 @@ static char *st_nbrcpy(char *dst, long long int num)
return (dst + 1);
}
-static void st_strcat(char *dst, char *str)
+static void st_strcat(char *dst, char *str)
{
while (*dst != '\0')
dst++;
@@ -40,7 +40,7 @@ static void st_strcat(char *dst, char *str)
*dst = '\0';
}
-void philo_put(size_t id, t_philo_event event)
+void philo_put(size_t id, t_philo_event event)
{
static char buf[2048];
@@ -61,7 +61,7 @@ void philo_put(size_t id, t_philo_event event)
write(STDOUT_FILENO, buf, st_strlen(buf));
}
-int h_err(int ret, const char *format, char *str)
+int h_err(int ret, const char *format, char *str)
{
while (*format != '\0')
{
diff --git a/philo_one/src/main.c b/philo_one/src/main.c
index dce44fc..eec5d4e 100644
--- a/philo_one/src/main.c
+++ b/philo_one/src/main.c
@@ -6,13 +6,35 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */
-/* Updated: 2020/09/30 09:49:48 by cacharle ### ########.fr */
+/* Updated: 2020/10/05 15:42:09 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
-int main(int argc, char **argv)
+static int st_setup(
+ t_philo_conf *conf,
+ t_philo **philos,
+ pthread_mutex_t **forks)
+{
+ if ((*forks = forks_new(conf->philo_num)) == NULL)
+ return (1);
+ if ((*philos = philos_new(conf, *forks)) == NULL)
+ {
+ forks_destroy(*forks, conf->philo_num);
+ return (1);
+ }
+ if (pthread_mutex_init(&conf->mutex_stdout, NULL) != 0)
+ {
+ forks_destroy(*forks, conf->philo_num);
+ free(*philos);
+ return (1);
+ }
+ conf->all_alive = true;
+ return (0);
+}
+
+int main(int argc, char **argv)
{
t_philo_conf conf;
t_philo *philos;
@@ -22,14 +44,14 @@ int main(int argc, char **argv)
return (1);
if (conf.philo_num == 0)
return (0);
- if ((forks = forks_new(conf.philo_num)) == NULL)
- return (1);
- if ((philos = philos_new(&conf, forks)) == NULL)
+ if (st_setup(&conf, &philos, &forks) != 0)
return (1);
- conf.all_alive = true;
- pthread_mutex_init(&conf.mutex_stdout, NULL);
if (!philos_start(philos, conf.philo_num))
+ {
+ forks_destroy(forks, conf.philo_num);
+ free(philos);
return (1);
+ }
while (conf.all_alive)
;
philos_detach(philos, conf.philo_num);
diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c
index 6ea1024..30c0a7c 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/10/05 14:29:10 by cacharle ### ########.fr */
+/* Updated: 2020/10/05 16:13:10 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,8 +43,12 @@ bool philos_start(t_philo *philos, int num)
&philos[i].thread,
NULL,
(t_routine)routine_philo,
- (void*)(philos + i)) == -1)
+ (void*)(philos + i)) != 0)
+ {
+ while (--i >= 0)
+ pthread_detach(philos[i].thread);
return (false);
+ }
}
return (true);
}
@@ -53,9 +57,9 @@ void philos_detach(t_philo *philos, int num)
{
int i;
+ if (philos == NULL)
+ return ;
i = -1;
while (++i < num)
- {
pthread_detach(philos[i].thread);
- }
}
diff --git a/philo_three/src/main.c b/philo_three/src/main.c
index 04b1069..d6749d8 100644
--- a/philo_three/src/main.c
+++ b/philo_three/src/main.c
@@ -6,62 +6,84 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */
-/* Updated: 2020/10/05 14:31:39 by cacharle ### ########.fr */
+/* Updated: 2020/10/05 16:59:54 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_three.h"
-sem_t *philo_sem_create(char *name, int value)
+static sem_t *st_sem_create(char *name, int value)
{
sem_t *sem;
sem_unlink(name);
sem = sem_open(name, O_CREAT | O_EXCL, 0700, value);
if (sem == SEM_FAILED)
- return (NULL);
+ return (SEM_FAILED);
return (sem);
}
-int main(int argc, char **argv)
+static int st_destroy(t_sems *sems, pid_t *pids, int philo_num)
{
- t_philo_args args;
- t_philo philo;
- sem_t *forks;
- sem_t *sem_stdout;
- sem_t *sem_dead;
- pid_t *pids;
- int i;
+ 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, 0)) == NULL)
- return (1);
- if ((pids = malloc(sizeof(pid_t) * args.philo_num)) == NULL)
- return (1);
- i = -1;
- while (++i < args.philo_num)
+ if (pids != NULL)
{
- philo.conf = &args;
- philo.id = i + 1;
- if ((pids[i] = child_start(&philo)) == -1)
- {
- free(pids);
- return (1);
- }
+ i = -1;
+ while (++i < philo_num)
+ kill(pids[i], SIGKILL);
+ free(pids);
}
- 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);
+ sem_close(sems->forks);
+ sem_close(sems->sem_stdout);
+ sem_close(sems->sem_dead);
sem_unlink(PHILO_SEM_NAME);
sem_unlink(PHILO_SEM_STDOUT_NAME);
sem_unlink(PHILO_SEM_DEAD_NAME);
+ return (1);
+}
+
+static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids)
+{
+ t_philo philo;
+ int i;
+
+ sems->sem_stdout = SEM_FAILED;
+ sems->sem_dead = SEM_FAILED;
+ *pids = NULL;
+ if ((sems->forks =
+ st_sem_create(PHILO_SEM_NAME, args->philo_num)) == SEM_FAILED
+ || (sems->sem_stdout =
+ st_sem_create(PHILO_SEM_STDOUT_NAME, 1)) == SEM_FAILED
+ || (sems->sem_dead =
+ st_sem_create(PHILO_SEM_DEAD_NAME, 1)) == SEM_FAILED
+ || (*pids = malloc(sizeof(pid_t) * args->philo_num)) == NULL)
+ return (st_destroy(sems, *pids, 0));
+ i = -1;
+ while (++i < args->philo_num)
+ {
+ philo.conf = args;
+ philo.id = i + 1;
+ if (((*pids)[i] = child_start(&philo)) == -1)
+ return (st_destroy(sems, *pids, i));
+ }
+ return (0);
+}
+
+int main(int argc, char **argv)
+{
+ t_philo_args args;
+ t_sems sems;
+ pid_t *pids;
+
+ if (!parse_args(&args, argc, argv))
+ return (1);
+ if (args.philo_num == 0)
+ return (0);
+ if (st_setup(&args, &sems, &pids) != 0)
+ return (1);
+ sem_wait(sems.sem_dead);
+ sem_wait(sems.sem_dead);
+ st_destroy(&sems, pids, args.philo_num);
return (0);
}
diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h
index e30a857..1c1b8af 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/05 15:05:26 by cacharle ### ########.fr */
+/* Updated: 2020/10/05 16:54:29 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,6 +35,13 @@ typedef struct s_philo
sem_t *sem_dead;
} t_philo;
+typedef struct s_sems
+{
+ sem_t *forks;
+ sem_t *sem_stdout;
+ sem_t *sem_dead;
+} t_sems;
+
pid_t child_start(t_philo *arg);
void event_take_fork(t_philo *arg);
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);
}