diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-02-10 00:34:47 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-02-10 01:30:53 +0100 |
| commit | 1a7c6c5a0ed9a5aae1fe45c3af335c120c2dc642 (patch) | |
| tree | 965aa21be5411d2a9e1156013efc1980bca29f90 /philo_one/routine.c | |
| parent | dfbafce6506fe5c47b0e60289a9d4629e502c9ac (diff) | |
| download | philosophers-1a7c6c5a0ed9a5aae1fe45c3af335c120c2dc642.tar.gz philosophers-1a7c6c5a0ed9a5aae1fe45c3af335c120c2dc642.tar.bz2 philosophers-1a7c6c5a0ed9a5aae1fe45c3af335c120c2dc642.zip | |
WIP: philo one
Diffstat (limited to 'philo_one/routine.c')
| -rw-r--r-- | philo_one/routine.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/philo_one/routine.c b/philo_one/routine.c new file mode 100644 index 0000000..48f3c0d --- /dev/null +++ b/philo_one/routine.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* routine.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ +/* Updated: 2020/02/10 01:19:13 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo_one.h" + +void *routine_philo(t_routine_arg *routine_arg) +{ + pthread_t thread_death; + + if (pthread_create(&thread_death, NULL, routine_death, routine_arg) != 0) + return (NULL); + philo_think(routine_arg->philo->id); + while (routine_arg->philo->alive && !philo_starved(routine_arg->philo)) + { + if (!routine_arg->fork_left->used && !routine_arg->fork_right->used) + { + pthread_mutex_lock(&routine_arg->fork_left->mutex); + pthread_mutex_lock(&routine_arg->fork_right->mutex); + philo_eat(routine_arg->philo->id, routine_arg->args->timeout_eat); + pthread_mutex_unlock(&routine_arg->fork_left->mutex); + pthread_mutex_unlock(&routine_arg->fork_right->mutex); + philo_sleep(routine_arg->philo->id, routine_arg->args->timeout_sleep); + philo_think(routine_arg->philo->id); + } + } + if (routine_arg->philo->alive) + philo_die(routine_arg->philo->id); + free(routine_arg); + return (NULL); +} + +void *routine_death(t_routine_arg *arg) +{ + struct timeval tv; + + if (gettimeofday(&tv, NULL) == -1) + return (NULL); + while (arg->philo->alive && + arg->philo->time_last_eat - tv.asd > arg->args->timeout_death) + if (gettimeofday(&tv, NULL) == -1) + return (NULL); + arg->philo->alive = FALSE; + return (NULL); +} |
