|
|
@ -6,7 +6,7 @@ |
|
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|
|
|
/* +#+#+#+#+#+ +#+ */ |
|
|
|
/* Created: 2022/05/16 14:03:42 by narnaud #+# #+# */ |
|
|
|
/* Updated: 2022/05/20 10:34:10 by narnaud ### ########.fr */ |
|
|
|
/* Updated: 2022/05/20 11:58:25 by narnaud ### ########.fr */ |
|
|
|
/* */ |
|
|
|
/* ************************************************************************** */ |
|
|
|
|
|
|
@ -24,10 +24,11 @@ void *check_halt(void *p) |
|
|
|
time = gettime(); |
|
|
|
if (time - philo->last_meal_time > philo->table->param[DIE_TIME]) |
|
|
|
{ |
|
|
|
printf("%i %d died\n", time, philo->id); |
|
|
|
safe_print(philo, "%i %d died\n"); |
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
} |
|
|
|
if (philo->meals_done == philo->table->param[MEALS_AMOUNT]) |
|
|
|
if (philo->meals_done == philo->table->param[MEALS_AMOUNT] |
|
|
|
&& philo->id == philo->table->param[PHILO_AMOUNT] - 1) |
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
} |
|
|
|
} |
|
|
@ -40,18 +41,18 @@ void philo_life(t_philo *philo) |
|
|
|
while (1) |
|
|
|
{ |
|
|
|
sem_wait(table->sptr); |
|
|
|
printf("%i %d has taken a fork\n", gettime(), philo->id); |
|
|
|
safe_print(philo, "%i %d has taken a fork\n"); |
|
|
|
sem_wait(table->sptr); |
|
|
|
printf("%i %d has taken a fork\n", gettime(), philo->id); |
|
|
|
safe_print(philo, "%i %d has taken a fork\n"); |
|
|
|
philo->last_meal_time = gettime(); |
|
|
|
printf("%i %d is eating\n", gettime(), philo->id); |
|
|
|
safe_print(philo, "%i %d is eating\n"); |
|
|
|
usleep(table->param[EAT_DURATION] * 1000); |
|
|
|
philo->meals_done++; |
|
|
|
sem_post(table->sptr); |
|
|
|
sem_post(table->sptr); |
|
|
|
printf("%i %d is sleeping\n", gettime(), philo->id); |
|
|
|
safe_print(philo, "%i %d is sleeping\n"); |
|
|
|
usleep(table->param[SLEEP_DURATION] * 1000); |
|
|
|
printf("%i %d is thinking\n", gettime(), philo->id); |
|
|
|
safe_print(philo, "%i %d is thinking\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -91,9 +92,10 @@ int main(int argc, char **argv) |
|
|
|
if (argc < 5 || argc > 6) |
|
|
|
return (1); |
|
|
|
table = parsing(argv + 1); |
|
|
|
table->talk = sem_open("/talk", O_CREAT, 0664, |
|
|
|
1); |
|
|
|
table->sptr = sem_open("/chopsticks", O_CREAT, 0664, |
|
|
|
table->param[PHILO_AMOUNT]); |
|
|
|
table->death = sem_open("/death", O_CREAT, 0664, 1); |
|
|
|
init_philos(table); |
|
|
|
waitpid(-1, &status, 0); |
|
|
|
while (i < table->param[PHILO_AMOUNT]) |
|
|
@ -102,6 +104,6 @@ int main(int argc, char **argv) |
|
|
|
i++; |
|
|
|
} |
|
|
|
sem_unlink("/chopsticks"); |
|
|
|
sem_unlink("/deaths"); |
|
|
|
sem_unlink("/talk"); |
|
|
|
return (0); |
|
|
|
} |
|
|
|