aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/inc/common.h5
-rw-r--r--common/src/helper.c30
-rw-r--r--common/src/io.c33
-rw-r--r--philo_one/src/event.c5
-rw-r--r--philo_one/src/main.c2
-rw-r--r--philo_one/src/philo.c3
-rw-r--r--philo_one/src/philo_one.h3
-rw-r--r--philo_one/src/routine.c13
-rw-r--r--philo_three/src/child.c23
-rw-r--r--philo_three/src/event.c5
-rw-r--r--philo_three/src/philo_three.h3
-rw-r--r--philo_two/src/event.c5
-rw-r--r--philo_two/src/main.c7
-rw-r--r--philo_two/src/philo_two.h3
-rw-r--r--philo_two/src/routine.c30
15 files changed, 111 insertions, 59 deletions
diff --git a/common/inc/common.h b/common/inc/common.h
index 6d36e35..56fb284 100644
--- a/common/inc/common.h
+++ b/common/inc/common.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */
-/* Updated: 2021/01/04 13:06:52 by charles ### ########.fr */
+/* Updated: 2021/01/10 10:32:17 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -56,6 +56,9 @@ bool parse_args(t_philo_args *args, int argc, char **argv);
long int h_atou_strict(char *s);
t_time h_time_now(void);
void h_sleep(t_time sleep_time);
+char *h_nbrcpy(char *dst, long long int num);
+char *h_strcpy_end(char *dst, const char *str);
+const char *philo_sem_eat_name(const char *prefix, long int id);
/*
** io.c
diff --git a/common/src/helper.c b/common/src/helper.c
index a9d0652..53c24c4 100644
--- a/common/src/helper.c
+++ b/common/src/helper.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */
-/* Updated: 2021/01/03 16:55:42 by cacharle ### ########.fr */
+/* Updated: 2021/01/10 10:32:06 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -57,3 +57,31 @@ void h_sleep(t_time sleep_time)
while (h_time_now() - start < sleep_time)
usleep(500);
}
+
+char *h_nbrcpy(char *dst, long long int num)
+{
+ if (num > 9)
+ dst = h_nbrcpy(dst, num / 10);
+ dst[0] = num % 10 + '0';
+ return (dst + 1);
+}
+
+char *h_strcpy_end(char *dst, const char *str)
+{
+ while (*str != '\0')
+ *dst++ = *str++;
+ return (dst);
+}
+
+#define PHILO_SEM_EAT_BUF_SIZE 2048
+
+const char *philo_sem_eat_name(const char *prefix, long int id)
+{
+ static char buf[PHILO_SEM_EAT_BUF_SIZE];
+ char *end;
+
+ buf[0] = '\0';
+ end = h_strcpy_end(buf, prefix);
+ h_nbrcpy(end, id);
+ return (buf);
+}
diff --git a/common/src/io.c b/common/src/io.c
index 6fc202b..abb26b7 100644
--- a/common/src/io.c
+++ b/common/src/io.c
@@ -6,7 +6,7 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */
-/* Updated: 2021/01/08 21:25:43 by charles ### ########.fr */
+/* Updated: 2021/01/10 10:12:23 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,21 +22,6 @@ static size_t st_strlen(char *s)
return (counter);
}
-static char *st_nbrcpy(char *dst, long long int num)
-{
- if (num > 9)
- dst = st_nbrcpy(dst, num / 10);
- dst[0] = num % 10 + '0';
- return (dst + 1);
-}
-
-static char *st_strcpy_end(char *dst, char *str)
-{
- while (*str != '\0')
- *dst++ = *str++;
- return (dst);
-}
-
#define PHILO_PUT_BUF_SIZE 20000
static char g_buf[PHILO_PUT_BUF_SIZE + 256] = {'\0'};
@@ -44,19 +29,19 @@ static char *g_curr = g_buf;
void philo_put(size_t id, t_philo_event event, t_time initial_time)
{
- g_curr = st_nbrcpy(g_curr, h_time_now() - initial_time);
- g_curr = st_strcpy_end(g_curr, " ");
- g_curr = st_nbrcpy(g_curr, id);
+ g_curr = h_nbrcpy(g_curr, h_time_now() - initial_time);
+ g_curr = h_strcpy_end(g_curr, " ");
+ g_curr = h_nbrcpy(g_curr, id);
if (event == EVENT_FORK)
- g_curr = st_strcpy_end(g_curr, " has taken fork\n");
+ g_curr = h_strcpy_end(g_curr, " has taken fork\n");
else if (event == EVENT_EAT)
- g_curr = st_strcpy_end(g_curr, " is eating\n");
+ g_curr = h_strcpy_end(g_curr, " is eating\n");
else if (event == EVENT_SLEEP)
- g_curr = st_strcpy_end(g_curr, " is sleeping\n");
+ g_curr = h_strcpy_end(g_curr, " is sleeping\n");
else if (event == EVENT_THINK)
- g_curr = st_strcpy_end(g_curr, " is thinking\n");
+ g_curr = h_strcpy_end(g_curr, " is thinking\n");
else if (event == EVENT_DIE)
- g_curr = st_strcpy_end(g_curr, " died\n");
+ g_curr = h_strcpy_end(g_curr, " died\n");
if (g_curr - g_buf > PHILO_PUT_BUF_SIZE)
philo_put_flush();
}
diff --git a/philo_one/src/event.c b/philo_one/src/event.c
index 92ca399..88b9ff1 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/09 13:12:20 by charles ### ########.fr */
+/* Updated: 2021/01/10 09:55:58 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -28,14 +28,15 @@ void event_eat(t_philo *arg)
{
if (philo_finished(arg->conf))
return ;
+ pthread_mutex_lock(&arg->mutex_eat);
pthread_mutex_lock(&arg->conf->mutex_stdout);
if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_EAT, arg->conf->initial_time);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
arg->time_last_eat = h_time_now();
+ pthread_mutex_unlock(&arg->mutex_eat);
h_sleep(arg->conf->timeout_eat);
- arg->time_last_eat = h_time_now();
}
void event_think(t_philo *arg)
diff --git a/philo_one/src/main.c b/philo_one/src/main.c
index d02fb7a..3dacf8f 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/09 14:26:48 by charles ### ########.fr */
+/* Updated: 2021/01/10 09:33:17 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c
index 911f6b9..8a41fe7 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: 2021/01/09 14:39:03 by charles ### ########.fr */
+/* Updated: 2021/01/10 09:49:48 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,6 +30,7 @@ t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks)
philos[i].fork_right = forks + (i + 1) % conf->philo_num;
pthread_mutex_init(&philos[i].mutex_start, NULL);
pthread_mutex_lock(&philos[i].mutex_start);
+ pthread_mutex_init(&philos[i].mutex_eat, NULL);
}
return (philos);
}
diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h
index 910dfe8..aeabc4c 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/09 14:26:58 by charles ### ########.fr */
+/* Updated: 2021/01/10 09:50:02 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -52,6 +52,7 @@ typedef struct s_philo
pthread_mutex_t *fork_right;
pthread_mutex_t *mutex_stdout;
pthread_mutex_t mutex_start;
+ pthread_mutex_t mutex_eat;
} t_philo;
/*
diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c
index 340f3b7..01c74d5 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/09 14:32:33 by charles ### ########.fr */
+/* Updated: 2021/01/10 09:54:30 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -67,9 +67,14 @@ void *routine_philo(t_philo *arg)
void *routine_death(t_philo *arg)
{
- while (!philo_finished(arg->conf) &&
- h_time_now() - arg->time_last_eat < arg->conf->timeout_death)
- usleep(1000);
+ while (!philo_finished(arg->conf))
+ {
+ pthread_mutex_lock(&arg->mutex_eat);
+ if (h_time_now() - arg->time_last_eat > arg->conf->timeout_death)
+ break ;
+ pthread_mutex_unlock(&arg->mutex_eat);
+ usleep(2000);
+ }
event_die(arg);
return (NULL);
}
diff --git a/philo_three/src/child.c b/philo_three/src/child.c
index 641b82d..63ec3e4 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/09 15:59:00 by charles ### ########.fr */
+/* Updated: 2021/01/10 10:39:19 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,13 +14,13 @@
void *routine_death(t_philo *philo)
{
- t_time current;
-
- current = h_time_now();
- while (current - philo->time_last_eat < philo->conf->timeout_death)
+ while (true)
{
- current = h_time_now();
- usleep(1000);
+ sem_wait(philo->sem_eat);
+ if (h_time_now() - philo->time_last_eat > philo->conf->timeout_death)
+ break ;
+ sem_post(philo->sem_eat);
+ usleep(2000);
}
event_die(philo);
return (NULL);
@@ -37,7 +37,6 @@ void st_child_loop(t_philo *philo)
event_take_fork(philo);
event_take_fork(philo);
sem_post(philo->sem_grab);
- philo->time_last_eat = h_time_now();
event_eat(philo);
eat_counter++;
if (philo->conf->meal_num != -1 && eat_counter == philo->conf->meal_num)
@@ -51,10 +50,13 @@ void st_child_loop(t_philo *philo)
}
}
+#define PHILO_SEM_EAT_PREFIX "semaphore_philo_three_eat_"
+
pid_t child_start(t_philo *philo)
{
pthread_t thread_death;
pid_t pid;
+ const char *sem_eat_name;
if ((pid = fork()) == -1)
return (-1);
@@ -66,10 +68,13 @@ pid_t child_start(t_philo *philo)
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);
+ sem_eat_name = philo_sem_eat_name(PHILO_SEM_EAT_PREFIX, philo->id);
+ sem_unlink(sem_eat_name);
+ philo->sem_eat = sem_open(sem_eat_name, O_CREAT | O_EXCL, 0700, 1);
+ sem_wait(philo->sem_start);
philo->time_last_eat = h_time_now();
pthread_create(&thread_death, NULL, (t_routine)routine_death, philo);
pthread_detach(thread_death);
- sem_wait(philo->sem_start);
st_child_loop(philo);
exit(0);
}
diff --git a/philo_three/src/event.c b/philo_three/src/event.c
index a7b6d2a..ef86f46 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/09 15:48:12 by charles ### ########.fr */
+/* Updated: 2021/01/10 10:39:20 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,10 +23,13 @@ void event_take_fork(t_philo *philo)
void event_eat(t_philo *philo)
{
+ sem_wait(philo->sem_eat);
sem_wait(philo->sem_stdout);
philo_put(philo->id, EVENT_EAT, philo->initial_time);
philo_put_flush();
sem_post(philo->sem_stdout);
+ philo->time_last_eat = h_time_now();
+ sem_post(philo->sem_eat);
h_sleep(philo->conf->timeout_eat);
}
diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h
index 18c7bcb..f11b8c7 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/09 15:47:32 by charles ### ########.fr */
+/* Updated: 2021/01/10 10:26:27 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -58,6 +58,7 @@ typedef struct s_philo
sem_t *sem_meal_num;
sem_t *sem_start;
sem_t *sem_grab;
+ sem_t *sem_eat;
} t_philo;
pid_t child_start(t_philo *arg);
diff --git a/philo_two/src/event.c b/philo_two/src/event.c
index 2fc1249..4b86b15 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/09 15:18:24 by charles ### ########.fr */
+/* Updated: 2021/01/10 10:24:44 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,9 +22,12 @@ void event_take_fork(t_philo *arg)
void event_eat(t_philo *arg)
{
+ sem_wait(arg->sem_eat);
sem_wait(arg->conf->sem_stdout);
philo_put(arg->id, EVENT_EAT, arg->conf->initial_time);
sem_post(arg->conf->sem_stdout);
+ arg->time_last_eat = h_time_now();
+ sem_post(arg->sem_eat);
h_sleep(arg->conf->timeout_eat);
}
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
index fb441dc..197ee5c 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/09 15:35:47 by charles ### ########.fr */
+/* Updated: 2021/01/10 10:18:12 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,12 +23,17 @@ static int st_destroy(
t_philo *philos,
pthread_t *threads)
{
+ /* long int i; */
+
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);
+ /* i = -1; */
+ /* while (++i < philos[0].conf->philo_num) */
+ /* sem_unlink(philo_sem_eat_name(philo[i].id)); */
free(philos);
free(threads);
return (1);
diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h
index b2b723e..0126430 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/09 15:17:40 by charles ### ########.fr */
+/* Updated: 2021/01/10 10:03:58 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,6 +43,7 @@ typedef struct
long int id;
t_philo_conf *conf;
t_time time_last_eat;
+ sem_t *sem_eat;
} t_philo;
/*
diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c
index c491a0a..ab01958 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/09 15:17:32 by charles ### ########.fr */
+/* Updated: 2021/01/10 10:30:04 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -28,7 +28,6 @@ void *routine_philo(t_philo *arg)
event_take_fork(arg);
event_take_fork(arg);
sem_post(arg->conf->sem_grab);
- arg->time_last_eat = h_time_now();
event_eat(arg);
if (arg->conf->meal_num != -1 && ++eat_counter == arg->conf->meal_num)
{
@@ -45,22 +44,25 @@ void *routine_philo(t_philo *arg)
void *routine_death(t_philo *arg)
{
- t_time current;
-
- current = h_time_now();
- while (current - arg->time_last_eat < arg->conf->timeout_death)
+ while (true)
{
- current = h_time_now();
- usleep(1000);
+ sem_wait(arg->sem_eat);
+ if (h_time_now() - arg->time_last_eat > arg->conf->timeout_death)
+ break ;
+ sem_post(arg->sem_eat);
+ usleep(2000);
}
event_die(arg);
return (NULL);
}
+#define PHILO_SEM_EAT_PREFIX "semaphore_philo_two_eat_"
+
t_philo *routine_create_philos(t_philo_conf *conf)
{
- int i;
- t_philo *philos;
+ long int i;
+ t_philo *philos;
+ const char *name;
if ((philos = malloc(sizeof(t_philo) * conf->philo_num)) == NULL)
return (NULL);
@@ -69,6 +71,14 @@ t_philo *routine_create_philos(t_philo_conf *conf)
{
philos[i].id = i + 1;
philos[i].conf = conf;
+ name = philo_sem_eat_name(PHILO_SEM_EAT_PREFIX, philos[i].id);
+ sem_unlink(name);
+ philos[i].sem_eat = sem_open(name, O_CREAT | O_EXCL, 0700, 1);
+ if (philos[i].sem_eat == SEM_FAILED)
+ {
+ free(philos);
+ return (NULL);
+ }
}
return (philos);
}