aboutsummaryrefslogtreecommitdiff
path: root/philo_two
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-01 16:39:36 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-01 16:39:36 +0100
commita73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb (patch)
treeee14275a0f1ad9b8ddfa8feee1ee77ce0de5f285 /philo_two
parentb9d93edf40f228fcc6e18e9e6d0a1c5db498c004 (diff)
downloadphilosophers-a73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb.tar.gz
philosophers-a73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb.tar.bz2
philosophers-a73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb.zip
Fixing meal num for philo_three
Diffstat (limited to 'philo_two')
-rw-r--r--philo_two/Makefile4
-rw-r--r--philo_two/src/event.c18
-rw-r--r--philo_two/src/main.c26
3 files changed, 33 insertions, 15 deletions
diff --git a/philo_two/Makefile b/philo_two/Makefile
index 6ee7cca..a4373d0 100644
--- a/philo_two/Makefile
+++ b/philo_two/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/24 05:50:15 by cacharle #+# #+# #
-# Updated: 2021/01/01 14:45:16 by charles ### ########.fr #
+# Updated: 2021/01/01 15:18:02 by charles ### ########.fr #
# #
# **************************************************************************** #
@@ -16,7 +16,7 @@ MAKE = make --no-print-directory
COMMONDIR = ../common
CC = gcc
-CCFLAGS = -g -I$(COMMONDIR) -std=c99 -Wall -Wextra -Werror
+CCFLAGS = -g -I$(COMMONDIR) -O2 -std=c99 -Wall -Wextra -Werror
LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon
NAME = philo_two
diff --git a/philo_two/src/event.c b/philo_two/src/event.c
index faf963b..0eaad11 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/01 14:26:02 by charles ### ########.fr */
+/* Updated: 2021/01/01 15:57:43 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,6 +23,8 @@ void event_take_fork(t_philo *arg)
if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_FORK);
+ if (philo_finished(arg->conf))
+ return ;
sem_post(arg->conf->sem_stdout);
}
@@ -34,6 +36,8 @@ void event_eat(t_philo *arg)
if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_EAT);
+ if (philo_finished(arg->conf))
+ return ;
sem_post(arg->conf->sem_stdout);
usleep(arg->conf->timeout_eat * 1000);
}
@@ -46,6 +50,8 @@ void event_think(t_philo *arg)
if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_THINK);
+ if (philo_finished(arg->conf))
+ return ;
sem_post(arg->conf->sem_stdout);
}
@@ -57,9 +63,17 @@ void event_sleep(t_philo *arg)
if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_SLEEP);
+ if (philo_finished(arg->conf))
+ return ;
sem_post(arg->conf->sem_stdout);
+ if (philo_finished(arg->conf))
+ return ;
sem_post(arg->forks);
+ if (philo_finished(arg->conf))
+ return ;
sem_post(arg->forks);
+ if (philo_finished(arg->conf))
+ return ;
usleep(arg->conf->timeout_sleep * 1000);
}
@@ -72,5 +86,7 @@ void event_die(t_philo *arg)
return ;
philo_put(arg->id, EVENT_DIE);
arg->conf->all_alive = false;
+ if (philo_finished(arg->conf))
+ return ;
sem_post(arg->conf->sem_stdout);
}
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
index 46ec474..85994da 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/01 14:44:21 by charles ### ########.fr */
+/* Updated: 2021/01/01 15:57:40 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,7 +16,6 @@
#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,
t_philo *philos,
@@ -39,25 +38,27 @@ static int st_destroy(
return (1);
}
+static sem_t *st_sem_create(const char *name, unsigned int value)
+{
+ sem_unlink(name);
+ return (sem_open(name, O_CREAT | O_EXCL, 0700, value));
+}
+
static int st_setup(
t_philo_conf *conf,
t_philo **philos,
sem_t **forks,
pthread_t **threads)
{
- int i;
+ long int i;
- sem_unlink(PHILO_SEM_NAME);
- *forks = sem_open(PHILO_SEM_NAME, O_CREAT | O_EXCL, 0700, conf->philo_num);
+ *forks = st_sem_create(PHILO_SEM_NAME, conf->philo_num);
if (*forks == SEM_FAILED)
return (1);
- sem_unlink(PHILO_SEM_STDOUT_NAME);
- conf->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, O_CREAT | O_EXCL, 0700, 1);
+ conf->sem_stdout = st_sem_create(PHILO_SEM_STDOUT_NAME, 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);
+ conf->sem_meal_num_finished_counter = st_sem_create(PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME, 1);
if (conf->sem_meal_num_finished_counter == SEM_FAILED)
return (1);
*threads = NULL;
@@ -83,7 +84,7 @@ static int st_setup(
int main(int argc, char **argv)
{
- int i;
+ long int i;
t_philo_conf conf;
t_philo *philos;
sem_t *forks;
@@ -91,12 +92,13 @@ 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, &threads) != 0)
return (1);
while (!philo_finished(&conf))
;
+ conf.all_alive = false;
i = -1;
while (++i < conf.philo_num)
pthread_detach(threads[i]);