aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xclean_philo_three2
-rw-r--r--philo_three/src/child.c5
-rw-r--r--philo_three/src/event.c13
-rw-r--r--philo_three/src/main.c110
-rw-r--r--philo_three/src/philo_three.h35
-rw-r--r--philo_two/src/event.c19
-rw-r--r--philo_two/src/main.c57
-rw-r--r--philo_two/src/philo_two.h7
-rw-r--r--philo_two/src/routine.c7
9 files changed, 127 insertions, 128 deletions
diff --git a/clean_philo_three b/clean_philo_three
index 64e53bc..7e5c722 100755
--- a/clean_philo_three
+++ b/clean_philo_three
@@ -1,3 +1,3 @@
#!/bin/sh
-ps aux | grep philo_three | tr -s ' ' | cut -d ' ' -f 2 | xargs kill -9
+ps aux | grep philo_three | grep -v grep | tr -s ' ' | cut -d ' ' -f 2 | xargs -L1 kill -9
diff --git a/philo_three/src/child.c b/philo_three/src/child.c
index 144f7ba..641b82d 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/08 20:52:22 by charles ### ########.fr */
+/* Updated: 2021/01/09 15:59:00 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,7 +43,7 @@ void st_child_loop(t_philo *philo)
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_meal_num);
sem_post(philo->sem_stdout);
}
event_sleep(philo);
@@ -63,6 +63,7 @@ pid_t child_start(t_philo *philo)
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->sem_meal_num = sem_open(PHILO_SEM_MEAL_NUM_NAME, 0);
philo->sem_start = sem_open(PHILO_SEM_START_NAME, 0);
philo->sem_grab = sem_open(PHILO_SEM_GRAB_NAME, 0);
philo->time_last_eat = h_time_now();
diff --git a/philo_three/src/event.c b/philo_three/src/event.c
index 1c7fa14..a7b6d2a 100644
--- a/philo_three/src/event.c
+++ b/philo_three/src/event.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2021/01/08 20:25:44 by charles ### ########.fr */
+/* Updated: 2021/01/09 15:48:12 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -51,17 +51,8 @@ void event_sleep(t_philo *philo)
void event_die(t_philo *philo)
{
- long int i;
-
sem_wait(philo->sem_stdout);
philo_put(philo->id, EVENT_DIE, philo->initial_time);
philo_put_flush();
- if (philo->conf->meal_num == -1)
- sem_post(philo->sem_finish);
- else
- {
- i = -1;
- while (++i < philo->conf->philo_num)
- sem_post(philo->sem_finish);
- }
+ sem_post(philo->sem_finish);
}
diff --git a/philo_three/src/main.c b/philo_three/src/main.c
index 8790140..64b1004 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/08 20:23:28 by charles ### ########.fr */
+/* Updated: 2021/01/09 15:57:47 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,7 +18,7 @@ static sem_t *st_sem_create(char *name, unsigned int value)
return (sem_open(name, O_CREAT | O_EXCL, 0700, value));
}
-static int st_destroy(t_sems *sems, pid_t *pids, int philo_num)
+static int st_destroy(t_philo_conf *conf, pid_t *pids, int philo_num)
{
int i;
@@ -29,92 +29,98 @@ static int st_destroy(t_sems *sems, pid_t *pids, int philo_num)
kill(pids[i], SIGKILL);
free(pids);
}
- sem_close(sems->forks);
+ sem_close(conf->forks);
sem_unlink(PHILO_SEM_NAME);
- sem_close(sems->sem_stdout);
+ sem_close(conf->sem_stdout);
sem_unlink(PHILO_SEM_STDOUT_NAME);
- sem_close(sems->sem_finish);
+ sem_close(conf->sem_finish);
sem_unlink(PHILO_SEM_FINISH_NAME);
- sem_close(sems->sem_start);
+ sem_close(conf->sem_meal_num);
+ sem_unlink(PHILO_SEM_MEAL_NUM_NAME);
+ sem_close(conf->sem_start);
sem_unlink(PHILO_SEM_START_NAME);
- sem_close(sems->sem_grab);
+ sem_close(conf->sem_grab);
sem_unlink(PHILO_SEM_GRAB_NAME);
return (1);
}
+/* static bool st_sem_create(const char *name, unsigned int value, sem_t **sem) */
+/* { */
+/* sem_unlink(name); */
+/* return ((*sem = sem_open(name, O_CREAT | O_EXCL, 0700, value)) */
+/* != SEM_FAILED); */
+/* } */
+
static int st_setup(
- t_philo_args *args, t_sems *sems, pid_t **pids, t_time initial_time)
+ t_philo_conf *conf, pid_t **pids, t_time initial_time)
{
t_philo philo;
int i;
- sems->sem_stdout = SEM_FAILED;
- sems->sem_finish = SEM_FAILED;
- sems->sem_start = SEM_FAILED;
- 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_finish =
- st_sem_create(PHILO_SEM_FINISH_NAME,
- args->meal_num == -1 ? 1 : args->philo_num)) == SEM_FAILED
- || (sems->sem_start =
- st_sem_create(PHILO_SEM_START_NAME, args->philo_num)) == SEM_FAILED
- || (sems->sem_grab = st_sem_create(PHILO_SEM_GRAB_NAME, 1)) == SEM_FAILED
- || (*pids = malloc(sizeof(pid_t) * args->philo_num)) == NULL)
- return (st_destroy(sems, *pids, 0));
+ conf->sem_stdout = SEM_FAILED;
+ conf->sem_finish = SEM_FAILED;
+ conf->sem_start = SEM_FAILED;
+ if ((conf->forks = st_sem_create(PHILO_SEM_NAME, conf->philo_num)) == SEM_FAILED
+ || (conf->sem_stdout = st_sem_create(PHILO_SEM_STDOUT_NAME, 1)) == SEM_FAILED
+ || (conf->sem_finish = st_sem_create(PHILO_SEM_FINISH_NAME, 1)) == SEM_FAILED
+ || (conf->sem_meal_num = st_sem_create(PHILO_SEM_MEAL_NUM_NAME, conf->philo_num)) == SEM_FAILED
+ || (conf->sem_start = st_sem_create(PHILO_SEM_START_NAME, conf->philo_num)) == SEM_FAILED
+ || (conf->sem_grab = st_sem_create(PHILO_SEM_GRAB_NAME, 1)) == SEM_FAILED
+ || (*pids = malloc(sizeof(pid_t) * conf->philo_num)) == NULL)
+ return (st_destroy(conf, *pids, 0));
i = -1;
- while (++i < args->philo_num)
- sem_wait(sems->sem_start);
+ while (++i < conf->philo_num)
+ sem_wait(conf->sem_start);
i = -1;
- while (++i < args->philo_num)
+ while (++i < conf->philo_num)
{
- philo.conf = args;
+ philo.conf = conf;
philo.id = i + 1;
philo.initial_time = initial_time;
if (((*pids)[i] = child_start(&philo)) == -1)
- return (st_destroy(sems, *pids, i));
+ return (st_destroy(conf, *pids, i));
}
i = -1;
- while (++i < args->philo_num)
- sem_post(sems->sem_start);
+ while (++i < conf->philo_num)
+ sem_post(conf->sem_start);
return (0);
}
-static void st_wait(t_philo_args *args, t_sems *sems)
+static void *st_routine_meal_num(t_philo_conf *conf)
{
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);
- }
+ i = -1;
+ while (++i < conf->philo_num)
+ sem_wait(conf->sem_meal_num);
+ i = -1;
+ while (++i < conf->philo_num)
+ sem_wait(conf->sem_meal_num);
+ sem_wait(conf->sem_stdout);
+ sem_post(conf->sem_finish);
+ return (NULL);
}
int main(int argc, char **argv)
{
- t_philo_args args;
- t_sems sems;
+ t_philo_conf conf;
pid_t *pids;
- if (!parse_args(&args, argc, argv))
+ if (!parse_args((t_philo_args*)&conf, argc, argv))
return (1);
- if (args.philo_num == 0 || args.meal_num == 0)
+ if (conf.philo_num == 0 || conf.meal_num == 0)
return (0);
pids = NULL;
- if (st_setup(&args, &sems, &pids, h_time_now()) != 0)
+ if (st_setup(&conf, &pids, h_time_now()) != 0)
return (1);
- st_wait(&args, &sems);
- st_destroy(&sems, pids, args.philo_num);
+ if (conf.meal_num != -1)
+ {
+ pthread_t thread_meal_num;
+ pthread_create(&thread_meal_num, NULL, (t_routine)st_routine_meal_num, (void*)&conf);
+ pthread_detach(thread_meal_num);
+ }
+ sem_wait(conf.sem_finish);
+ sem_wait(conf.sem_finish);
+ st_destroy(&conf, pids, conf.philo_num);
return (0);
}
diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h
index f2b841d..18c7bcb 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: 2021/01/08 20:25:00 by charles ### ########.fr */
+/* Updated: 2021/01/09 15:47:32 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,33 +23,42 @@
# include "common.h"
-# define PHILO_SEM_NAME "semaphore_philo_three"
-# define PHILO_SEM_STDOUT_NAME "semaphore_philo_three_stdout"
-# define PHILO_SEM_FINISH_NAME "semaphore_philo_three_finish"
-# define PHILO_SEM_START_NAME "semaphore_philo_three_start"
-# define PHILO_SEM_GRAB_NAME "semaphore_philo_three_grab"
+# define PHILO_SEM_NAME "semaphore_philo_three"
+# define PHILO_SEM_STDOUT_NAME "semaphore_philo_three_stdout"
+# define PHILO_SEM_FINISH_NAME "semaphore_philo_three_finish"
+# define PHILO_SEM_MEAL_NUM_NAME "semaphore_philo_three_meal_num"
+# define PHILO_SEM_START_NAME "semaphore_philo_three_start"
+# define PHILO_SEM_GRAB_NAME "semaphore_philo_three_grab"
-typedef struct s_philo
+typedef struct s_philo_conf
{
- t_philo_args *conf;
- int id;
- t_time time_last_eat;
+ long int philo_num;
+ t_time timeout_death;
+ t_time timeout_eat;
+ t_time timeout_sleep;
+ long int meal_num;
t_time initial_time;
sem_t *forks;
sem_t *sem_stdout;
sem_t *sem_finish;
+ sem_t *sem_meal_num;
sem_t *sem_start;
sem_t *sem_grab;
-} t_philo;
+} t_philo_conf;
-typedef struct s_sems
+typedef struct s_philo
{
+ t_philo_conf *conf;
+ int id;
+ t_time time_last_eat;
+ t_time initial_time;
sem_t *forks;
sem_t *sem_stdout;
sem_t *sem_finish;
+ sem_t *sem_meal_num;
sem_t *sem_start;
sem_t *sem_grab;
-} t_sems;
+} t_philo;
pid_t child_start(t_philo *arg);
diff --git a/philo_two/src/event.c b/philo_two/src/event.c
index 3ce471e..2fc1249 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/08 19:54:40 by charles ### ########.fr */
+/* Updated: 2021/01/09 15:18:24 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,7 @@
void event_take_fork(t_philo *arg)
{
- sem_wait(arg->forks);
+ sem_wait(arg->conf->sem_forks);
sem_wait(arg->conf->sem_stdout);
philo_put(arg->id, EVENT_FORK, arg->conf->initial_time);
sem_post(arg->conf->sem_stdout);
@@ -40,23 +40,14 @@ void event_sleep(t_philo *arg)
sem_wait(arg->conf->sem_stdout);
philo_put(arg->id, EVENT_SLEEP, arg->conf->initial_time);
sem_post(arg->conf->sem_stdout);
- sem_post(arg->forks);
- sem_post(arg->forks);
+ sem_post(arg->conf->sem_forks);
+ sem_post(arg->conf->sem_forks);
h_sleep(arg->conf->timeout_sleep);
}
void event_die(t_philo *arg)
{
- long int i;
-
sem_wait(arg->conf->sem_stdout);
philo_put(arg->id, EVENT_DIE, arg->conf->initial_time);
- 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);
- }
+ sem_post(arg->conf->sem_finish);
}
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
index 034518c..fb441dc 100644
--- a/philo_two/src/main.c
+++ b/philo_two/src/main.c
@@ -6,17 +6,18 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */
-/* Updated: 2021/01/08 20:16:20 by charles ### ########.fr */
+/* Updated: 2021/01/09 15:35:47 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_two.h"
-#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"
-#define PHILO_SEM_START_NAME "semaphore_philo_two_start"
-#define PHILO_SEM_GRAB_NAME "semaphore_philo_two_grab"
+#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"
+#define PHILO_SEM_START_NAME "semaphore_philo_two_start"
+#define PHILO_SEM_GRAB_NAME "semaphore_philo_two_grab"
+#define PHILO_SEM_MEAL_NUM_NAME "semaphore_philo_two_meal_num"
static int st_destroy(
t_philo *philos,
@@ -25,6 +26,7 @@ static int st_destroy(
sem_unlink(PHILO_SEM_NAME);
sem_unlink(PHILO_SEM_STDOUT_NAME);
sem_unlink(PHILO_SEM_FINISH_NAME);
+ sem_unlink(PHILO_SEM_MEAL_NUM_NAME);
sem_unlink(PHILO_SEM_START_NAME);
sem_unlink(PHILO_SEM_GRAB_NAME);
free(philos);
@@ -53,20 +55,19 @@ void *routine_flush(t_philo_conf *conf)
static int st_setup(
t_philo_conf *conf,
t_philo **philos,
- sem_t **forks,
pthread_t **threads)
{
long int i;
- if (!st_sem_create(PHILO_SEM_NAME, conf->philo_num, forks) ||
+ if (!st_sem_create(PHILO_SEM_NAME, conf->philo_num, &conf->sem_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) ||
+ !st_sem_create(PHILO_SEM_FINISH_NAME, 1, &conf->sem_finish) ||
+ !st_sem_create(PHILO_SEM_MEAL_NUM_NAME, conf->philo_num, &conf->sem_meal_num) ||
!st_sem_create(PHILO_SEM_START_NAME, conf->philo_num, &conf->sem_start) ||
!st_sem_create(PHILO_SEM_GRAB_NAME, 1, &conf->sem_grab))
return (1);
*threads = NULL;
- if ((*philos = routine_create_philos(conf, *forks)) == NULL ||
+ if ((*philos = routine_create_philos(conf)) == NULL ||
(*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL)
return (st_destroy(*philos, *threads));
i = -1;
@@ -90,24 +91,21 @@ static int st_setup(
return (0);
}
-static void st_wait(t_philo_conf *conf)
+static void *st_routine_meal_num(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);
- }
+ return (NULL);
+ i = -1;
+ while (++i < conf->philo_num)
+ sem_wait(conf->sem_meal_num);
+ i = -1;
+ while (++i < conf->philo_num)
+ sem_wait(conf->sem_meal_num);
+ sem_wait(conf->sem_stdout);
+ sem_post(conf->sem_finish);
+ return (NULL);
}
int main(int argc, char **argv)
@@ -115,19 +113,22 @@ int main(int argc, char **argv)
long int i;
t_philo_conf conf;
t_philo *philos;
- sem_t *forks;
pthread_t *threads;
if (!parse_args((t_philo_args *)&conf, argc, argv))
return (1);
if (conf.philo_num == 0 || conf.meal_num == 0)
return (0);
- if (st_setup(&conf, &philos, &forks, &threads) != 0)
+ if (st_setup(&conf, &philos, &threads) != 0)
return (1);
pthread_t thread_flush;
pthread_create(&thread_flush, NULL, (t_routine)routine_flush, (void*)&conf);
pthread_detach(thread_flush);
- st_wait(&conf);
+ pthread_t thread_meal_num;
+ pthread_create(&thread_meal_num, NULL, (t_routine)st_routine_meal_num, (void*)&conf);
+ pthread_detach(thread_meal_num);
+ sem_wait(conf.sem_finish);
+ sem_wait(conf.sem_finish);
philo_put_flush();
i = -1;
while (++i < conf.philo_num)
diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h
index 91cdbef..b2b723e 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/08 20:10:36 by charles ### ########.fr */
+/* Updated: 2021/01/09 15:17:40 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,8 +30,10 @@ typedef struct
t_time timeout_sleep;
long int meal_num;
t_time initial_time;
+ sem_t *sem_forks;
sem_t *sem_stdout;
sem_t *sem_finish;
+ sem_t *sem_meal_num;
sem_t *sem_start;
sem_t *sem_grab;
} t_philo_conf;
@@ -41,7 +43,6 @@ typedef struct
long int id;
t_philo_conf *conf;
t_time time_last_eat;
- sem_t *forks;
} t_philo;
/*
@@ -50,7 +51,7 @@ typedef struct
void *routine_philo(t_philo *arg);
void *routine_death(t_philo *arg);
-t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks);
+t_philo *routine_create_philos(t_philo_conf *conf);
/*
** io.c
diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c
index 52bf53d..c491a0a 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: 2021/01/08 20:18:00 by charles ### ########.fr */
+/* Updated: 2021/01/09 15:17:32 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -33,7 +33,7 @@ void *routine_philo(t_philo *arg)
if (arg->conf->meal_num != -1 && ++eat_counter == arg->conf->meal_num)
{
sem_wait(arg->conf->sem_stdout);
- sem_post(arg->conf->sem_finish);
+ sem_post(arg->conf->sem_meal_num);
sem_post(arg->conf->sem_stdout);
}
event_sleep(arg);
@@ -57,7 +57,7 @@ void *routine_death(t_philo *arg)
return (NULL);
}
-t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks)
+t_philo *routine_create_philos(t_philo_conf *conf)
{
int i;
t_philo *philos;
@@ -68,7 +68,6 @@ t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks)
while (++i < conf->philo_num)
{
philos[i].id = i + 1;
- philos[i].forks = forks;
philos[i].conf = conf;
}
return (philos);