debug messages about forks and ids
This commit is contained in:
parent
3de1ac17b3
commit
c4ec3b4000
BIN
philo/philo
BIN
philo/philo
Binary file not shown.
@ -11,7 +11,7 @@ int create_philos(t_rules *rules)
|
|||||||
return(FAILURE);
|
return(FAILURE);
|
||||||
while (i < rules->n_philos)
|
while (i < rules->n_philos)
|
||||||
{
|
{
|
||||||
philo = &rules->philos[i];
|
philo = &(rules->philos[i]);
|
||||||
philo->id = i + 1;
|
philo->id = i + 1;
|
||||||
philo->l_fork = &rules->forks[i];
|
philo->l_fork = &rules->forks[i];
|
||||||
philo->r_fork = &rules->forks[i + 1 % rules->n_philos];
|
philo->r_fork = &rules->forks[i + 1 % rules->n_philos];
|
||||||
|
|||||||
@ -17,7 +17,7 @@ int get_finished(t_rules *rules)
|
|||||||
}
|
}
|
||||||
void philo_die(t_philo *philo)
|
void philo_die(t_philo *philo)
|
||||||
{
|
{
|
||||||
print_status(philo, "died 💀");
|
print_status(philo, "died");
|
||||||
philo->death = 1;
|
philo->death = 1;
|
||||||
set_finished(philo->rules);
|
set_finished(philo->rules);
|
||||||
pthread_mutex_unlock(philo->l_fork);
|
pthread_mutex_unlock(philo->l_fork);
|
||||||
@ -40,18 +40,18 @@ int philo_eat(t_philo *philo)
|
|||||||
if (philo->id % 2 == 1)
|
if (philo->id % 2 == 1)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(philo->r_fork);
|
pthread_mutex_lock(philo->r_fork);
|
||||||
print_status(philo, "has taken a fork 🍴");
|
print_status(philo, "has taken a fork");
|
||||||
}
|
}
|
||||||
pthread_mutex_lock(philo->l_fork);
|
pthread_mutex_lock(philo->l_fork);
|
||||||
print_status(philo, "has taken a fork 🍴");
|
print_status(philo, "has taken a fork");
|
||||||
if (philo->id % 2 == 0)
|
if (philo->id % 2 == 0)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(philo->r_fork);
|
pthread_mutex_lock(philo->r_fork);
|
||||||
print_status(philo, "has taken a fork 🍴");
|
print_status(philo, "has taken a fork");
|
||||||
}
|
}
|
||||||
if (check_death(philo))
|
if (check_death(philo))
|
||||||
return (philo_die(philo), FAILURE);
|
return (philo_die(philo), FAILURE);
|
||||||
print_status(philo, "is eating 🍝");
|
print_status(philo, "is eating");
|
||||||
philo->last_meal = get_time();
|
philo->last_meal = get_time();
|
||||||
philo->n_eat++;
|
philo->n_eat++;
|
||||||
return (SUCCESS);
|
return (SUCCESS);
|
||||||
@ -77,10 +77,10 @@ void *philo_routine(void *arg)
|
|||||||
return (philo_die(philo), NULL);
|
return (philo_die(philo), NULL);
|
||||||
pthread_mutex_unlock(philo->l_fork);
|
pthread_mutex_unlock(philo->l_fork);
|
||||||
pthread_mutex_unlock(philo->r_fork);
|
pthread_mutex_unlock(philo->r_fork);
|
||||||
print_status(philo, "is sleeping 💤");
|
print_status(philo, "is sleeping");
|
||||||
if (philo_died_while_sleeping(philo, philo->rules->time_to_sleep))
|
if (philo_died_while_sleeping(philo, philo->rules->time_to_sleep))
|
||||||
return (philo_die(philo), NULL);
|
return (philo_die(philo), NULL);
|
||||||
print_status(philo, "is thinking 💭");
|
print_status(philo, "is thinking");
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
@ -2,9 +2,30 @@
|
|||||||
|
|
||||||
void print_rules(t_rules *rules)
|
void print_rules(t_rules *rules)
|
||||||
{
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
printf("n_philos: %d\n", rules->n_philos);
|
printf("n_philos: %d\n", rules->n_philos);
|
||||||
printf("time_to_die: %d\n", rules->time_to_die);
|
printf("time_to_die: %d\n", rules->time_to_die);
|
||||||
printf("time_to_eat: %d\n", rules->time_to_eat);
|
printf("time_to_eat: %d\n", rules->time_to_eat);
|
||||||
printf("time_to_sleep: %d\n", rules->time_to_sleep);
|
printf("time_to_sleep: %d\n", rules->time_to_sleep);
|
||||||
printf("n_must_eat: %d\n", rules->n_must_eat);
|
printf("n_must_eat: %d\n", rules->n_must_eat);
|
||||||
|
i = 0;
|
||||||
|
while (i < rules->n_philos)
|
||||||
|
{
|
||||||
|
printf("philo %d has id: %d\n", i, rules->philos[i].id);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
while (i < rules->n_philos)
|
||||||
|
{
|
||||||
|
printf("fork %d has pointer:%p\n", i, &rules->forks[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
while (i < rules->n_philos)
|
||||||
|
{
|
||||||
|
printf("philo %d\n has l_fork %p and r_fork: %p\n", rules->philos[i].id, rules->philos[i].l_fork, rules->philos[i].r_fork);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user