This commit is contained in:
kinou-p 2022-02-28 22:21:05 +01:00
parent 4db31b05e9
commit 641e225ded

59
main.c
View File

@ -33,17 +33,18 @@ s_arg *create_data(int argc, char **argv)
void put_event(s_philo philo, char *event) void put_event(s_philo philo, char *event)
{ {
printf("%ldms Philo N°%d %s\n", get_time() - philo.data->time_start, philo.philo_id, event); pthread_mutex_lock(philo.display);
if (*philo.data->death)
printf("%ldms Philo N°%d %s\n", get_time() - philo.data->time_start, philo.philo_id, event);
pthread_mutex_unlock(philo.display);
} }
void philo_take_fork(s_philo *philo_tmp) void philo_take_fork(s_philo philo)
{ {
pthread_mutex_lock(philo.right_fork);
} put_event(philo, "has taken a fork");
pthread_mutex_lock(philo.left_fork);
void philo_sleep_think(s_philo *philo_tmp) put_event(philo, "has taken a fork");
{
} }
void philo_eat(s_philo *philo_tmp) void philo_eat(s_philo *philo_tmp)
@ -51,26 +52,15 @@ void philo_eat(s_philo *philo_tmp)
s_philo philo; s_philo philo;
philo = *philo_tmp; philo = *philo_tmp;
pthread_mutex_lock(philo.right_fork); philo_take_fork(philo);
pthread_mutex_lock(philo.left_fork);
pthread_mutex_lock(philo.display);
put_event(philo, "has taken a fork");
put_event(philo, "has taken a fork");
pthread_mutex_unlock(philo.display);
pthread_mutex_lock(philo.display);
*philo_tmp->last_eat = get_time(); *philo_tmp->last_eat = get_time();
put_event(philo, "is eating"); put_event(philo, "is eating");
pthread_mutex_unlock(philo.display);
usleep(philo.data->time_to_eat * 1000); usleep(philo.data->time_to_eat * 1000);
pthread_mutex_unlock(philo.right_fork);
pthread_mutex_unlock(philo.left_fork); pthread_mutex_unlock(philo.left_fork);
pthread_mutex_lock(philo.display); pthread_mutex_unlock(philo.right_fork);
put_event(philo, "is sleeping"); put_event(philo, "is sleeping");
pthread_mutex_unlock(philo.display);
usleep(philo.data->time_to_sleep * 1000); usleep(philo.data->time_to_sleep * 1000);
pthread_mutex_lock(philo.display);
put_event(philo, "is thinking"); put_event(philo, "is thinking");
pthread_mutex_unlock(philo.display);
} }
void *death_check(void *tmp) void *death_check(void *tmp)
@ -82,9 +72,9 @@ void *death_check(void *tmp)
{ {
if (get_time() - *philo->last_eat > philo->data->time_to_die) if (get_time() - *philo->last_eat > philo->data->time_to_die)
{ {
//printf("PHILO IS DEAD last eat: %ld-- ", *philo->last_eat - philo->data->time_start); *philo->data->death = 0;
pthread_mutex_lock(philo->display); pthread_mutex_lock(philo->display);
put_event(*philo, "died"); printf("%ldms Philo N°%d is died\n", get_time() - philo->data->time_start, philo->philo_id);
exit(1); exit(1);
} }
} }
@ -98,6 +88,9 @@ void *routine(void *tmp)
philo = (s_philo *)tmp; philo = (s_philo *)tmp;
if (philo->philo_id == 5)
usleep(1530494976);
//usleep(100000);
*philo->last_eat = get_time(); *philo->last_eat = get_time();
pthread_create(&death, 0, &death_check, philo); pthread_create(&death, 0, &death_check, philo);
pthread_detach(death); pthread_detach(death);
@ -136,32 +129,33 @@ s_philo *create_philo(s_arg *data)
i++; i++;
} }
i = 0; i = 0;
while (i < data->nb_philo - 1) while (i < data->nb_philo)
{ {
philo[i].right_fork = philo[i + 1].left_fork; philo[i].right_fork = philo[(i + 1) % data->nb_philo].left_fork;
i++; i++;
} }
philo[i].left_fork = philo[0].right_fork; //philo[i].right_fork = philo[0].left_fork;
return (philo); return (philo);
} }
int start_philo(s_philo *philo) int start_philo(s_philo *philo, s_arg *data)
{ {
int i; int i;
i = 0; i = 0;
philo->data->time_start = get_time(); data->time_start = get_time();
while (i < philo[i].data->nb_philo - 1) while (i < data->nb_philo)
{ {
pthread_create(&philo[i].thread, 0, &routine, &philo[i]); pthread_create(&philo[i].thread, 0, &routine, &philo[i]);
usleep(10); usleep(10);
i++; i++;
} }
i = 0; i = 0;
while (i < philo[i].data->nb_philo - 1) while (i < data->nb_philo)
{ {
pthread_join(philo[i].thread, 0); pthread_join(philo[i++].thread, 0);
} }
return (0);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -169,10 +163,9 @@ int main(int argc, char **argv)
s_arg *data; s_arg *data;
s_philo *philo; s_philo *philo;
get_time();
data = create_data(argc, argv); data = create_data(argc, argv);
philo = create_philo(data); philo = create_philo(data);
start_philo(philo); start_philo(philo, data );
} }