first
This commit is contained in:
parent
eee4d08902
commit
4db31b05e9
37
main.c
37
main.c
@ -33,7 +33,17 @@ 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("%.4ldms Philo N°%d %s\n", get_time() - philo.data->time_start, philo.philo_id, event);
|
printf("%ldms Philo N°%d %s\n", get_time() - philo.data->time_start, philo.philo_id, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void philo_take_fork(s_philo *philo_tmp)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void philo_sleep_think(s_philo *philo_tmp)
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void philo_eat(s_philo *philo_tmp)
|
void philo_eat(s_philo *philo_tmp)
|
||||||
@ -42,19 +52,25 @@ void philo_eat(s_philo *philo_tmp)
|
|||||||
|
|
||||||
philo = *philo_tmp;
|
philo = *philo_tmp;
|
||||||
pthread_mutex_lock(philo.right_fork);
|
pthread_mutex_lock(philo.right_fork);
|
||||||
put_event(philo, "has taken a fork");
|
|
||||||
pthread_mutex_lock(philo.left_fork);
|
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");
|
||||||
printf("old last eat of --%d-- = %ld\n", philo.philo_id, *philo_tmp->last_eat - philo.data->time_start);
|
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();
|
||||||
printf("last eat of --%d-- = %ld\n", philo.philo_id, *philo_tmp->last_eat - philo.data->time_start);
|
|
||||||
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.right_fork);
|
||||||
pthread_mutex_unlock(philo.left_fork);
|
pthread_mutex_unlock(philo.left_fork);
|
||||||
|
pthread_mutex_lock(philo.display);
|
||||||
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)
|
||||||
@ -66,8 +82,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);
|
//printf("PHILO IS DEAD last eat: %ld-- ", *philo->last_eat - philo->data->time_start);
|
||||||
put_event(*philo, "PHILO IS DEAD\n\n\n--\n");
|
pthread_mutex_lock(philo->display);
|
||||||
|
put_event(*philo, "died");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,6 +106,8 @@ void *routine(void *tmp)
|
|||||||
philo_eat(philo);
|
philo_eat(philo);
|
||||||
philo->must_eat--;
|
philo->must_eat--;
|
||||||
}
|
}
|
||||||
|
if (!philo->must_eat)
|
||||||
|
printf("philo n%d is full\n", philo->philo_id);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,13 +115,19 @@ s_philo *create_philo(s_arg *data)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
s_philo *philo;
|
s_philo *philo;
|
||||||
|
pthread_mutex_t *display;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
philo = malloc(sizeof(s_philo) * data->nb_philo);
|
philo = malloc(sizeof(s_philo) * data->nb_philo);
|
||||||
if (!philo)
|
if (!philo)
|
||||||
return (0);
|
return (0);
|
||||||
|
display = malloc(sizeof(pthread_mutex_t));
|
||||||
|
if (!display)
|
||||||
|
return (0);
|
||||||
|
pthread_mutex_init(display, 0);
|
||||||
while (i < data->nb_philo)
|
while (i < data->nb_philo)
|
||||||
{
|
{
|
||||||
|
philo[i].display = display;
|
||||||
philo[i].philo_id = i + 1;
|
philo[i].philo_id = i + 1;
|
||||||
philo[i].data = data;
|
philo[i].data = data;
|
||||||
philo[i].must_eat = data->must_eat;
|
philo[i].must_eat = data->must_eat;
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
typedef struct s_list
|
typedef struct s_list
|
||||||
{
|
{
|
||||||
int nb_philo;
|
int nb_philo;
|
||||||
@ -36,6 +37,7 @@ typedef struct t_list
|
|||||||
long last_eat[1];
|
long last_eat[1];
|
||||||
pthread_mutex_t *right_fork;
|
pthread_mutex_t *right_fork;
|
||||||
pthread_mutex_t *left_fork;
|
pthread_mutex_t *left_fork;
|
||||||
|
pthread_mutex_t *display;
|
||||||
|
|
||||||
} s_philo;
|
} s_philo;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user