aboutsummaryrefslogtreecommitdiff
path: root/philo_three/src/child.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-10 14:26:22 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-10 14:26:22 +0100
commit051e5fa4382a629b76f32e46d3e766ed2eb306fc (patch)
tree1ccd0625181cdc58cf6499ece8c2a48e1342d303 /philo_three/src/child.c
parentd552a2af95a3e03f937a94945369536d1eaae74a (diff)
downloadphilosophers-051e5fa4382a629b76f32e46d3e766ed2eb306fc.tar.gz
philosophers-051e5fa4382a629b76f32e46d3e766ed2eb306fc.tar.bz2
philosophers-051e5fa4382a629b76f32e46d3e766ed2eb306fc.zip
Fixing philo_three destructor, Norming philo_three
Diffstat (limited to 'philo_three/src/child.c')
-rw-r--r--philo_three/src/child.c44
1 files changed, 28 insertions, 16 deletions
diff --git a/philo_three/src/child.c b/philo_three/src/child.c
index 5516151..661e0a6 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: 2021/01/10 12:06:40 by cacharle ### ########.fr */
+/* Updated: 2021/01/10 14:24:05 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,11 +26,12 @@ static void *st_routine_death(t_philo *philo)
return (NULL);
}
-void st_child_loop(t_philo *philo)
+static void st_child_loop(t_philo *philo)
{
long int eat_counter;
eat_counter = 0;
+ philo->time_last_eat = h_time_now();
while (true)
{
sem_wait(philo->sem_grab);
@@ -52,31 +53,42 @@ void st_child_loop(t_philo *philo)
#define PHILO_SEM_EAT_PREFIX "semaphore_philo_three_eat_"
-pid_t child_start(t_philo *philo)
+static bool st_child_setup(t_philo *philo)
+{
+ if (!h_sem_create(PHILO_SEM_FINISH_NAME, 0, &philo->sem_finish) ||
+ !h_sem_create(philo_sem_eat_name(PHILO_SEM_EAT_PREFIX, philo->id),
+ 1, &philo->sem_eat) ||
+ !h_sem_create(PHILO_SEM_NAME, 0, &philo->forks) ||
+ !h_sem_create(PHILO_SEM_STDOUT_NAME, 0, &philo->sem_stdout) ||
+ !h_sem_create(PHILO_SEM_MEAL_NUM_NAME, 0, &philo->sem_meal_num) ||
+ !h_sem_create(PHILO_SEM_START_NAME, 0, &philo->sem_start) ||
+ !h_sem_create(PHILO_SEM_GRAB_NAME, 0, &philo->sem_grab))
+ return (false);
+ return (true);
+}
+
+pid_t child_start(t_philo *philo)
{
pthread_t thread_death;
pid_t pid;
- const char *sem_eat_name;
if ((pid = fork()) == -1)
return (-1);
if (pid == 0)
{
- philo->forks = sem_open(PHILO_SEM_NAME, 0);
- philo->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, 0);
- philo->sem_finish = sem_open(PHILO_SEM_FINISH_NAME, 0);
- philo->sem_meal_num = sem_open(PHILO_SEM_MEAL_NUM_NAME, 0);
- philo->sem_start = sem_open(PHILO_SEM_START_NAME, 0);
- philo->sem_grab = sem_open(PHILO_SEM_GRAB_NAME, 0);
- sem_eat_name = philo_sem_eat_name(PHILO_SEM_EAT_PREFIX, philo->id);
- sem_unlink(sem_eat_name);
- philo->sem_eat = sem_open(sem_eat_name, O_CREAT | O_EXCL, 0700, 1);
+ philo->sem_finish = SEM_FAILED;
+ if (!st_child_setup(philo))
+ {
+ if (philo->sem_finish != SEM_FAILED)
+ sem_post(philo->sem_finish);
+ exit(1);
+ }
sem_wait(philo->sem_start);
philo->time_last_eat = h_time_now();
- if (pthread_create(&thread_death, NULL, (t_routine)st_routine_death, philo) != 0)
- exit(1);
+ if (pthread_create(&thread_death, NULL,
+ (t_routine)st_routine_death, philo) != 0)
+ exit(sem_post(philo->sem_finish));
pthread_detach(thread_death);
- philo->time_last_eat = h_time_now();
st_child_loop(philo);
exit(0);
}