aboutsummaryrefslogtreecommitdiff
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
parentb9d93edf40f228fcc6e18e9e6d0a1c5db498c004 (diff)
downloadphilosophers-a73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb.tar.gz
philosophers-a73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb.tar.bz2
philosophers-a73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb.zip
Fixing meal num for philo_three
-rw-r--r--philo_one/Makefile4
-rw-r--r--philo_three/Makefile4
-rw-r--r--philo_three/src/child.c15
-rw-r--r--philo_three/src/event.c28
-rw-r--r--philo_three/src/main.c45
-rw-r--r--philo_three/src/philo_three.h9
-rw-r--r--philo_two/Makefile4
-rw-r--r--philo_two/src/event.c18
-rw-r--r--philo_two/src/main.c26
9 files changed, 97 insertions, 56 deletions
diff --git a/philo_one/Makefile b/philo_one/Makefile
index 76874a6..0026516 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: 2021/01/01 13:50:11 by charles ### ########.fr #
+# Updated: 2021/01/01 15:18:11 by charles ### ########.fr #
# #
# **************************************************************************** #
@@ -16,7 +16,7 @@ MAKE = make --no-print-directory
COMMONDIR = ../common
CC = gcc
-CCFLAGS = -I$(COMMONDIR) -std=c99 -Wall -Wextra -Werror
+CCFLAGS = -I$(COMMONDIR) -O2 -std=c99 -Wall -Wextra -Werror
LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon
NAME = philo_one
diff --git a/philo_three/Makefile b/philo_three/Makefile
index 32b3982..17c5b46 100644
--- a/philo_three/Makefile
+++ b/philo_three/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:46 by charles ### ########.fr #
+# Updated: 2021/01/01 16:28:33 by charles ### ########.fr #
# #
# **************************************************************************** #
@@ -16,7 +16,7 @@ MAKE = make --no-print-directory
COMMON_DIR = ../common
CC = gcc
-CCFLAGS = -I$(COMMON_DIR) -Wall -Wextra -Werror
+CCFLAGS = -I$(COMMON_DIR) -O2 -std=c99 -Wall -Wextra #-Werror
LDFLAGS = -lpthread -L$(COMMON_DIR) -lphilocommon
NAME = philo_three
diff --git a/philo_three/src/child.c b/philo_three/src/child.c
index 2b17960..e79f782 100644
--- a/philo_three/src/child.c
+++ b/philo_three/src/child.c
@@ -6,7 +6,7 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/30 14:36:16 by cacharle #+# #+# */
-/* Updated: 2020/12/31 19:12:12 by charles ### ########.fr */
+/* Updated: 2021/01/01 16:31:24 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,14 +30,16 @@ pid_t child_start(t_philo *philo)
{
pthread_t thread_death;
pid_t pid;
+ long int eat_counter;
if ((pid = fork()) == -1)
return (-1);
if (pid == 0)
{
+ eat_counter = 0;
philo->forks = sem_open(PHILO_SEM_NAME, 0);
philo->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, 0);
- philo->sem_dead = sem_open(PHILO_SEM_DEAD_NAME, 0);
+ philo->sem_finish = sem_open(PHILO_SEM_FINISH_NAME, 0);
philo->time_last_eat = h_time_now();
pthread_create(&thread_death, NULL, (t_routine)routine_death, philo);
event_think(philo);
@@ -45,8 +47,15 @@ pid_t child_start(t_philo *philo)
{
event_take_fork(philo);
event_take_fork(philo);
- philo->time_last_eat = h_time_now();
event_eat(philo);
+ philo->time_last_eat = h_time_now();
+ eat_counter++;
+ if (philo->conf->meal_num != -1 && eat_counter == philo->conf->meal_num)
+ {
+ sem_wait(philo->sem_stdout);
+ sem_post(philo->sem_finish);
+ sem_post(philo->sem_stdout);
+ }
event_sleep(philo);
event_think(philo);
}
diff --git a/philo_three/src/event.c b/philo_three/src/event.c
index edeeb38..33b1e00 100644
--- a/philo_three/src/event.c
+++ b/philo_three/src/event.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2020/10/01 09:04:39 by cacharle ### ########.fr */
+/* Updated: 2021/01/01 16:29:31 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,17 +22,10 @@ void event_take_fork(t_philo *philo)
void event_eat(t_philo *philo)
{
- int eat_counter;
-
- eat_counter = 0;
- while (eat_counter < philo->conf->meal_num)
- {
- sem_wait(philo->sem_stdout);
- philo_put(philo->id, EVENT_EAT);
- sem_post(philo->sem_stdout);
- usleep(philo->conf->timeout_eat * 1000);
- eat_counter++;
- }
+ sem_wait(philo->sem_stdout);
+ philo_put(philo->id, EVENT_EAT);
+ sem_post(philo->sem_stdout);
+ usleep(philo->conf->timeout_eat * 1000);
}
void event_think(t_philo *philo)
@@ -54,7 +47,16 @@ void event_sleep(t_philo *philo)
void event_die(t_philo *philo)
{
+ long int i;
+
sem_wait(philo->sem_stdout);
philo_put(philo->id, EVENT_DIE);
- sem_post(philo->sem_dead);
+ if (philo->conf->meal_num == -1)
+ sem_post(philo->sem_finish);
+ else
+ {
+ i = -1;
+ while (++i < philo->conf->philo_num)
+ sem_post(philo->sem_finish);
+ }
}
diff --git a/philo_three/src/main.c b/philo_three/src/main.c
index d6749d8..e5e8dd2 100644
--- a/philo_three/src/main.c
+++ b/philo_three/src/main.c
@@ -6,21 +6,16 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */
-/* Updated: 2020/10/05 16:59:54 by cacharle ### ########.fr */
+/* Updated: 2021/01/01 16:30:33 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_three.h"
-static sem_t *st_sem_create(char *name, int value)
+static sem_t *st_sem_create(char *name, unsigned int value)
{
- sem_t *sem;
-
sem_unlink(name);
- sem = sem_open(name, O_CREAT | O_EXCL, 0700, value);
- if (sem == SEM_FAILED)
- return (SEM_FAILED);
- return (sem);
+ return (sem_open(name, O_CREAT | O_EXCL, 0700, value));
}
static int st_destroy(t_sems *sems, pid_t *pids, int philo_num)
@@ -35,11 +30,11 @@ static int st_destroy(t_sems *sems, pid_t *pids, int philo_num)
free(pids);
}
sem_close(sems->forks);
- sem_close(sems->sem_stdout);
- sem_close(sems->sem_dead);
sem_unlink(PHILO_SEM_NAME);
+ sem_close(sems->sem_stdout);
sem_unlink(PHILO_SEM_STDOUT_NAME);
- sem_unlink(PHILO_SEM_DEAD_NAME);
+ sem_close(sems->sem_finish);
+ sem_unlink(PHILO_SEM_FINISH_NAME);
return (1);
}
@@ -49,14 +44,14 @@ static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids)
int i;
sems->sem_stdout = SEM_FAILED;
- sems->sem_dead = SEM_FAILED;
+ sems->sem_finish = SEM_FAILED;
*pids = NULL;
if ((sems->forks =
st_sem_create(PHILO_SEM_NAME, args->philo_num)) == SEM_FAILED
|| (sems->sem_stdout =
st_sem_create(PHILO_SEM_STDOUT_NAME, 1)) == SEM_FAILED
- || (sems->sem_dead =
- st_sem_create(PHILO_SEM_DEAD_NAME, 1)) == SEM_FAILED
+ || (sems->sem_finish =
+ st_sem_create(PHILO_SEM_FINISH_NAME, args->meal_num == -1 ? 1 : args->philo_num)) == SEM_FAILED
|| (*pids = malloc(sizeof(pid_t) * args->philo_num)) == NULL)
return (st_destroy(sems, *pids, 0));
i = -1;
@@ -66,6 +61,7 @@ static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids)
philo.id = i + 1;
if (((*pids)[i] = child_start(&philo)) == -1)
return (st_destroy(sems, *pids, i));
+ usleep(200);
}
return (0);
}
@@ -75,15 +71,30 @@ int main(int argc, char **argv)
t_philo_args args;
t_sems sems;
pid_t *pids;
+ long int i;
if (!parse_args(&args, argc, argv))
return (1);
- if (args.philo_num == 0)
+ if (args.philo_num == 0 || args.meal_num == 0)
return (0);
if (st_setup(&args, &sems, &pids) != 0)
return (1);
- sem_wait(sems.sem_dead);
- sem_wait(sems.sem_dead);
+ if (args.meal_num == -1)
+ {
+ sem_wait(sems.sem_finish);
+ sem_wait(sems.sem_finish);
+ }
+ else
+ {
+ /* printf("eat %d\n", args.meal_num); */
+ i = -1;
+ while (++i < args.philo_num)
+ sem_wait(sems.sem_finish);
+ i = -1;
+ while (++i < args.philo_num)
+ sem_wait(sems.sem_finish);
+ sem_wait(sems.sem_stdout);
+ }
st_destroy(&sems, pids, args.philo_num);
return (0);
}
diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h
index bb41343..380efc5 100644
--- a/philo_three/src/philo_three.h
+++ b/philo_three/src/philo_three.h
@@ -6,13 +6,14 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/15 00:46:26 by cacharle #+# #+# */
-/* Updated: 2020/10/24 13:02:33 by charles ### ########.fr */
+/* Updated: 2021/01/01 15:34:04 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILO_THREE_H
# define PHILO_THREE_H
+# define _XOPEN_SOURCE 500
# include <unistd.h>
# include <fcntl.h>
# include <stdlib.h>
@@ -24,7 +25,7 @@
# define PHILO_SEM_NAME "semaphore_philo_three"
# define PHILO_SEM_STDOUT_NAME "semaphore_philo_three_stdout"
-# define PHILO_SEM_DEAD_NAME "semaphore_philo_three_dead"
+# define PHILO_SEM_FINISH_NAME "semaphore_philo_three_finish"
typedef struct s_philo
{
@@ -33,14 +34,14 @@ typedef struct s_philo
t_time time_last_eat;
sem_t *forks;
sem_t *sem_stdout;
- sem_t *sem_dead;
+ sem_t *sem_finish;
} t_philo;
typedef struct s_sems
{
sem_t *forks;
sem_t *sem_stdout;
- sem_t *sem_dead;
+ sem_t *sem_finish;
} t_sems;
pid_t child_start(t_philo *arg);
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]);