42 school project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.9 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:03:42 by narnaud #+# #+# */
3 years ago
/* Updated: 2022/05/20 10:34:10 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
3 years ago
#include "philo.h"
3 years ago
3 years ago
void *check_halt(void *p)
3 years ago
{
3 years ago
int time;
3 years ago
t_philo *philo;
3 years ago
philo = p;
3 years ago
while (1)
{
usleep(1000);
time = gettime();
3 years ago
if (time - philo->last_meal_time > philo->table->param[DIE_TIME])
3 years ago
{
3 years ago
printf("%i %d died\n", time, philo->id);
3 years ago
exit(EXIT_SUCCESS);
}
if (philo->meals_done == philo->table->param[MEALS_AMOUNT])
exit(EXIT_SUCCESS);
3 years ago
}
}
void philo_life(t_philo *philo)
{
t_table *table;
table = philo->table;
while (1)
{
sem_wait(table->sptr);
3 years ago
printf("%i %d has taken a fork\n", gettime(), philo->id);
3 years ago
sem_wait(table->sptr);
3 years ago
printf("%i %d has taken a fork\n", gettime(), philo->id);
philo->last_meal_time = gettime();
printf("%i %d is eating\n", gettime(), philo->id);
usleep(table->param[EAT_DURATION] * 1000);
philo->meals_done++;
3 years ago
sem_post(table->sptr);
sem_post(table->sptr);
3 years ago
printf("%i %d is sleeping\n", gettime(), philo->id);
usleep(table->param[SLEEP_DURATION] * 1000);
printf("%i %d is thinking\n", gettime(), philo->id);
3 years ago
}
}
void init_philos(t_table *table)
{
3 years ago
int i;
3 years ago
pthread_t checks;
3 years ago
int amount;
3 years ago
3 years ago
amount = table->param[PHILO_AMOUNT];
table->pid = malloc(sizeof(pid_t) * amount);
3 years ago
i = 0;
3 years ago
while (i < amount)
3 years ago
{
table->pid[i] = fork();
table->philos[i].table = table;
table->philos[i].id = i;
table->philos[i].meals_done = 0;
3 years ago
if (!table->pid[i])
{
pthread_create(&checks, NULL, check_halt, table->philos + i);
philo_life(table->philos + i);
}
usleep(500);
i++;
}
}
3 years ago
int main(int argc, char **argv)
3 years ago
{
static t_table *table;
3 years ago
static int i = 0;
static int status;
3 years ago
sem_unlink("/chopsticks");
sem_unlink("/death");
if (argc < 5 || argc > 6)
return (1);
table = parsing(argv + 1);
3 years ago
table->sptr = sem_open("/chopsticks", O_CREAT, 0664,
table->param[PHILO_AMOUNT]);
3 years ago
table->death = sem_open("/death", O_CREAT, 0664, 1);
init_philos(table);
waitpid(-1, &status, 0);
3 years ago
while (i < table->param[PHILO_AMOUNT])
3 years ago
{
kill(table->pid[i], SIGINT);
i++;
}
sem_unlink("/chopsticks");
sem_unlink("/deaths");
3 years ago
return (0);
3 years ago
}