diff --git a/README.md b/README.md index 9c69dc7..a7e2962 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,23 @@ 42 school project +## Subject : + - -PHILOSOPHERS BONUS +PHILOSOPHERS MANDATORY === -## Subject : - +## Docs : +[Threads](https://franckh.developpez.com/tutoriels/posix/pthreads/) + +## Notes + +PHILOSOPHERS BONUS +=== ## Docs : [Semaphores](https://sites.uclouvain.be/SyllabusC/notes/Theorie/Threads/coordination.html) - ## Structure : Main : diff --git a/philo/philo.c b/philo/philo.c index ef1f173..623b29b 100644 --- a/philo/philo.c +++ b/philo/philo.c @@ -6,7 +6,7 @@ /* By: narnaud@student.42nice.fr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/19 00:36:57 by narnaud@stude #+# #+# */ -/* Updated: 2022/05/19 20:01:36 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/20 08:39:34 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ int main(int argc, char **argv) pthread_mutex_lock(room->lock); room->running = 0; pthread_mutex_unlock(room->lock); - sleep(1); + usleep(25000); return (clean_memory(room, philos)); } diff --git a/philo/philo_init.c b/philo/philo_init.c index 01c6c55..8fa3f8f 100644 --- a/philo/philo_init.c +++ b/philo/philo_init.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/24 15:44:04 by narnaud #+# #+# */ -/* Updated: 2022/05/19 22:24:51 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/20 08:38:02 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,12 +37,15 @@ t_philo *create_philos(t_room *room) { t_philo *ret; static int i; + int amount; - ret = (t_philo *)malloc((room->param[PHILO_AMOUNT] + 1) * sizeof(t_philo)); - while (i < room->param[PHILO_AMOUNT]) + amount = room->param[PHILO_AMOUNT]; + ret = (t_philo *)malloc((amount + 1) * sizeof(t_philo)); + while (i < amount) { ret[i].fork = &room->forks[i]; - ret[i].fork_r = &room->forks[(i + 1) % room->param[PHILO_AMOUNT]]; + if (amount > 1) + ret[i].fork_r = &room->forks[(i + 1) % amount]; ret[i].lock = malloc(sizeof(pthread_mutex_t)); ret[i].thd = malloc(sizeof(pthread_t)); ret[i].room = room; @@ -77,6 +80,6 @@ void invit_philos(t_philo *philos) else if ((i % 2) == 1) i -= 2; philo = philos + i; - usleep(10); + usleep(50); } } diff --git a/philo/philo_life.c b/philo/philo_life.c index 404996a..5f5d763 100644 --- a/philo/philo_life.c +++ b/philo/philo_life.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/26 07:43:48 by narnaud #+# #+# */ -/* Updated: 2022/05/19 01:23:29 by narnaud ### ########.fr */ +/* Updated: 2022/05/20 08:35:19 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,8 @@ static int eat(t_room *room, t_philo *philo) { pthread_mutex_lock(philo->fork); - if (!safe_print(philo, "%d %d has taken a fork\n")) + if (!safe_print(philo, "%d %d has taken a fork\n") + || !philo->fork_r) return (1); pthread_mutex_lock(philo->fork_r); safe_print(philo, "%d %d has taken a fork\n");