aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-03 14:04:22 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-03 14:04:22 +0100
commite874ad94a31a8259b8eb7ad2865767c081bcd279 (patch)
treee629a6cef53b2c6cafcde9f8bbf5d1f7678753d6
parent592c515b742222c637478b3339b95cd2de8b4379 (diff)
downloadphilosophers-e874ad94a31a8259b8eb7ad2865767c081bcd279.tar.gz
philosophers-e874ad94a31a8259b8eb7ad2865767c081bcd279.tar.bz2
philosophers-e874ad94a31a8259b8eb7ad2865767c081bcd279.zip
Fixing philo_one/philo_two dying by sleeping more when checking the death, Fixing timestamp in the wrong order in philo_three
-rw-r--r--common/common.h5
-rw-r--r--common/io.c7
-rw-r--r--philo_one/src/event.c12
-rw-r--r--philo_one/src/main.c3
-rw-r--r--philo_one/src/philo_one.h3
-rw-r--r--philo_one/src/routine.c4
-rw-r--r--philo_three/src/child.c4
-rw-r--r--philo_three/src/event.c12
-rw-r--r--philo_three/src/main.c10
-rw-r--r--philo_three/src/philo_three.h3
-rw-r--r--philo_two/src/event.c12
-rw-r--r--philo_two/src/main.c3
-rw-r--r--philo_two/src/philo_two.h3
-rw-r--r--philo_two/src/routine.c4
14 files changed, 45 insertions, 40 deletions
diff --git a/common/common.h b/common/common.h
index 30a00a2..617db1a 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: 2021/01/02 11:01:23 by cacharle ### ########.fr */
+/* Updated: 2021/01/03 14:02:38 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -61,6 +61,7 @@ int h_err(int ret, const char *format, char *str);
*/
void philo_put_set_initial_time(void);
-void philo_put(size_t id, t_philo_event event);
+void philo_put(
+ size_t id, t_philo_event event, t_time initial_time);
#endif
diff --git a/common/io.c b/common/io.c
index cbaa69f..5d50ccc 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: 2021/01/02 12:08:16 by cacharle ### ########.fr */
+/* Updated: 2021/01/03 14:02:21 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -40,13 +40,10 @@ 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, t_time initial_time)
{
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() - initial_time);
st_strcat(buf, " ");
diff --git a/philo_one/src/event.c b/philo_one/src/event.c
index 2a0c302..4abfa67 100644
--- a/philo_one/src/event.c
+++ b/philo_one/src/event.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2021/01/02 10:49:08 by cacharle ### ########.fr */
+/* Updated: 2021/01/03 13:56:42 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,7 +20,7 @@ void event_take_fork(t_philo *arg, pthread_mutex_t *fork)
pthread_mutex_lock(&arg->conf->mutex_stdout);
if (philo_finished(arg->conf))
return ;
- philo_put(arg->id, EVENT_FORK);
+ philo_put(arg->id, EVENT_FORK, arg->conf->initial_time);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
}
@@ -31,7 +31,7 @@ void event_eat(t_philo *arg)
pthread_mutex_lock(&arg->conf->mutex_stdout);
if (philo_finished(arg->conf))
return ;
- philo_put(arg->id, EVENT_EAT);
+ philo_put(arg->id, EVENT_EAT, arg->conf->initial_time);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
usleep(arg->conf->timeout_eat * 1000);
}
@@ -43,7 +43,7 @@ void event_think(t_philo *arg)
pthread_mutex_lock(&arg->conf->mutex_stdout);
if (philo_finished(arg->conf))
return ;
- philo_put(arg->id, EVENT_THINK);
+ philo_put(arg->id, EVENT_THINK, arg->conf->initial_time);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
}
@@ -57,7 +57,7 @@ void event_sleep(
pthread_mutex_lock(&arg->conf->mutex_stdout);
if (philo_finished(arg->conf))
return ;
- philo_put(arg->id, EVENT_SLEEP);
+ philo_put(arg->id, EVENT_SLEEP, arg->conf->initial_time);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
pthread_mutex_unlock(fork_right);
pthread_mutex_unlock(fork_left);
@@ -72,6 +72,6 @@ void event_die(t_philo *arg)
if (philo_finished(arg->conf))
return ;
arg->conf->all_alive = false;
- philo_put(arg->id, EVENT_DIE);
+ philo_put(arg->id, EVENT_DIE, arg->conf->initial_time);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
}
diff --git a/philo_one/src/main.c b/philo_one/src/main.c
index 0c406b0..55e70d3 100644
--- a/philo_one/src/main.c
+++ b/philo_one/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */
-/* Updated: 2021/01/02 10:49:26 by cacharle ### ########.fr */
+/* Updated: 2021/01/03 13:57:04 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -54,6 +54,7 @@ int main(int argc, char **argv)
return (0);
if (st_setup(&conf, &philos, &forks) != 0)
return (1);
+ conf.initial_time = h_time_now();
if (!philos_start(philos, conf.philo_num))
{
forks_destroy(forks, conf.philo_num);
diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h
index b566f80..de5f2c4 100644
--- a/philo_one/src/philo_one.h
+++ b/philo_one/src/philo_one.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */
-/* Updated: 2021/01/01 13:51:29 by charles ### ########.fr */
+/* Updated: 2021/01/03 13:56:16 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,6 +37,7 @@ typedef struct
long int meal_num;
bool all_alive;
long int meal_num_finished_counter;
+ t_time initial_time;
pthread_mutex_t mutex_stdout;
pthread_mutex_t mutex_meal_num_finished_counter;
} t_philo_conf;
diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c
index 6ee37f1..70e6694 100644
--- a/philo_one/src/routine.c
+++ b/philo_one/src/routine.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */
-/* Updated: 2021/01/02 10:51:36 by cacharle ### ########.fr */
+/* Updated: 2021/01/03 12:23:35 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -66,7 +66,7 @@ void *routine_death(t_philo *arg)
current - arg->time_last_eat < arg->conf->timeout_death)
{
current = h_time_now();
- usleep(200);
+ usleep(1000);
}
event_die(arg);
return (NULL);
diff --git a/philo_three/src/child.c b/philo_three/src/child.c
index bce5d21..e9c743b 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/02 11:58:03 by cacharle ### ########.fr */
+/* Updated: 2021/01/03 13:47:27 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,7 +20,7 @@ void *routine_death(t_philo *philo)
while (current - philo->time_last_eat < philo->conf->timeout_death)
{
current = h_time_now();
- usleep(200);
+ usleep(500);
}
event_die(philo);
return (NULL);
diff --git a/philo_three/src/event.c b/philo_three/src/event.c
index 33b1e00..10f82c4 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/01 16:29:31 by charles ### ########.fr */
+/* Updated: 2021/01/03 13:51:20 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,14 +16,14 @@ void event_take_fork(t_philo *philo)
{
sem_wait(philo->forks);
sem_wait(philo->sem_stdout);
- philo_put(philo->id, EVENT_FORK);
+ philo_put(philo->id, EVENT_FORK, philo->initial_time);
sem_post(philo->sem_stdout);
}
void event_eat(t_philo *philo)
{
sem_wait(philo->sem_stdout);
- philo_put(philo->id, EVENT_EAT);
+ philo_put(philo->id, EVENT_EAT, philo->initial_time);
sem_post(philo->sem_stdout);
usleep(philo->conf->timeout_eat * 1000);
}
@@ -31,14 +31,14 @@ void event_eat(t_philo *philo)
void event_think(t_philo *philo)
{
sem_wait(philo->sem_stdout);
- philo_put(philo->id, EVENT_THINK);
+ philo_put(philo->id, EVENT_THINK, philo->initial_time);
sem_post(philo->sem_stdout);
}
void event_sleep(t_philo *philo)
{
sem_wait(philo->sem_stdout);
- philo_put(philo->id, EVENT_SLEEP);
+ philo_put(philo->id, EVENT_SLEEP, philo->initial_time);
sem_post(philo->sem_stdout);
sem_post(philo->forks);
sem_post(philo->forks);
@@ -50,7 +50,7 @@ void event_die(t_philo *philo)
long int i;
sem_wait(philo->sem_stdout);
- philo_put(philo->id, EVENT_DIE);
+ philo_put(philo->id, EVENT_DIE, philo->initial_time);
if (philo->conf->meal_num == -1)
sem_post(philo->sem_finish);
else
diff --git a/philo_three/src/main.c b/philo_three/src/main.c
index 9597fc0..8a5dec4 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/02 12:49:44 by cacharle ### ########.fr */
+/* Updated: 2021/01/03 14:01:58 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -38,14 +38,14 @@ static int st_destroy(t_sems *sems, pid_t *pids, int philo_num)
return (1);
}
-static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids)
+static int st_setup(
+ t_philo_args *args, t_sems *sems, pid_t **pids, t_time initial_time)
{
t_philo philo;
int i;
sems->sem_stdout = SEM_FAILED;
sems->sem_finish = SEM_FAILED;
- *pids = NULL;
if ((sems->forks =
st_sem_create(PHILO_SEM_NAME, args->philo_num)) == SEM_FAILED
|| (sems->sem_stdout =
@@ -60,6 +60,7 @@ static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids)
{
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);
@@ -97,7 +98,8 @@ int main(int argc, char **argv)
return (1);
if (args.philo_num == 0 || args.meal_num == 0)
return (0);
- if (st_setup(&args, &sems, &pids) != 0)
+ pids = NULL;
+ if (st_setup(&args, &sems, &pids, h_time_now()) != 0)
return (1);
st_wait(&args, &sems);
st_destroy(&sems, pids, args.philo_num);
diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h
index 380efc5..3d6b138 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/01 15:34:04 by charles ### ########.fr */
+/* Updated: 2021/01/03 13:51:54 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -32,6 +32,7 @@ typedef struct s_philo
t_philo_args *conf;
int id;
t_time time_last_eat;
+ t_time initial_time;
sem_t *forks;
sem_t *sem_stdout;
sem_t *sem_finish;
diff --git a/philo_two/src/event.c b/philo_two/src/event.c
index 0544042..eead2f5 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/02 11:25:08 by cacharle ### ########.fr */
+/* Updated: 2021/01/03 13:55:28 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,14 +16,14 @@ void event_take_fork(t_philo *arg)
{
sem_wait(arg->forks);
sem_wait(arg->conf->sem_stdout);
- philo_put(arg->id, EVENT_FORK);
+ philo_put(arg->id, EVENT_FORK, arg->conf->initial_time);
sem_post(arg->conf->sem_stdout);
}
void event_eat(t_philo *arg)
{
sem_wait(arg->conf->sem_stdout);
- philo_put(arg->id, EVENT_EAT);
+ philo_put(arg->id, EVENT_EAT, arg->conf->initial_time);
sem_post(arg->conf->sem_stdout);
usleep(arg->conf->timeout_eat * 1000);
}
@@ -31,14 +31,14 @@ void event_eat(t_philo *arg)
void event_think(t_philo *arg)
{
sem_wait(arg->conf->sem_stdout);
- philo_put(arg->id, EVENT_THINK);
+ philo_put(arg->id, EVENT_THINK, arg->conf->initial_time);
sem_post(arg->conf->sem_stdout);
}
void event_sleep(t_philo *arg)
{
sem_wait(arg->conf->sem_stdout);
- philo_put(arg->id, EVENT_SLEEP);
+ philo_put(arg->id, EVENT_SLEEP, arg->conf->initial_time);
sem_post(arg->conf->sem_stdout);
sem_post(arg->forks);
sem_post(arg->forks);
@@ -50,7 +50,7 @@ void event_die(t_philo *arg)
long int i;
sem_wait(arg->conf->sem_stdout);
- philo_put(arg->id, EVENT_DIE);
+ philo_put(arg->id, EVENT_DIE, arg->conf->initial_time);
if (arg->conf->meal_num == -1)
sem_post(arg->conf->sem_finish);
else
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
index e3994e2..0f823b5 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: 2021/01/02 12:49:06 by cacharle ### ########.fr */
+/* Updated: 2021/01/03 13:54:45 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -52,6 +52,7 @@ static int st_setup(
if ((*philos = routine_create_philos(conf, *forks)) == NULL ||
(*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL)
return (st_destroy(*philos, *threads));
+ conf->initial_time = h_time_now();
i = -1;
while (++i < conf->philo_num)
{
diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h
index f0c55ba..8261b0c 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/02 11:20:45 by cacharle ### ########.fr */
+/* Updated: 2021/01/03 13:55:04 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,6 +29,7 @@ typedef struct
t_time timeout_eat;
t_time timeout_sleep;
long int meal_num;
+ t_time initial_time;
sem_t *sem_stdout;
sem_t *sem_finish;
} t_philo_conf;
diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c
index 1066e97..3c04817 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/02 11:59:27 by cacharle ### ########.fr */
+/* Updated: 2021/01/03 12:57:27 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -49,7 +49,7 @@ void *routine_death(t_philo *arg)
while (current - arg->time_last_eat < arg->conf->timeout_death)
{
current = h_time_now();
- usleep(200);
+ usleep(1000);
}
event_die(arg);
return (NULL);