aboutsummaryrefslogtreecommitdiff
path: root/philo_two/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'philo_two/src/main.c')
-rw-r--r--philo_two/src/main.c26
1 files changed, 14 insertions, 12 deletions
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]);