69 lines
1.4 KiB
C
69 lines
1.4 KiB
C
#include "philo.h"
|
|
|
|
int take_forks(t_philo *philo)
|
|
{
|
|
// pthread_mutex_lock(&mutex);
|
|
while (philo->fork == 0 || philo->next->fork == 0)
|
|
continue ;
|
|
if (philo->fork == 1 && philo->next->fork == 1)
|
|
{
|
|
philo->fork = 0;
|
|
philo->next->fork = 0;
|
|
ft_printf("%i has taken a fork\n", philo->philo_nbr);
|
|
return (1);
|
|
}
|
|
// pthread_mutex_unlock(&mutex);
|
|
return (0);
|
|
}
|
|
|
|
void *philo_exec(void *arg)
|
|
{
|
|
t_philo *philo;
|
|
int nbr;
|
|
|
|
philo = (t_philo*)arg;
|
|
nbr = philo->philo_nbr;
|
|
|
|
pthread_mutex_lock(philo->m_print);
|
|
ft_printf("%i is thinking\n", philo->philo_nbr);
|
|
pthread_mutex_unlock(philo->m_print);
|
|
|
|
// eat
|
|
// "has taken a fork"
|
|
// "is eating"
|
|
// sleep
|
|
// "is sleeping"
|
|
// think
|
|
// "is thinking"
|
|
// die
|
|
// "died"
|
|
//int i = 0;
|
|
// while (i < 2)
|
|
// {
|
|
// if (!take_forks(philo))
|
|
// continue ;
|
|
//
|
|
// usleep(philo->params->t_eat);
|
|
//
|
|
// pthread_mutex_lock(&mutex);
|
|
// philo->fork = 1;
|
|
// philo->next->fork = 1;
|
|
// ft_printf("%i is sleeping\n", philo->philo_nbr);
|
|
// pthread_mutex_unlock(&mutex);
|
|
//
|
|
// usleep(philo->params->t_slp);
|
|
//
|
|
// pthread_mutex_lock(&mutex);
|
|
// ft_printf("%i is thinking\n", philo->philo_nbr);
|
|
// pthread_mutex_unlock(&mutex);
|
|
|
|
// pthread_mutex_lock(philo->mutex);
|
|
// pthread_mutex_lock(&mutex);
|
|
// ft_printf("%i is thinking\n", philo->philo_nbr);
|
|
// pthread_mutex_unlock(philo->mutex);
|
|
// pthread_mutex_unlock(&mutex);
|
|
// i++;
|
|
// }
|
|
return (NULL);
|
|
}
|