aboutsummaryrefslogtreecommitdiff
path: root/philo_three/src
diff options
context:
space:
mode:
Diffstat (limited to 'philo_three/src')
-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
4 files changed, 60 insertions, 37 deletions
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);