aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/common.h5
-rw-r--r--common/helper.c2
-rw-r--r--common/io.c9
-rw-r--r--philo_three/src/child.c44
-rw-r--r--philo_three/src/main.c44
-rw-r--r--philo_two/Makefile4
-rw-r--r--philo_two/src/event.c52
-rw-r--r--philo_two/src/main.c72
-rw-r--r--philo_two/src/philo_two.h7
-rw-r--r--philo_two/src/routine.c26
10 files changed, 120 insertions, 145 deletions
diff --git a/common/common.h b/common/common.h
index 2415e73..30a00a2 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/10/05 14:30:07 by cacharle ### ########.fr */
+/* Updated: 2021/01/02 11:01:23 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -54,12 +54,13 @@ bool parse_args(t_philo_args *args, int argc, char **argv);
long int h_atou_strict(char *s);
t_time h_time_now(void);
+int h_err(int ret, const char *format, char *str);
/*
** io.c
*/
+void philo_put_set_initial_time(void);
void philo_put(size_t id, t_philo_event event);
-int h_err(int ret, const char *format, char *str);
#endif
diff --git a/common/helper.c b/common/helper.c
index 9f29ad9..e1cc1e2 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/10/05 15:28:35 by cacharle ### ########.fr */
+/* Updated: 2021/01/02 12:07:49 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/common/io.c b/common/io.c
index 48bb8cc..cbaa69f 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/10/05 15:29:09 by cacharle ### ########.fr */
+/* Updated: 2021/01/02 12:08:16 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -42,10 +42,13 @@ static void st_strcat(char *dst, char *str)
void philo_put(size_t id, t_philo_event event)
{
- static char buf[2048];
+ static char buf[2048];
+ static t_time initial_time = -1;
+ if (initial_time == -1)
+ initial_time = h_time_now();
buf[0] = '\0';
- st_nbrcpy(buf, h_time_now());
+ st_nbrcpy(buf, h_time_now() - initial_time);
st_strcat(buf, " ");
st_nbrcpy(buf + st_strlen(buf), id);
if (event == EVENT_FORK)
diff --git a/philo_three/src/child.c b/philo_three/src/child.c
index e79f782..bce5d21 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: 2021/01/01 16:31:24 by charles ### ########.fr */
+/* Updated: 2021/01/02 11:58:03 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,39 +26,45 @@ void *routine_death(t_philo *philo)
return (NULL);
}
+void st_child_loop(t_philo *philo)
+{
+ long int eat_counter;
+
+ eat_counter = 0;
+ while (true)
+ {
+ event_take_fork(philo);
+ event_take_fork(philo);
+ event_eat(philo);
+ philo->time_last_eat = h_time_now();
+ eat_counter++;
+ if (philo->conf->meal_num != -1 && eat_counter == philo->conf->meal_num)
+ {
+ sem_wait(philo->sem_stdout);
+ sem_post(philo->sem_finish);
+ sem_post(philo->sem_stdout);
+ }
+ event_sleep(philo);
+ event_think(philo);
+ }
+}
+
pid_t child_start(t_philo *philo)
{
pthread_t thread_death;
pid_t pid;
- long int eat_counter;
if ((pid = fork()) == -1)
return (-1);
if (pid == 0)
{
- eat_counter = 0;
philo->forks = sem_open(PHILO_SEM_NAME, 0);
philo->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, 0);
philo->sem_finish = sem_open(PHILO_SEM_FINISH_NAME, 0);
philo->time_last_eat = h_time_now();
pthread_create(&thread_death, NULL, (t_routine)routine_death, philo);
event_think(philo);
- while (true)
- {
- event_take_fork(philo);
- event_take_fork(philo);
- event_eat(philo);
- philo->time_last_eat = h_time_now();
- eat_counter++;
- if (philo->conf->meal_num != -1 && eat_counter == philo->conf->meal_num)
- {
- sem_wait(philo->sem_stdout);
- sem_post(philo->sem_finish);
- sem_post(philo->sem_stdout);
- }
- event_sleep(philo);
- event_think(philo);
- }
+ st_child_loop(philo);
exit(0);
}
return (pid);
diff --git a/philo_three/src/main.c b/philo_three/src/main.c
index e5e8dd2..eb69ae8 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: 2021/01/01 16:30:33 by charles ### ########.fr */
+/* Updated: 2021/01/02 12:07:14 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -51,7 +51,8 @@ static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids)
|| (sems->sem_stdout =
st_sem_create(PHILO_SEM_STDOUT_NAME, 1)) == SEM_FAILED
|| (sems->sem_finish =
- st_sem_create(PHILO_SEM_FINISH_NAME, args->meal_num == -1 ? 1 : args->philo_num)) == SEM_FAILED
+ st_sem_create(PHILO_SEM_FINISH_NAME,
+ args->meal_num == -1 ? 1 : args->philo_num)) == SEM_FAILED
|| (*pids = malloc(sizeof(pid_t) * args->philo_num)) == NULL)
return (st_destroy(sems, *pids, 0));
i = -1;
@@ -66,12 +67,32 @@ static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids)
return (0);
}
+static void st_wait(t_philo_args *args, t_sems *sems)
+{
+ long int i;
+
+ if (args->meal_num == -1)
+ {
+ sem_wait(sems->sem_finish);
+ sem_wait(sems->sem_finish);
+ }
+ else
+ {
+ i = -1;
+ while (++i < args->philo_num)
+ sem_wait(sems->sem_finish);
+ i = -1;
+ while (++i < args->philo_num)
+ sem_wait(sems->sem_finish);
+ sem_wait(sems->sem_stdout);
+ }
+}
+
int main(int argc, char **argv)
{
t_philo_args args;
t_sems sems;
pid_t *pids;
- long int i;
if (!parse_args(&args, argc, argv))
return (1);
@@ -79,22 +100,7 @@ int main(int argc, char **argv)
return (0);
if (st_setup(&args, &sems, &pids) != 0)
return (1);
- if (args.meal_num == -1)
- {
- sem_wait(sems.sem_finish);
- sem_wait(sems.sem_finish);
- }
- else
- {
- /* printf("eat %d\n", args.meal_num); */
- i = -1;
- while (++i < args.philo_num)
- sem_wait(sems.sem_finish);
- i = -1;
- while (++i < args.philo_num)
- sem_wait(sems.sem_finish);
- sem_wait(sems.sem_stdout);
- }
+ st_wait(&args, &sems);
st_destroy(&sems, pids, args.philo_num);
return (0);
}
diff --git a/philo_two/Makefile b/philo_two/Makefile
index a4373d0..88c0f8b 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: 2021/01/01 15:18:02 by charles ### ########.fr #
+# Updated: 2021/01/02 11:27:33 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -16,7 +16,7 @@ MAKE = make --no-print-directory
COMMONDIR = ../common
CC = gcc
-CCFLAGS = -g -I$(COMMONDIR) -O2 -std=c99 -Wall -Wextra -Werror
+CCFLAGS = -g -I$(COMMONDIR) -O2 -std=c99 -Wall -Wextra #-Werror
LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon
NAME = philo_two
diff --git a/philo_two/src/event.c b/philo_two/src/event.c
index 0eaad11..0544042 100644
--- a/philo_two/src/event.c
+++ b/philo_two/src/event.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2021/01/01 15:57:43 by charles ### ########.fr */
+/* Updated: 2021/01/02 11:25:08 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,79 +14,49 @@
void event_take_fork(t_philo *arg)
{
- if (philo_finished(arg->conf))
- return ;
sem_wait(arg->forks);
- if (philo_finished(arg->conf))
- return ;
sem_wait(arg->conf->sem_stdout);
- if (philo_finished(arg->conf))
- return ;
philo_put(arg->id, EVENT_FORK);
- if (philo_finished(arg->conf))
- return ;
sem_post(arg->conf->sem_stdout);
}
void event_eat(t_philo *arg)
{
- if (philo_finished(arg->conf))
- return ;
sem_wait(arg->conf->sem_stdout);
- if (philo_finished(arg->conf))
- return ;
philo_put(arg->id, EVENT_EAT);
- if (philo_finished(arg->conf))
- return ;
sem_post(arg->conf->sem_stdout);
usleep(arg->conf->timeout_eat * 1000);
}
void event_think(t_philo *arg)
{
- if (philo_finished(arg->conf))
- return ;
sem_wait(arg->conf->sem_stdout);
- if (philo_finished(arg->conf))
- return ;
philo_put(arg->id, EVENT_THINK);
- if (philo_finished(arg->conf))
- return ;
sem_post(arg->conf->sem_stdout);
}
void event_sleep(t_philo *arg)
{
- if (philo_finished(arg->conf))
- return ;
sem_wait(arg->conf->sem_stdout);
- if (philo_finished(arg->conf))
- return ;
philo_put(arg->id, EVENT_SLEEP);
- if (philo_finished(arg->conf))
- return ;
sem_post(arg->conf->sem_stdout);
- if (philo_finished(arg->conf))
- return ;
sem_post(arg->forks);
- if (philo_finished(arg->conf))
- return ;
sem_post(arg->forks);
- if (philo_finished(arg->conf))
- return ;
usleep(arg->conf->timeout_sleep * 1000);
}
void event_die(t_philo *arg)
{
- if (philo_finished(arg->conf))
- return ;
+ long int i;
+
sem_wait(arg->conf->sem_stdout);
- if (philo_finished(arg->conf))
- return ;
philo_put(arg->id, EVENT_DIE);
- arg->conf->all_alive = false;
- if (philo_finished(arg->conf))
- return ;
- sem_post(arg->conf->sem_stdout);
+ if (arg->conf->meal_num == -1)
+ sem_post(arg->conf->sem_finish);
+ else
+ {
+ i = -1;
+ while (++i < arg->conf->philo_num)
+ sem_post(arg->conf->sem_finish);
+ }
}
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
index 85994da..e88ccac 100644
--- a/philo_two/src/main.c
+++ b/philo_two/src/main.c
@@ -6,42 +6,33 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */
-/* Updated: 2021/01/01 15:57:40 by charles ### ########.fr */
+/* Updated: 2021/01/02 12:05:01 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_two.h"
-#define PHILO_SEM_NAME "semaphore_philo_two"
-#define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout"
-#define PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME "semaphore_philo_two_meal_num"
+#define PHILO_SEM_NAME "semaphore_philo_two"
+#define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout"
+#define PHILO_SEM_FINISH_NAME "semaphore_philo_two_finish"
static int st_destroy(
- sem_t *forks,
t_philo *philos,
- pthread_t *threads,
- t_philo_conf *conf)
+ pthread_t *threads)
{
- /* int i; */
- /* */
- /* i = -1; */
- /* while (++i < conf->philo_num) */
- /* sem_post(forks); */
- sem_close(forks);
sem_unlink(PHILO_SEM_NAME);
- sem_close(conf->sem_stdout);
sem_unlink(PHILO_SEM_STDOUT_NAME);
- sem_close(conf->sem_meal_num_finished_counter);
- sem_unlink(PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME);
+ sem_unlink(PHILO_SEM_FINISH_NAME);
free(philos);
free(threads);
return (1);
}
-static sem_t *st_sem_create(const char *name, unsigned int value)
+static bool st_sem_create(const char *name, unsigned int value, sem_t **sem)
{
sem_unlink(name);
- return (sem_open(name, O_CREAT | O_EXCL, 0700, value));
+ return ((*sem = sem_open(name, O_CREAT | O_EXCL, 0700, value))
+ != SEM_FAILED);
}
static int st_setup(
@@ -52,21 +43,15 @@ static int st_setup(
{
long int i;
- *forks = st_sem_create(PHILO_SEM_NAME, conf->philo_num);
- if (*forks == SEM_FAILED)
- return (1);
- conf->sem_stdout = st_sem_create(PHILO_SEM_STDOUT_NAME, 1);
- if (conf->sem_stdout == SEM_FAILED)
- return (1);
- conf->sem_meal_num_finished_counter = st_sem_create(PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME, 1);
- if (conf->sem_meal_num_finished_counter == SEM_FAILED)
+ if (!st_sem_create(PHILO_SEM_NAME, conf->philo_num, forks) ||
+ !st_sem_create(PHILO_SEM_STDOUT_NAME, 1, &conf->sem_stdout) ||
+ !st_sem_create(PHILO_SEM_FINISH_NAME,
+ conf->meal_num == -1 ? 1 : conf->philo_num, &conf->sem_finish))
return (1);
*threads = NULL;
if ((*philos = routine_create_philos(conf, *forks)) == NULL ||
(*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL)
- return (st_destroy(*forks, *philos, *threads, conf));
- conf->all_alive = true;
- conf->meal_num_finished_counter = 0;
+ return (st_destroy(*philos, *threads));
i = -1;
while (++i < conf->philo_num)
{
@@ -75,13 +60,34 @@ static int st_setup(
{
while (--i >= 0)
pthread_detach((*threads)[i]);
- return (st_destroy(*forks, *philos, *threads, conf));
+ return (st_destroy(*philos, *threads));
}
usleep(200);
}
return (0);
}
+static void st_wait(t_philo_conf *conf)
+{
+ long int i;
+
+ if (conf->meal_num == -1)
+ {
+ sem_wait(conf->sem_finish);
+ sem_wait(conf->sem_finish);
+ }
+ else
+ {
+ i = -1;
+ while (++i < conf->philo_num)
+ sem_wait(conf->sem_finish);
+ i = -1;
+ while (++i < conf->philo_num)
+ sem_wait(conf->sem_finish);
+ sem_wait(conf->sem_stdout);
+ }
+}
+
int main(int argc, char **argv)
{
long int i;
@@ -96,12 +102,10 @@ int main(int argc, char **argv)
return (0);
if (st_setup(&conf, &philos, &forks, &threads) != 0)
return (1);
- while (!philo_finished(&conf))
- ;
- conf.all_alive = false;
+ st_wait(&conf);
i = -1;
while (++i < conf.philo_num)
pthread_detach(threads[i]);
- st_destroy(forks, philos, threads, &conf);
+ st_destroy(philos, threads);
return (0);
}
diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h
index ec5783b..f0c55ba 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: 2021/01/01 14:18:57 by charles ### ########.fr */
+/* Updated: 2021/01/02 11:20:45 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,10 +29,8 @@ typedef struct
t_time timeout_eat;
t_time timeout_sleep;
long int meal_num;
- bool all_alive;
- long int meal_num_finished_counter;
sem_t *sem_stdout;
- sem_t *sem_meal_num_finished_counter;
+ sem_t *sem_finish;
} t_philo_conf;
typedef struct
@@ -47,7 +45,6 @@ typedef struct
** routine.c
*/
-bool philo_finished(t_philo_conf *conf);
void *routine_philo(t_philo *arg);
void *routine_death(t_philo *arg);
t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks);
diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c
index 783011c..1066e97 100644
--- a/philo_two/src/routine.c
+++ b/philo_two/src/routine.c
@@ -6,44 +6,33 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */
-/* Updated: 2021/01/01 14:23:59 by charles ### ########.fr */
+/* Updated: 2021/01/02 11:59:27 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_two.h"
-inline bool philo_finished(t_philo_conf *conf)
-{
- return (!conf->all_alive ||
- (conf->meal_num != -1 &&
- conf->meal_num_finished_counter == conf->philo_num));
-}
-
void *routine_philo(t_philo *arg)
{
long int eat_counter;
pthread_t thread_death;
- if (philo_finished(arg->conf))
- return (NULL);
arg->time_last_eat = h_time_now();
if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0)
return (NULL);
eat_counter = 0;
event_think(arg);
- while (!philo_finished(arg->conf))
+ while (true)
{
event_take_fork(arg);
event_take_fork(arg);
event_eat(arg);
arg->time_last_eat = h_time_now();
- eat_counter++;
- if (!philo_finished(arg->conf) && arg->conf->meal_num != -1 &&
- eat_counter == arg->conf->meal_num)
+ if (arg->conf->meal_num != -1 && ++eat_counter == arg->conf->meal_num)
{
- sem_wait(arg->conf->sem_meal_num_finished_counter);
- arg->conf->meal_num_finished_counter++;
- sem_post(arg->conf->sem_meal_num_finished_counter);
+ sem_wait(arg->conf->sem_stdout);
+ sem_post(arg->conf->sem_finish);
+ sem_post(arg->conf->sem_stdout);
}
event_sleep(arg);
event_think(arg);
@@ -57,8 +46,7 @@ void *routine_death(t_philo *arg)
t_time current;
current = h_time_now();
- while (!philo_finished(arg->conf)
- && current - arg->time_last_eat < arg->conf->timeout_death)
+ while (current - arg->time_last_eat < arg->conf->timeout_death)
{
current = h_time_now();
usleep(200);