aboutsummaryrefslogtreecommitdiff
path: root/philo_two/src
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-01 14:46:13 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-01 14:46:13 +0100
commitb9d93edf40f228fcc6e18e9e6d0a1c5db498c004 (patch)
tree5abb172c43b54adcf4caef33ede04f64c957246f /philo_two/src
parent83df2b3a49bd3f627ae304bb1529e27d9b2d988c (diff)
downloadphilosophers-b9d93edf40f228fcc6e18e9e6d0a1c5db498c004.tar.gz
philosophers-b9d93edf40f228fcc6e18e9e6d0a1c5db498c004.tar.bz2
philosophers-b9d93edf40f228fcc6e18e9e6d0a1c5db498c004.zip
Added meal counter to philo_two (still segfault 10% of the time)
Diffstat (limited to 'philo_two/src')
-rw-r--r--philo_two/src/event.c41
-rw-r--r--philo_two/src/main.c31
-rw-r--r--philo_two/src/philo_two.h6
-rw-r--r--philo_two/src/routine.c27
4 files changed, 72 insertions, 33 deletions
diff --git a/philo_two/src/event.c b/philo_two/src/event.c
index 8060b2b..faf963b 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: 2020/10/24 13:02:01 by charles ### ########.fr */
+/* Updated: 2021/01/01 14:26:02 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,9 +14,13 @@
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 (!arg->conf->all_alive)
+ if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_FORK);
sem_post(arg->conf->sem_stdout);
@@ -24,25 +28,22 @@ void event_take_fork(t_philo *arg)
void event_eat(t_philo *arg)
{
- int eat_counter;
-
- eat_counter = 0;
- while (eat_counter < arg->conf->meal_num)
- {
- sem_wait(arg->conf->sem_stdout);
- if (!arg->conf->all_alive)
- return ;
- philo_put(arg->id, EVENT_EAT);
- sem_post(arg->conf->sem_stdout);
- usleep(arg->conf->timeout_eat * 1000);
- eat_counter++;
- }
+ if (philo_finished(arg->conf))
+ return ;
+ sem_wait(arg->conf->sem_stdout);
+ if (philo_finished(arg->conf))
+ return ;
+ philo_put(arg->id, EVENT_EAT);
+ 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 (!arg->conf->all_alive)
+ if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_THINK);
sem_post(arg->conf->sem_stdout);
@@ -50,8 +51,10 @@ void event_think(t_philo *arg)
void event_sleep(t_philo *arg)
{
+ if (philo_finished(arg->conf))
+ return ;
sem_wait(arg->conf->sem_stdout);
- if (!arg->conf->all_alive)
+ if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_SLEEP);
sem_post(arg->conf->sem_stdout);
@@ -62,8 +65,10 @@ void event_sleep(t_philo *arg)
void event_die(t_philo *arg)
{
+ if (philo_finished(arg->conf))
+ return ;
sem_wait(arg->conf->sem_stdout);
- if (!arg->conf->all_alive)
+ if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_DIE);
arg->conf->all_alive = false;
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
index 788122b..46ec474 100644
--- a/philo_two/src/main.c
+++ b/philo_two/src/main.c
@@ -6,14 +6,16 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */
-/* Updated: 2020/10/24 13:02:12 by charles ### ########.fr */
+/* Updated: 2021/01/01 14:44:21 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_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"
+
static int st_destroy(
sem_t *forks,
@@ -21,15 +23,17 @@ static int st_destroy(
pthread_t *threads,
t_philo_conf *conf)
{
- int i;
-
- i = -1;
- while (++i < conf->philo_num)
- sem_post(forks);
+ /* 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);
free(philos);
free(threads);
return (1);
@@ -51,13 +55,20 @@ static int st_setup(
conf->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, O_CREAT | O_EXCL, 0700, 1);
if (conf->sem_stdout == SEM_FAILED)
return (1);
+ sem_unlink(PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME);
+ conf->sem_meal_num_finished_counter = sem_open(
+ PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME, O_CREAT | O_EXCL, 0700, 1);
+ if (conf->sem_meal_num_finished_counter == SEM_FAILED)
+ 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;
i = -1;
while (++i < conf->philo_num)
+ {
if (pthread_create(*threads + i, NULL,
(t_routine)routine_philo, *philos + i) != 0)
{
@@ -65,6 +76,8 @@ static int st_setup(
pthread_detach((*threads)[i]);
return (st_destroy(*forks, *philos, *threads, conf));
}
+ usleep(200);
+ }
return (0);
}
@@ -82,7 +95,7 @@ int main(int argc, char **argv)
return (0);
if (st_setup(&conf, &philos, &forks, &threads) != 0)
return (1);
- while (conf.all_alive)
+ while (!philo_finished(&conf))
;
i = -1;
while (++i < conf.philo_num)
diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h
index b947684..ec5783b 100644
--- a/philo_two/src/philo_two.h
+++ b/philo_two/src/philo_two.h
@@ -6,13 +6,14 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 22:47:23 by cacharle #+# #+# */
-/* Updated: 2020/10/24 13:03:58 by charles ### ########.fr */
+/* Updated: 2021/01/01 14:18:57 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILO_TWO_H
# define PHILO_TWO_H
+# define _XOPEN_SOURCE 500
# include <unistd.h>
# include <fcntl.h>
# include <stdbool.h>
@@ -29,7 +30,9 @@ typedef struct
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;
} t_philo_conf;
typedef struct
@@ -44,6 +47,7 @@ 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 29f2e23..783011c 100644
--- a/philo_two/src/routine.c
+++ b/philo_two/src/routine.c
@@ -6,28 +6,45 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */
-/* Updated: 2020/12/31 19:16:32 by charles ### ########.fr */
+/* Updated: 2021/01/01 14:23:59 by charles ### ########.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;
- event_think(arg);
- if (!arg->conf->all_alive)
+ 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);
- while (arg->conf->all_alive)
+ eat_counter = 0;
+ event_think(arg);
+ while (!philo_finished(arg->conf))
{
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)
+ {
+ sem_wait(arg->conf->sem_meal_num_finished_counter);
+ arg->conf->meal_num_finished_counter++;
+ sem_post(arg->conf->sem_meal_num_finished_counter);
+ }
event_sleep(arg);
event_think(arg);
}
@@ -40,7 +57,7 @@ void *routine_death(t_philo *arg)
t_time current;
current = h_time_now();
- while (arg->conf->all_alive
+ while (!philo_finished(arg->conf)
&& current - arg->time_last_eat < arg->conf->timeout_death)
{
current = h_time_now();