From f57ec76fd1be738d7b9d82c1f7548883efa15d0c Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 30 Sep 2020 14:48:12 +0200 Subject: Added philo_three draft --- philo_three/src/child.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 philo_three/src/child.c (limited to 'philo_three/src/child.c') diff --git a/philo_three/src/child.c b/philo_three/src/child.c new file mode 100644 index 0000000..f0e7ebd --- /dev/null +++ b/philo_three/src/child.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* child.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/30 14:36:16 by cacharle #+# #+# */ +/* Updated: 2020/09/30 14:39:58 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo_three.h" + +void *routine_death(t_philo_args *arg) +{ + t_time current; + + current = h_time_now(); + while (current - arg->time_last_eat < arg->conf->timeout_death) + current = h_time_now(); + if (!arg->conf->all_alive) + return (NULL); + event_die(arg); + return (NULL); +} + +pid_t child_start(t_philo_args *arg) +{ + pid_t pid; + sem_t *forks; + sem_t *sem_stdout; + sem_t *sem_alive; + + pid = fork(); + if (pid == -1) + return (-1); + if (pid == 0) + { + forks = sem_open(PHILO_SEM_NAME, 0); + sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, 0); + pthread_create(&death_thread, NULL, (t_routine)routine_death, &args); + event_think(); + while (true) + { + event_eat(); + event_sleep(); + event_think(); + } + exit(0); + } + return pid; +} -- cgit