school
This commit is contained in:
parent
8f81091d27
commit
40357b94a2
22
main.c
22
main.c
@ -16,6 +16,7 @@ s_arg *create_data(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
s_arg *data;
|
s_arg *data;
|
||||||
|
|
||||||
|
usleep(1500000);
|
||||||
data = malloc(sizeof(s_arg));
|
data = malloc(sizeof(s_arg));
|
||||||
*data->death = 1;
|
*data->death = 1;
|
||||||
data->nb_full_philo = 0;
|
data->nb_full_philo = 0;
|
||||||
@ -40,7 +41,6 @@ void exit_philo(s_philo *philo, s_arg *data)
|
|||||||
{
|
{
|
||||||
pthread_mutex_destroy(philo[i].left_fork);
|
pthread_mutex_destroy(philo[i].left_fork);
|
||||||
free(philo[i].left_fork);
|
free(philo[i].left_fork);
|
||||||
//pthread_
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
pthread_mutex_destroy(philo[i].left_fork);
|
pthread_mutex_destroy(philo[i].left_fork);
|
||||||
@ -89,17 +89,19 @@ void *death_checker(s_philo *philo, s_arg *data)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (1)
|
while (*data->death)
|
||||||
{
|
{
|
||||||
i = -1;
|
i = -1;
|
||||||
while (i++ < data->nb_philo - 1 && philo[i].data->death)
|
while (i++ < data->nb_philo - 1 && *data->death && *philo[i].last_eat)
|
||||||
{
|
{
|
||||||
if (get_time() - *philo[i].last_eat > data->time_to_die && philo[i].must_eat)
|
if ((get_time() - *philo[i].last_eat) > data->time_to_die && philo[i].must_eat)
|
||||||
{
|
{
|
||||||
*philo[i].data->death = 0;
|
*philo[i].data->death = 0;
|
||||||
pthread_mutex_lock(philo->display);
|
pthread_mutex_lock(philo->display);
|
||||||
printf("%ldms Philo N°%d is died\n", get_time() - data->time_start, philo[i].philo_id);
|
printf("%ldms Philo N°%d is died\n",
|
||||||
|
get_time() - data->time_start, philo[i].philo_id);
|
||||||
printf("End: one philo died\n");
|
printf("End: one philo died\n");
|
||||||
|
printf("time_to_die: %d TIME: %ld start: %ld philo.start: %ld last_eat: %ld condition: %ld\n", data->time_to_die ,get_time(), data->time_start , philo[i].data->time_start, get_time() - *philo[i].last_eat ,get_time() - *philo[i].last_eat);
|
||||||
exit_philo(philo, data);
|
exit_philo(philo, data);
|
||||||
}
|
}
|
||||||
else if (data->nb_full_philo == data->nb_philo)
|
else if (data->nb_full_philo == data->nb_philo)
|
||||||
@ -116,19 +118,20 @@ void *death_checker(s_philo *philo, s_arg *data)
|
|||||||
void *routine(void *tmp)
|
void *routine(void *tmp)
|
||||||
{
|
{
|
||||||
s_philo *philo;
|
s_philo *philo;
|
||||||
pthread_t death;
|
|
||||||
|
|
||||||
philo = (s_philo *)tmp;
|
philo = (s_philo *)tmp;
|
||||||
*philo->last_eat = get_time();
|
*philo->last_eat = get_time();
|
||||||
//if (philo->philo_id % 2)
|
if (philo->philo_id % 2)
|
||||||
// usleep(15000);
|
usleep(15000);
|
||||||
while (1 && *philo->data->death && philo->must_eat && philo->data->nb_full_philo != philo->data->nb_philo)
|
while (*philo->data->death && philo->must_eat
|
||||||
|
&& philo->data->nb_full_philo != philo->data->nb_philo)
|
||||||
{
|
{
|
||||||
philo_eat(philo);
|
philo_eat(philo);
|
||||||
philo->must_eat--;
|
philo->must_eat--;
|
||||||
if (!philo->must_eat)
|
if (!philo->must_eat)
|
||||||
philo->data->nb_full_philo++;
|
philo->data->nb_full_philo++;
|
||||||
}
|
}
|
||||||
|
printf("philo n%d return\n", philo->philo_id);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,6 +151,7 @@ s_philo *create_philo(s_arg *data)
|
|||||||
pthread_mutex_init(display, 0);
|
pthread_mutex_init(display, 0);
|
||||||
while (i < data->nb_philo)
|
while (i < data->nb_philo)
|
||||||
{
|
{
|
||||||
|
*philo[i].last_eat = 0;
|
||||||
philo[i].display = display;
|
philo[i].display = display;
|
||||||
philo[i].philo_id = i + 1;
|
philo[i].philo_id = i + 1;
|
||||||
philo[i].data = data;
|
philo[i].data = data;
|
||||||
|
|||||||
@ -10,13 +10,15 @@
|
|||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PHILOSOPHERS_H
|
||||||
|
# define PHILOSOPHERS_H
|
||||||
|
|
||||||
# include <pthread.h>
|
# include <pthread.h>
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
# 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;
|
||||||
@ -45,3 +47,5 @@ typedef struct t_list
|
|||||||
//utils fonctions
|
//utils fonctions
|
||||||
long ft_atoi(const char *nptr);
|
long ft_atoi(const char *nptr);
|
||||||
long get_time(void);
|
long get_time(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user