aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-02-14 21:24:22 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-14 21:24:22 +0100
commitf6a960c09c9593af72ff6da2c3ed501e01a0f429 (patch)
tree783c452486c2528fe0df25d76f49d1d4f20c1d47
parentadbe9cdb61e5d12299b8872b2ac27a48036bc95d (diff)
downloadphilosophers-f6a960c09c9593af72ff6da2c3ed501e01a0f429.tar.gz
philosophers-f6a960c09c9593af72ff6da2c3ed501e01a0f429.tar.bz2
philosophers-f6a960c09c9593af72ff6da2c3ed501e01a0f429.zip
philo one working (with what may be a hack)
-rw-r--r--.gitignore1
-rw-r--r--common/common.c2
-rw-r--r--common/common.h6
-rw-r--r--common/helper.c14
-rw-r--r--common/philo.c2
-rw-r--r--philo_one/Makefile4
-rw-r--r--philo_one/fork.c11
-rw-r--r--philo_one/main.c26
-rw-r--r--philo_one/philo.c6
-rw-r--r--philo_one/philo_one.h2
-rw-r--r--philo_one/routine.c79
11 files changed, 97 insertions, 56 deletions
diff --git a/.gitignore b/.gitignore
index 4275a65..e531b95 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
*.o
*.ghc
*.a
+*.dSYM
a.out
philo_one/philo_one
philo_two/philo_two
diff --git a/common/common.c b/common/common.c
index 0ec7f80..381256b 100644
--- a/common/common.c
+++ b/common/common.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
-/* Updated: 2020/02/14 01:32:37 by cacharle ### ########.fr */
+/* Updated: 2020/02/14 19:42:18 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/common/common.h b/common/common.h
index e314960..a838750 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: 2020/02/14 00:35:30 by cacharle ### ########.fr */
+/* Updated: 2020/02/14 20:45:33 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -48,6 +48,9 @@ typedef struct
t_time timeout_eat;
t_time timeout_sleep;
int meal_num;
+ t_bool all_alive;
+ pthread_mutex_t mutex_stdout;
+ pthread_mutex_t mutex_all_alive;
} t_philo_args;
typedef void (*t_philo_routine)(void *arg);
@@ -79,5 +82,6 @@ void h_putchar(char c);
void h_putstr(char *s);
void *h_calloc(int count, int size);
t_time h_timeval_to_time(struct timeval *tp);
+t_time h_time_now(void);
#endif
diff --git a/common/helper.c b/common/helper.c
index afdb7f3..2826ceb 100644
--- a/common/helper.c
+++ b/common/helper.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */
-/* Updated: 2020/02/14 01:37:31 by cacharle ### ########.fr */
+/* Updated: 2020/02/14 21:07:54 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -74,7 +74,15 @@ t_time h_timeval_to_time(struct timeval *tp)
{
t_time t;
- t = tp->tv_sec * 1000;
- t += tp->tv_usec / 1000;
+ t = tp->tv_sec * 1000 + tp->tv_usec / 1000;
return (t);
}
+
+t_time h_time_now(void)
+{
+ struct timeval tv;
+
+ if (gettimeofday(&tv, NULL) == -1)
+ return (-1);
+ return (h_timeval_to_time(&tv));
+}
diff --git a/common/philo.c b/common/philo.c
index dfc9278..d342802 100644
--- a/common/philo.c
+++ b/common/philo.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/09 01:54:53 by cacharle #+# #+# */
-/* Updated: 2020/02/14 00:29:17 by cacharle ### ########.fr */
+/* Updated: 2020/02/14 21:21:02 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/philo_one/Makefile b/philo_one/Makefile
index b043ab5..b630f40 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/02/14 00:50:47 by cacharle ### ########.fr #
+# Updated: 2020/02/14 19:53:14 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -15,7 +15,7 @@ RM = rm -f
COMMON_DIR = ../common
CC = gcc
-CCFLAGS = -I$(COMMON_DIR) -Wall -Wextra #-Werror
+CCFLAGS = -g -I$(COMMON_DIR) -Wall -Wextra #-Werror
LDFLAGS = -lpthread -L$(COMMON_DIR) -lphilocommon
NAME = philo_one
diff --git a/philo_one/fork.c b/philo_one/fork.c
index 62e3355..fd7ee90 100644
--- a/philo_one/fork.c
+++ b/philo_one/fork.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/09 23:46:40 by cacharle #+# #+# */
-/* Updated: 2020/02/14 01:29:26 by cacharle ### ########.fr */
+/* Updated: 2020/02/14 21:18:02 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,7 +22,7 @@ t_fork *forks_new(int num)
i = -1;
while (++i < num)
{
- if (pthread_mutex_init(&forks[num].mutex, NULL) != 0)
+ if (pthread_mutex_init(&forks[i].mutex, NULL) != 0)
{
forks_destroy(forks, i + 1);
return (NULL);
@@ -36,8 +36,9 @@ void forks_destroy(t_fork *forks, int num)
while (num-- > 0)
{
forks[num].used = TRUE;
- /* pthread_mutex_destroy(&forks[num].mutex); */
+ pthread_mutex_destroy(&forks[num].mutex);
}
+ free(forks);
}
t_routine_arg *forks_dispatch(t_philo *philos, t_fork *forks, t_philo_args *args)
@@ -52,8 +53,8 @@ t_routine_arg *forks_dispatch(t_philo *philos, t_fork *forks, t_philo_args *args
{
routine_args[i].args = args;
routine_args[i].philo = philos + i;
- routine_args[i].fork_right = forks + i % args->philo_num;
- routine_args[i].fork_left = forks + (i - 1) % args->philo_num;
+ routine_args[i].fork_left = forks + i % args->philo_num;
+ routine_args[i].fork_right = forks + (i + 1) % args->philo_num;
}
return (routine_args);
}
diff --git a/philo_one/main.c b/philo_one/main.c
index 94ac0f7..ddc9e52 100644
--- a/philo_one/main.c
+++ b/philo_one/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */
-/* Updated: 2020/02/14 01:41:21 by cacharle ### ########.fr */
+/* Updated: 2020/02/14 20:52:02 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,35 +19,25 @@ int main(int argc, char **argv)
t_philo *philos;
t_fork *forks;
t_routine_arg *routine_args;
- /* mutex_t mutex_stdout; */
- /* pthread_mutex_init(mutex_stdout); */
if (!parse_args(&philo_args, argc, argv))
return (1);
if ((forks = forks_new(philo_args.philo_num)) == NULL)
return (1);
if ((philos = philos_new(philo_args.philo_num)) == NULL)
- {
- free(forks);
return (1);
- }
if ((routine_args = forks_dispatch(philos, forks, &philo_args)) == NULL)
- {
- free(philos);
- free(forks);
return (1);
- }
+ philo_args.all_alive = TRUE;
+ pthread_mutex_init(&philo_args.mutex_all_alive, NULL);
+ pthread_mutex_init(&philo_args.mutex_stdout, NULL);
if (!philos_start(philos, routine_args, philo_args.philo_num))
- {
- free(philos);
- free(forks);
- free(routine_args);
return (1);
- }
- while (TRUE)
- if (philos_starved(philos, philo_args.philo_num))
- break;
+ while (philo_args.all_alive)
+ ;
philos_join(philos, philo_args.philo_num);
+ pthread_mutex_destroy(&philo_args.mutex_stdout);
+ pthread_mutex_destroy(&philo_args.mutex_all_alive);
free(routine_args);
free(philos);
forks_destroy(forks, philo_args.philo_num);
diff --git a/philo_one/philo.c b/philo_one/philo.c
index c585e34..84dfd7f 100644
--- a/philo_one/philo.c
+++ b/philo_one/philo.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */
-/* Updated: 2020/02/14 01:38:45 by cacharle ### ########.fr */
+/* Updated: 2020/02/14 20:07:57 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,7 +30,7 @@ t_philo *philos_new(int num)
return (philos);
}
-void philos_destroy(t_philo *philos, int num)
+void philos_destroy(t_philo *philos, int num)
{
(void)num;
if (philos == NULL)
@@ -62,7 +62,7 @@ void philos_join(t_philo *philos, int num)
if (philos[i].alive)
{
philos[i].alive = FALSE;
- /* pthread_join(philos[i].thread, NULL); */
+ pthread_join(philos[i].thread, NULL);
}
}
}
diff --git a/philo_one/philo_one.h b/philo_one/philo_one.h
index 9c888ee..486cfeb 100644
--- a/philo_one/philo_one.h
+++ b/philo_one/philo_one.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */
-/* Updated: 2020/02/14 01:41:39 by cacharle ### ########.fr */
+/* Updated: 2020/02/14 20:23:15 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/philo_one/routine.c b/philo_one/routine.c
index 713e4ee..c9e2339 100644
--- a/philo_one/routine.c
+++ b/philo_one/routine.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */
-/* Updated: 2020/02/14 00:45:23 by cacharle ### ########.fr */
+/* Updated: 2020/02/14 21:22:52 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,26 +18,54 @@ void *routine_philo(void *void_arg)
pthread_t thread_death;
arg = (t_routine_arg*)void_arg;
+
+ if (!arg->args->all_alive)
+ return (NULL);
+ arg->philo->time_last_eat = h_time_now();
if (pthread_create(&thread_death, NULL, routine_death, arg) != 0)
return (NULL);
- philo_think(arg->philo->id);
- while (arg->philo->alive)
+ if (!arg->args->all_alive)
+ return (NULL);
+ pthread_mutex_lock(&arg->args->mutex_stdout);
+ if (arg->args->all_alive)
+ philo_think(arg->philo->id);
+ pthread_mutex_unlock(&arg->args->mutex_stdout);
+ while (arg->args->all_alive)
{
if (!arg->fork_left->used && !arg->fork_right->used)
{
pthread_mutex_lock(&arg->fork_left->mutex);
pthread_mutex_lock(&arg->fork_right->mutex);
- philo_eat(arg->philo->id, arg->args->timeout_eat);
+ arg->fork_left->used = TRUE;
+ arg->fork_right->used = TRUE;
+ pthread_mutex_unlock(&arg->fork_left->mutex);
+ pthread_mutex_unlock(&arg->fork_right->mutex);
+
+ arg->philo->time_last_eat = h_time_now();
+ pthread_mutex_lock(&arg->args->mutex_stdout);
+ if (arg->args->all_alive)
+ philo_eat(arg->philo->id, arg->args->timeout_eat);
+ pthread_mutex_unlock(&arg->args->mutex_stdout);
+
+ pthread_mutex_lock(&arg->fork_left->mutex);
+ pthread_mutex_lock(&arg->fork_right->mutex);
+ arg->fork_left->used = FALSE;
+ arg->fork_right->used = FALSE;
pthread_mutex_unlock(&arg->fork_left->mutex);
pthread_mutex_unlock(&arg->fork_right->mutex);
- philo_sleep(arg->philo->id, arg->args->timeout_sleep);
- philo_think(arg->philo->id);
+
+ pthread_mutex_lock(&arg->args->mutex_stdout);
+ if (arg->args->all_alive)
+ philo_sleep(arg->philo->id, arg->args->timeout_sleep);
+ pthread_mutex_unlock(&arg->args->mutex_stdout);
+
+ pthread_mutex_lock(&arg->args->mutex_stdout);
+ if (arg->args->all_alive)
+ philo_think(arg->philo->id);
+ pthread_mutex_unlock(&arg->args->mutex_stdout);
}
}
- if (arg->philo->alive)
- philo_die(arg->philo->id);
- /* pthread_join( */
- free(arg);
+ pthread_join(thread_death, NULL);
return (NULL);
}
@@ -45,19 +73,28 @@ void *routine_death(void *void_arg)
{
t_routine_arg *arg;
t_time current;
- struct timeval tv;
+ /* if (!arg->args->all_alive) */
+ /* return (NULL); */
arg = (t_routine_arg*)void_arg;
- if (gettimeofday(&tv, NULL) == -1)
- return (NULL);
- current = h_timeval_to_time(&tv);
- while (arg->philo->alive &&
+
+ current = h_time_now();
+ /* pthread_mutex_lock(&arg->args->mutex_stdout); */
+ /* printf("===\n"); */
+ /* printf("%d crr %ld\n", arg->philo->id, current); */
+ /* printf("%d lst %ld\n", arg->philo->id, arg->philo->time_last_eat); */
+ /* printf("%d dif %ld\n", arg->philo->id, current - arg->philo->time_last_eat); */
+ /* printf("===\n"); */
+ /* pthread_mutex_unlock(&arg->args->mutex_stdout); */
+ while (arg->args->all_alive &&
current - arg->philo->time_last_eat < arg->args->timeout_death)
- {
- if (gettimeofday(&tv, NULL) == -1)
- return (NULL);
- current = h_timeval_to_time(&tv);
- }
- arg->philo->alive = FALSE;
+ current = h_time_now();
+ pthread_mutex_lock(&arg->args->mutex_stdout);
+ pthread_mutex_lock(&arg->args->mutex_all_alive);
+ if (arg->args->all_alive)
+ philo_die(arg->philo->id);
+ arg->args->all_alive = FALSE;
+ pthread_mutex_unlock(&arg->args->mutex_all_alive);
+ pthread_mutex_unlock(&arg->args->mutex_stdout);
return (NULL);
}