aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--philo_one/Makefile4
-rw-r--r--philo_one/src/forks.c25
-rw-r--r--philo_one/src/io.c41
-rw-r--r--philo_one/src/main.c14
-rw-r--r--philo_one/src/philo.c19
-rw-r--r--philo_one/src/philo_one.h37
-rw-r--r--philo_one/src/routine.c15
7 files changed, 55 insertions, 100 deletions
diff --git a/philo_one/Makefile b/philo_one/Makefile
index ea58d9e..4ec2e75 100644
--- a/philo_one/Makefile
+++ b/philo_one/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/24 05:50:15 by cacharle #+# #+# #
-# Updated: 2020/09/30 08:00:20 by cacharle ### ########.fr #
+# Updated: 2020/09/30 09:35:22 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -16,7 +16,7 @@ MAKE = make --no-print-directory
COMMONDIR = ../common
CC = gcc
-CCFLAGS = -I$(COMMONDIR) -Wall -Wextra #-Werror
+CCFLAGS = -g -I$(COMMONDIR) -Wall -Wextra #-Werror
LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon
NAME = philo_one
diff --git a/philo_one/src/forks.c b/philo_one/src/forks.c
index ebe5261..fef1de0 100644
--- a/philo_one/src/forks.c
+++ b/philo_one/src/forks.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/09 23:46:40 by cacharle #+# #+# */
-/* Updated: 2020/09/27 09:26:12 by charles ### ########.fr */
+/* Updated: 2020/09/30 09:45:46 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,7 +24,7 @@ pthread_mutex_t *forks_new(int num)
{
if (pthread_mutex_init(&forks[i], NULL) != 0)
{
- forks_destroy(forks, i + 1);
+ forks_destroy(forks, i);
return (NULL);
}
}
@@ -37,24 +37,3 @@ void forks_destroy(pthread_mutex_t *forks, int num)
pthread_mutex_destroy(&forks[num]);
free(forks);
}
-
-t_routine_arg *forks_dispatch(
- t_philo *philos,
- pthread_mutex_t *forks,
- t_philo_conf *conf)
-{
- int i;
- t_routine_arg *routine_args;
-
- if ((routine_args = malloc(conf->philo_num * sizeof(t_routine_arg))) == NULL)
- return (NULL);
- i = -1;
- while (++i < conf->philo_num)
- {
- routine_args[i].conf = conf;
- routine_args[i].philo = philos + i;
- routine_args[i].fork_left = forks + i % conf->philo_num;
- routine_args[i].fork_right = forks + (i + 1) % conf->philo_num;
- }
- return (routine_args);
-}
diff --git a/philo_one/src/io.c b/philo_one/src/io.c
index 43ae3e6..b5bf82e 100644
--- a/philo_one/src/io.c
+++ b/philo_one/src/io.c
@@ -6,73 +6,66 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2020/09/30 08:16:54 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 09:43:31 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
-void io_take_fork(t_routine_arg *arg, pthread_mutex_t *fork)
+void io_take_fork(t_philo *arg, pthread_mutex_t *fork)
{
pthread_mutex_lock(fork);
- pthread_mutex_lock(&arg->conf->mutex_all_alive);
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
return ;
- pthread_mutex_lock(&arg->conf->mutex_stdout);
- philo_put(arg->philo->id, EVENT_FORK);
+ philo_put(arg->id, EVENT_FORK);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
- pthread_mutex_unlock(&arg->conf->mutex_all_alive);
}
-void io_eat(t_routine_arg *arg)
+void io_eat(t_philo *arg)
{
int eat_counter;
eat_counter = 0;
while (eat_counter < arg->conf->meal_num)
{
- pthread_mutex_lock(&arg->conf->mutex_all_alive);
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
return ;
- pthread_mutex_lock(&arg->conf->mutex_stdout);
- philo_put(arg->philo->id, EVENT_EAT);
+ philo_put(arg->id, EVENT_EAT);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
- pthread_mutex_unlock(&arg->conf->mutex_all_alive);
usleep(arg->conf->timeout_eat * 1000);
eat_counter++;
}
}
-void io_think(t_routine_arg *arg)
+void io_think(t_philo *arg)
{
- pthread_mutex_lock(&arg->conf->mutex_all_alive);
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
return ;
- pthread_mutex_lock(&arg->conf->mutex_stdout);
- philo_put(arg->philo->id, EVENT_THINK);
+ philo_put(arg->id, EVENT_THINK);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
- pthread_mutex_unlock(&arg->conf->mutex_all_alive);
}
-void io_sleep(t_routine_arg *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left)
+void io_sleep(t_philo *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left)
{
- pthread_mutex_lock(&arg->conf->mutex_all_alive);
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
return ;
- pthread_mutex_lock(&arg->conf->mutex_stdout);
- philo_put(arg->philo->id, EVENT_SLEEP);
+ philo_put(arg->id, EVENT_SLEEP);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
- pthread_mutex_unlock(&arg->conf->mutex_all_alive);
pthread_mutex_unlock(fork_right);
pthread_mutex_unlock(fork_left);
usleep(arg->conf->timeout_sleep * 1000);
}
-void io_die(t_routine_arg *arg)
+void io_die(t_philo *arg)
{
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
return ;
- pthread_mutex_lock(&arg->conf->mutex_stdout);
- philo_put(arg->philo->id, EVENT_DIE);
+ arg->conf->all_alive = false;
+ philo_put(arg->id, EVENT_DIE);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
}
diff --git a/philo_one/src/main.c b/philo_one/src/main.c
index 53c1c56..dce44fc 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: 2020/09/30 08:06:03 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 09:49:48 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,7 +17,6 @@ int main(int argc, char **argv)
t_philo_conf conf;
t_philo *philos;
pthread_mutex_t *forks;
- t_routine_arg *routine_args;
if (!parse_args((t_philo_args*)&conf, argc, argv))
return (1);
@@ -25,22 +24,17 @@ int main(int argc, char **argv)
return (0);
if ((forks = forks_new(conf.philo_num)) == NULL)
return (1);
- if ((philos = philos_new(conf.philo_num)) == NULL)
- return (1);
- if ((routine_args = forks_dispatch(philos, forks, &conf)) == NULL)
+ if ((philos = philos_new(&conf, forks)) == NULL)
return (1);
conf.all_alive = true;
- pthread_mutex_init(&conf.mutex_all_alive, NULL);
pthread_mutex_init(&conf.mutex_stdout, NULL);
- if (!philos_start(philos, routine_args, conf.philo_num))
+ if (!philos_start(philos, conf.philo_num))
return (1);
while (conf.all_alive)
;
philos_detach(philos, conf.philo_num);
+ forks_destroy(forks, conf.philo_num);
pthread_mutex_destroy(&conf.mutex_stdout);
- pthread_mutex_destroy(&conf.mutex_all_alive);
- free(routine_args);
free(philos);
- forks_destroy(forks, conf.philo_num);
return (0);
}
diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c
index fd14674..45a8a17 100644
--- a/philo_one/src/philo.c
+++ b/philo_one/src/philo.c
@@ -6,28 +6,33 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */
-/* Updated: 2020/09/30 08:38:48 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 09:48:12 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
-t_philo *philos_new(int num)
+t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks)
{
int i;
t_philo *philos;
- if (num < 0)
+ if (conf->philo_num < 0)
return (NULL);
- if ((philos = malloc(num * sizeof(t_philo))) == NULL)
+ if ((philos = malloc(conf->philo_num * sizeof(t_philo))) == NULL)
return (NULL);
i = -1;
- while (++i < num)
+ while (++i < conf->philo_num)
+ {
philos[i].id = i + 1;
+ philos[i].conf = conf;
+ philos[i].fork_left = forks + i % conf->philo_num;
+ philos[i].fork_right = forks + (i + 1) % conf->philo_num;
+ }
return (philos);
}
-bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num)
+bool philos_start(t_philo *philos, int num)
{
int i;
@@ -35,7 +40,7 @@ bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num)
while (++i < num)
{
if (pthread_create(&philos[i].thread, NULL,
- (t_routine)routine_philo, (void*)(routine_args + i)) == -1)
+ (t_routine)routine_philo, (void*)(philos + i)) == -1)
return (false);
}
return (true);
diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h
index 2742072..9fc5e5f 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: 2020/09/30 08:16:11 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 09:50:02 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,8 +35,7 @@ typedef struct
t_time timeout_sleep;
long int meal_num;
bool all_alive;
- pthread_mutex_t mutex_stdout; // duplication in t_routine_arg
- pthread_mutex_t mutex_all_alive;
+ pthread_mutex_t mutex_stdout;
} t_philo_conf;
typedef struct s_philo
@@ -44,16 +43,11 @@ typedef struct s_philo
int id;
pthread_t thread;
t_time time_last_eat;
-} t_philo;
-
-typedef struct s_routine_arg
-{
t_philo_conf *conf;
- t_philo *philo;
pthread_mutex_t *fork_left;
pthread_mutex_t *fork_right;
pthread_mutex_t *mutex_stdout;
-} t_routine_arg;
+} t_philo;
/*
** forks.c
@@ -61,38 +55,31 @@ typedef struct s_routine_arg
pthread_mutex_t *forks_new(int num);
void forks_destroy(pthread_mutex_t *forks, int num);
-t_routine_arg *forks_dispatch(
- t_philo *philos,
- pthread_mutex_t *forks,
- t_philo_conf *conf);
/*
** philo.c
*/
-t_philo *philos_new(int num);
+t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks);
void philos_destroy(t_philo *philos, int num);
-bool philos_start(
- t_philo *philos,
- t_routine_arg *routine_args,
- int num);
+bool philos_start(t_philo *philos, int num);
void philos_detach(t_philo *philos, int num);
/*
** routine.c
*/
-void *routine_philo(t_routine_arg *arg);
-void *routine_death(t_routine_arg *arg);
+void *routine_philo(t_philo *arg);
+void *routine_death(t_philo *arg);
/*
** io.c
*/
-void io_take_fork(t_routine_arg *arg, pthread_mutex_t *fork);
-void io_eat(t_routine_arg *arg);
-void io_think(t_routine_arg *arg);
-void io_sleep(t_routine_arg *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left);
-void io_die(t_routine_arg *arg);
+void io_take_fork(t_philo *arg, pthread_mutex_t *fork);
+void io_eat(t_philo *arg);
+void io_think(t_philo *arg);
+void io_sleep(t_philo *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left);
+void io_die(t_philo *arg);
#endif
diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c
index 4b829f0..5f76a26 100644
--- a/philo_one/src/routine.c
+++ b/philo_one/src/routine.c
@@ -6,19 +6,19 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */
-/* Updated: 2020/09/30 08:38:59 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 09:43:16 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
-void *routine_philo(t_routine_arg *arg)
+void *routine_philo(t_philo *arg)
{
pthread_t thread_death;
if (!arg->conf->all_alive)
return (NULL);
- arg->philo->time_last_eat = h_time_now();
+ arg->time_last_eat = h_time_now();
if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0)
return (NULL);
io_think(arg);
@@ -26,7 +26,7 @@ void *routine_philo(t_routine_arg *arg)
{
io_take_fork(arg, arg->fork_left);
io_take_fork(arg, arg->fork_right);
- arg->philo->time_last_eat = h_time_now();
+ arg->time_last_eat = h_time_now();
io_eat(arg);
io_sleep(arg, arg->fork_right, arg->fork_left);
io_think(arg);
@@ -35,19 +35,16 @@ void *routine_philo(t_routine_arg *arg)
return (NULL);
}
-void *routine_death(t_routine_arg *arg)
+void *routine_death(t_philo *arg)
{
t_time current;
current = h_time_now();
while (arg->conf->all_alive &&
- current - arg->philo->time_last_eat < arg->conf->timeout_death)
+ current - arg->time_last_eat < arg->conf->timeout_death)
current = h_time_now();
if (!arg->conf->all_alive)
return (NULL);
- pthread_mutex_lock(&arg->conf->mutex_all_alive);
io_die(arg);
- arg->conf->all_alive = false;
- pthread_mutex_unlock(&arg->conf->mutex_all_alive);
return (NULL);
}