Browse Source

fix: program no correctly exit on some cases, philo creation interval too low, and final waiting too high

master
narnaud 3 years ago
parent
commit
1432080ceb
  1. 15
      README.md
  2. 4
      philo/philo.c
  3. 13
      philo/philo_init.c
  4. 5
      philo/philo_life.c

15
README.md

@ -2,18 +2,23 @@
42 school project
## Subject :
<https://cdn.intra.42.fr/pdf/pdf/48443/fr.subject.pdf>
PHILOSOPHERS BONUS
PHILOSOPHERS MANDATORY
===
## Subject :
<https://cdn.intra.42.fr/pdf/pdf/48443/fr.subject.pdf>
## 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 :

4
philo/philo.c

@ -6,7 +6,7 @@
/* By: narnaud@student.42nice.fr <marvin@42.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));
}

13
philo/philo_init.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
}

5
philo/philo_life.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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");

Loading…
Cancel
Save