diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-01-09 14:40:23 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-01-09 14:40:23 +0100 |
| commit | ebbd81e4312d945b359d2761acfeb613da1f98c8 (patch) | |
| tree | 5ac8f109d8f840e57f001b4739a1b2472a0a2df2 | |
| parent | a69a877b3a2758aafe3de0db87a56063b28ed00f (diff) | |
| download | philosophers-ebbd81e4312d945b359d2761acfeb613da1f98c8.tar.gz philosophers-ebbd81e4312d945b359d2761acfeb613da1f98c8.tar.bz2 philosophers-ebbd81e4312d945b359d2761acfeb613da1f98c8.zip | |
Added antidead lock in philo_one
| -rw-r--r-- | README.md | 21 | ||||
| -rwxr-xr-x | clean_philo_three | 3 | ||||
| -rw-r--r-- | philo_one/src/event.c | 11 | ||||
| -rw-r--r-- | philo_one/src/main.c | 4 | ||||
| -rw-r--r-- | philo_one/src/philo.c | 22 | ||||
| -rw-r--r-- | philo_one/src/philo_one.h | 4 | ||||
| -rw-r--r-- | philo_one/src/routine.c | 22 |
7 files changed, 44 insertions, 43 deletions
@@ -1,3 +1,22 @@ # philosophers -philosophers project of school 42 +philosophers project of school 42. + +Learning about threads, process, mutex and semaphore. + +## Usage + +```command +$ make philo_one +$ ./philo_one/philo_one 4 410 200 200 +$ make philo_two +$ ./philo_two/philo_two 4 410 200 200 +$ make philo_three +$ ./philo_three/philo_three 4 410 200 200 +``` + +### Kill zombie process after Ctrl-C on philo_three + +```command +$ ./clean_philo_three +``` diff --git a/clean_philo_three b/clean_philo_three new file mode 100755 index 0000000..64e53bc --- /dev/null +++ b/clean_philo_three @@ -0,0 +1,3 @@ +#!/bin/sh + +ps aux | grep philo_three | tr -s ' ' | cut -d ' ' -f 2 | xargs kill -9 diff --git a/philo_one/src/event.c b/philo_one/src/event.c index df621b4..92ca399 100644 --- a/philo_one/src/event.c +++ b/philo_one/src/event.c @@ -6,25 +6,21 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/08 18:27:33 by charles ### ########.fr */ +/* Updated: 2021/01/09 13:12:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -void event_take_fork(t_philo *arg) +void event_take_fork(t_philo *arg, pthread_mutex_t *mutex_fork) { if (philo_finished(arg->conf)) return ; - pthread_mutex_lock(arg->fork_left); - pthread_mutex_lock(arg->fork_right); + pthread_mutex_lock(mutex_fork); pthread_mutex_lock(&arg->conf->mutex_stdout); if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); - if (philo_finished(arg->conf)) - return ; - philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); pthread_mutex_unlock(&arg->conf->mutex_stdout); } @@ -39,6 +35,7 @@ void event_eat(t_philo *arg) pthread_mutex_unlock(&arg->conf->mutex_stdout); arg->time_last_eat = h_time_now(); h_sleep(arg->conf->timeout_eat); + arg->time_last_eat = h_time_now(); } void event_think(t_philo *arg) diff --git a/philo_one/src/main.c b/philo_one/src/main.c index b2dc5ed..d02fb7a 100644 --- a/philo_one/src/main.c +++ b/philo_one/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2021/01/08 15:20:18 by charles ### ########.fr */ +/* Updated: 2021/01/09 14:26:48 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,7 +76,7 @@ int main(int argc, char **argv) return (1); } while (!philo_finished(&conf)) - ; + usleep(500); philo_put_flush(); philos_detach(philos, conf.philo_num); forks_destroy(forks, conf.philo_num); diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index 1e98fa0..911f6b9 100644 --- a/philo_one/src/philo.c +++ b/philo_one/src/philo.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2021/01/08 18:56:10 by charles ### ########.fr */ +/* Updated: 2021/01/09 14:39:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,8 +41,6 @@ bool philos_start(t_philo *philos, long int num) i = -1; while (++i < num) { - if (i % 2 == 0) - continue; if (pthread_create( &philos[i].thread, NULL, @@ -60,23 +58,6 @@ bool philos_start(t_philo *philos, long int num) if (i % 2 == 0) continue; pthread_mutex_unlock(&philos[i].mutex_start); - /* usleep(200); */ - } - i = -1; - while (++i < num) - { - if (i % 2 == 1) - continue; - if (pthread_create( - &philos[i].thread, - NULL, - (t_routine)routine_philo, - (void*)(philos + i)) != 0) - { - while (--i >= 0) - pthread_detach(philos[i].thread); - return (false); - } } usleep(1000); i = -1; @@ -85,7 +66,6 @@ bool philos_start(t_philo *philos, long int num) if (i % 2 == 1) continue; pthread_mutex_unlock(&philos[i].mutex_start); - /* usleep(200); */ } return (true); diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index 2ebe708..910dfe8 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2021/01/04 10:38:44 by cacharle ### ########.fr */ +/* Updated: 2021/01/09 14:26:58 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,7 +82,7 @@ void *routine_death(t_philo *arg); ** io.c */ -void event_take_fork(t_philo *arg); +void event_take_fork(t_philo *arg, pthread_mutex_t *mutex_fork); void event_eat(t_philo *arg); void event_think(t_philo *arg); void event_sleep( diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index 05304fe..340f3b7 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2021/01/08 18:52:48 by charles ### ########.fr */ +/* Updated: 2021/01/09 14:32:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,20 +39,22 @@ void *routine_philo(t_philo *arg) pthread_mutex_lock(&arg->mutex_start); if (philo_finished(arg->conf)) return (NULL); - /* if (arg->conf->philo_num % 2 == 0 && arg->id % 2 == 0) */ - /* usleep(1000); */ - /* if (arg->conf->philo_num % 2 == 1 && arg->id % 3 == 0) */ - /* usleep(1000); */ - /* if (arg->conf->philo_num % 2 == 1 && arg->id % 3 == 1) */ - /* usleep(2000); */ arg->time_last_eat = h_time_now(); if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); arg->time_last_eat = h_time_now(); while (!philo_finished(arg->conf)) { - event_take_fork(arg); - /* arg->time_last_eat = h_time_now(); */ + if (arg->id % 2 == 0) + { + event_take_fork(arg, arg->fork_left); + event_take_fork(arg, arg->fork_right); + } + else + { + event_take_fork(arg, arg->fork_right); + event_take_fork(arg, arg->fork_left); + } event_eat(arg); eat_counter++; st_check_meal_num_finished(arg, eat_counter); @@ -67,7 +69,7 @@ void *routine_death(t_philo *arg) { while (!philo_finished(arg->conf) && h_time_now() - arg->time_last_eat < arg->conf->timeout_death) - usleep(500); + usleep(1000); event_die(arg); return (NULL); } |
