aboutsummaryrefslogtreecommitdiff
path: root/philo_three/src
diff options
context:
space:
mode:
Diffstat (limited to 'philo_three/src')
-rw-r--r--philo_three/src/child.c9
-rw-r--r--philo_three/src/event.c4
-rw-r--r--philo_three/src/main.c14
-rw-r--r--philo_three/src/philo_three.h5
4 files changed, 25 insertions, 7 deletions
diff --git a/philo_three/src/child.c b/philo_three/src/child.c
index e9c743b..6060cf7 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/03 13:47:27 by cacharle ### ########.fr */
+/* Updated: 2021/01/04 11:03:58 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -34,9 +34,8 @@ void st_child_loop(t_philo *philo)
while (true)
{
event_take_fork(philo);
- event_take_fork(philo);
- event_eat(philo);
philo->time_last_eat = h_time_now();
+ event_eat(philo);
eat_counter++;
if (philo->conf->meal_num != -1 && eat_counter == philo->conf->meal_num)
{
@@ -61,9 +60,13 @@ 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_start = sem_open(PHILO_SEM_START_NAME, 0);
philo->time_last_eat = h_time_now();
pthread_create(&thread_death, NULL, (t_routine)routine_death, philo);
event_think(philo);
+ sem_wait(philo->sem_start);
+ if (philo->id % 2 == 0)
+ usleep(500);
st_child_loop(philo);
exit(0);
}
diff --git a/philo_three/src/event.c b/philo_three/src/event.c
index 185147f..665f260 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/03 16:56:51 by cacharle ### ########.fr */
+/* Updated: 2021/01/04 10:57:35 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,8 +15,10 @@
void event_take_fork(t_philo *philo)
{
sem_wait(philo->forks);
+ sem_wait(philo->forks);
sem_wait(philo->sem_stdout);
philo_put(philo->id, EVENT_FORK, philo->initial_time);
+ philo_put(philo->id, EVENT_FORK, philo->initial_time);
sem_post(philo->sem_stdout);
}
diff --git a/philo_three/src/main.c b/philo_three/src/main.c
index 8a5dec4..105903d 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/03 14:01:58 by cacharle ### ########.fr */
+/* Updated: 2021/01/04 11:06:41 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,6 +35,8 @@ static int st_destroy(t_sems *sems, pid_t *pids, int philo_num)
sem_unlink(PHILO_SEM_STDOUT_NAME);
sem_close(sems->sem_finish);
sem_unlink(PHILO_SEM_FINISH_NAME);
+ sem_close(sems->sem_start);
+ sem_unlink(PHILO_SEM_START_NAME);
return (1);
}
@@ -46,6 +48,7 @@ static int st_setup(
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 =
@@ -53,18 +56,25 @@ static int st_setup(
|| (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
|| (*pids = malloc(sizeof(pid_t) * args->philo_num)) == NULL)
return (st_destroy(sems, *pids, 0));
i = -1;
while (++i < args->philo_num)
+ sem_wait(sems->sem_start);
+ i = -1;
+ while (++i < args->philo_num)
{
philo.conf = args;
philo.id = i + 1;
philo.initial_time = initial_time;
if (((*pids)[i] = child_start(&philo)) == -1)
return (st_destroy(sems, *pids, i));
- usleep(200);
}
+ i = -1;
+ while (++i < args->philo_num)
+ sem_post(sems->sem_start);
return (0);
}
diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h
index 3d6b138..fe3441b 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/03 13:51:54 by cacharle ### ########.fr */
+/* Updated: 2021/01/04 11:01:18 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,6 +26,7 @@
# 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"
typedef struct s_philo
{
@@ -36,6 +37,7 @@ typedef struct s_philo
sem_t *forks;
sem_t *sem_stdout;
sem_t *sem_finish;
+ sem_t *sem_start;
} t_philo;
typedef struct s_sems
@@ -43,6 +45,7 @@ typedef struct s_sems
sem_t *forks;
sem_t *sem_stdout;
sem_t *sem_finish;
+ sem_t *sem_start;
} t_sems;
pid_t child_start(t_philo *arg);