aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-12-30 14:06:17 +0100
committerCharles Cabergs <me@cacharle.xyz>2020-12-30 14:06:17 +0100
commitcf2ef2c3901f3da524079a9ad912dfb99e115a7e (patch)
tree7868f1f02a3fab1aa74606c1f9a3a00c0287976f
parent2baa67fd7ad31fe53ab21d257a104478e787fb62 (diff)
downloadphilosophers-cf2ef2c3901f3da524079a9ad912dfb99e115a7e.tar.gz
philosophers-cf2ef2c3901f3da524079a9ad912dfb99e115a7e.tar.bz2
philosophers-cf2ef2c3901f3da524079a9ad912dfb99e115a7e.zip
Fixed meal_num args for philo_one
-rw-r--r--Makefile12
-rw-r--r--common/args.c4
-rw-r--r--correction.pngbin0 -> 180674 bytes
-rw-r--r--philo_one/Makefile4
-rw-r--r--philo_one/src/event.c21
-rw-r--r--philo_one/src/main.c17
-rw-r--r--philo_one/src/philo.c3
-rw-r--r--philo_one/src/philo_one.h4
-rw-r--r--philo_one/src/routine.c14
9 files changed, 49 insertions, 30 deletions
diff --git a/Makefile b/Makefile
index 198661e..34f114d 100644
--- a/Makefile
+++ b/Makefile
@@ -6,20 +6,20 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/02/09 03:31:28 by cacharle #+# #+# #
-# Updated: 2020/10/24 13:05:33 by charles ### ########.fr #
+# Updated: 2020/12/30 13:48:29 by charles ### ########.fr #
# #
# **************************************************************************** #
MAKE = make --no-print-directory
-PHILO_ONE_DIR = philo_one
-PHILO_TWO_DIR = philo_two
+PHILO_ONE_DIR = philo_one
+PHILO_TWO_DIR = philo_two
PHILO_THREE_DIR = philo_three
help:
- @echo "make philo_one - compile philo_one"
- @echo "make philo_two - compile philo_two"
- @echo "make philo_three - compile philo_three"
+ @echo "make philo_one - compile philo_one"
+ @echo "make philo_two - compile philo_two"
+ @echo "make philo_three - compile philo_three"
philo_one:
$(MAKE) -C $(PHILO_ONE_DIR)
diff --git a/common/args.c b/common/args.c
index e55f004..2070e46 100644
--- a/common/args.c
+++ b/common/args.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
-/* Updated: 2020/10/05 14:26:40 by cacharle ### ########.fr */
+/* Updated: 2020/12/30 13:44:54 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,6 +30,6 @@ bool parse_args(t_philo_args *args, int argc, char **argv)
return (false);
}
else
- args->meal_num = 1;
+ args->meal_num = -1;
return (true);
}
diff --git a/correction.png b/correction.png
new file mode 100644
index 0000000..3b1558e
--- /dev/null
+++ b/correction.png
Binary files differ
diff --git a/philo_one/Makefile b/philo_one/Makefile
index 9fb6a6a..e6e3ec1 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/10/24 13:02:59 by charles ### ########.fr #
+# Updated: 2020/12/30 13:55:45 by charles ### ########.fr #
# #
# **************************************************************************** #
@@ -16,7 +16,7 @@ MAKE = make --no-print-directory
COMMONDIR = ../common
CC = gcc
-CCFLAGS = -g -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/event.c b/philo_one/src/event.c
index 5261f4c..ef3d7a5 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: 2020/10/05 14:27:39 by cacharle ### ########.fr */
+/* Updated: 2020/12/30 13:39:56 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,19 +24,12 @@ void event_take_fork(t_philo *arg, pthread_mutex_t *fork)
void event_eat(t_philo *arg)
{
- int eat_counter;
-
- eat_counter = 0;
- while (eat_counter < arg->conf->meal_num)
- {
- pthread_mutex_lock(&arg->conf->mutex_stdout);
- if (!arg->conf->all_alive)
- return ;
- philo_put(arg->id, EVENT_EAT);
- pthread_mutex_unlock(&arg->conf->mutex_stdout);
- usleep(arg->conf->timeout_eat * 1000);
- eat_counter++;
- }
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
+ if (!arg->conf->all_alive)
+ return ;
+ philo_put(arg->id, EVENT_EAT);
+ pthread_mutex_unlock(&arg->conf->mutex_stdout);
+ usleep(arg->conf->timeout_eat * 1000);
}
void event_think(t_philo *arg)
diff --git a/philo_one/src/main.c b/philo_one/src/main.c
index eec5d4e..a9a6a63 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/10/05 15:42:09 by cacharle ### ########.fr */
+/* Updated: 2020/12/30 14:04:42 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,7 +30,15 @@ static int st_setup(
free(*philos);
return (1);
}
+ if (pthread_mutex_init(&conf->mutex_meal_num_finished_counter, NULL) != 0)
+ {
+ forks_destroy(*forks, conf->philo_num);
+ free(*philos);
+ pthread_mutex_destroy(&conf->mutex_stdout);
+ return (1);
+ }
conf->all_alive = true;
+ conf->meal_num_finished_counter = 0;
return (0);
}
@@ -42,7 +50,7 @@ int main(int argc, char **argv)
if (!parse_args((t_philo_args*)&conf, argc, argv))
return (1);
- if (conf.philo_num == 0)
+ if (conf.philo_num == 0 || conf.meal_num == 0)
return (0);
if (st_setup(&conf, &philos, &forks) != 0)
return (1);
@@ -52,11 +60,14 @@ int main(int argc, char **argv)
free(philos);
return (1);
}
- while (conf.all_alive)
+ while (conf.all_alive && (conf.meal_num != -1 && conf.meal_num_finished_counter != conf.philo_num))
+ /* printf("%ld\n", conf.meal_num_finished_counter); */
;
+ conf.all_alive = false;
philos_detach(philos, conf.philo_num);
forks_destroy(forks, conf.philo_num);
pthread_mutex_destroy(&conf.mutex_stdout);
+ pthread_mutex_destroy(&conf.mutex_meal_num_finished_counter);
free(philos);
return (0);
}
diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c
index 30c0a7c..3e3d90f 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: 2020/10/05 16:13:10 by cacharle ### ########.fr */
+/* Updated: 2020/12/30 11:58:51 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -49,6 +49,7 @@ bool philos_start(t_philo *philos, int num)
pthread_detach(philos[i].thread);
return (false);
}
+ usleep(200);
}
return (true);
}
diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h
index ae7d040..341c1a8 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/10/05 14:28:24 by cacharle ### ########.fr */
+/* Updated: 2020/12/30 13:44:14 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,7 +35,9 @@ typedef struct
t_time timeout_sleep;
long int meal_num;
bool all_alive;
+ long int meal_num_finished_counter;
pthread_mutex_t mutex_stdout;
+ pthread_mutex_t mutex_meal_num_finished_counter;
} t_philo_conf;
typedef struct s_philo
diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c
index c1a7a4f..280f9d6 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: 2020/09/30 09:43:16 by cacharle ### ########.fr */
+/* Updated: 2020/12/30 13:44:25 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,6 +15,7 @@
void *routine_philo(t_philo *arg)
{
pthread_t thread_death;
+ long int eat_counter;
if (!arg->conf->all_alive)
return (NULL);
@@ -22,12 +23,20 @@ void *routine_philo(t_philo *arg)
if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0)
return (NULL);
event_think(arg);
+ eat_counter = 0;
while (arg->conf->all_alive)
{
event_take_fork(arg, arg->fork_left);
event_take_fork(arg, arg->fork_right);
arg->time_last_eat = h_time_now();
event_eat(arg);
+ eat_counter++;
+ if (arg->conf->meal_num != -1 && eat_counter == arg->conf->meal_num)
+ {
+ pthread_mutex_lock(&arg->conf->mutex_meal_num_finished_counter);
+ arg->conf->meal_num_finished_counter++;
+ pthread_mutex_unlock(&arg->conf->mutex_meal_num_finished_counter);
+ }
event_sleep(arg, arg->fork_right, arg->fork_left);
event_think(arg);
}
@@ -42,7 +51,10 @@ void *routine_death(t_philo *arg)
current = h_time_now();
while (arg->conf->all_alive &&
current - arg->time_last_eat < arg->conf->timeout_death)
+ {
current = h_time_now();
+ usleep(200);
+ }
if (!arg->conf->all_alive)
return (NULL);
event_die(arg);