school
This commit is contained in:
parent
40357b94a2
commit
6514f7414c
34
main.c
34
main.c
@ -12,12 +12,12 @@
|
|||||||
|
|
||||||
#include "philosophers.h"
|
#include "philosophers.h"
|
||||||
|
|
||||||
s_arg *create_data(int argc, char **argv)
|
t_arg *create_data(int argc, char **argv)
|
||||||
{
|
{
|
||||||
s_arg *data;
|
t_arg *data;
|
||||||
|
|
||||||
usleep(1500000);
|
usleep(1500000);
|
||||||
data = malloc(sizeof(s_arg));
|
data = malloc(sizeof(t_arg));
|
||||||
*data->death = 1;
|
*data->death = 1;
|
||||||
data->nb_full_philo = 0;
|
data->nb_full_philo = 0;
|
||||||
data->nb_philo = ft_atoi(argv[1]);
|
data->nb_philo = ft_atoi(argv[1]);
|
||||||
@ -32,7 +32,7 @@ s_arg *create_data(int argc, char **argv)
|
|||||||
return (data);
|
return (data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void exit_philo(s_philo *philo, s_arg *data)
|
void exit_philo(t_philo *philo, t_arg *data)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ void exit_philo(s_philo *philo, s_arg *data)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void put_event(s_philo philo, char *event)
|
void put_event(t_philo philo, char *event)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(philo.display);
|
pthread_mutex_lock(philo.display);
|
||||||
if (*philo.data->death)
|
if (*philo.data->death)
|
||||||
@ -60,7 +60,7 @@ void put_event(s_philo philo, char *event)
|
|||||||
pthread_mutex_unlock(philo.display);
|
pthread_mutex_unlock(philo.display);
|
||||||
}
|
}
|
||||||
|
|
||||||
void philo_take_fork(s_philo philo)
|
void philo_take_fork(t_philo philo)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(philo.left_fork);
|
pthread_mutex_lock(philo.left_fork);
|
||||||
put_event(philo, "has taken a fork");
|
put_event(philo, "has taken a fork");
|
||||||
@ -68,9 +68,9 @@ void philo_take_fork(s_philo philo)
|
|||||||
put_event(philo, "has taken a fork");
|
put_event(philo, "has taken a fork");
|
||||||
}
|
}
|
||||||
|
|
||||||
void philo_eat(s_philo *philo_tmp)
|
void philo_eat(t_philo *philo_tmp)
|
||||||
{
|
{
|
||||||
s_philo philo;
|
t_philo philo;
|
||||||
|
|
||||||
philo = *philo_tmp;
|
philo = *philo_tmp;
|
||||||
philo_take_fork(philo);
|
philo_take_fork(philo);
|
||||||
@ -84,7 +84,7 @@ void philo_eat(s_philo *philo_tmp)
|
|||||||
put_event(philo, "is thinking");
|
put_event(philo, "is thinking");
|
||||||
}
|
}
|
||||||
|
|
||||||
void *death_checker(s_philo *philo, s_arg *data)
|
void *death_checker(t_philo *philo, t_arg *data)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@ -117,9 +117,9 @@ void *death_checker(s_philo *philo, s_arg *data)
|
|||||||
|
|
||||||
void *routine(void *tmp)
|
void *routine(void *tmp)
|
||||||
{
|
{
|
||||||
s_philo *philo;
|
t_philo *philo;
|
||||||
|
|
||||||
philo = (s_philo *)tmp;
|
philo = (t_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);
|
||||||
@ -135,14 +135,14 @@ void *routine(void *tmp)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
s_philo *create_philo(s_arg *data)
|
t_philo *create_philo(t_arg *data)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
s_philo *philo;
|
t_philo *philo;
|
||||||
pthread_mutex_t *display;
|
pthread_mutex_t *display;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
philo = malloc(sizeof(s_philo) * data->nb_philo);
|
philo = malloc(sizeof(t_philo) * data->nb_philo);
|
||||||
if (!philo)
|
if (!philo)
|
||||||
return (0);
|
return (0);
|
||||||
display = malloc(sizeof(pthread_mutex_t));
|
display = malloc(sizeof(pthread_mutex_t));
|
||||||
@ -169,7 +169,7 @@ s_philo *create_philo(s_arg *data)
|
|||||||
return (philo);
|
return (philo);
|
||||||
}
|
}
|
||||||
|
|
||||||
int start_philo(s_philo *philo, s_arg *data)
|
int start_philo(t_philo *philo, t_arg *data)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@ -215,8 +215,8 @@ int check_arg(int argc, char **argv)
|
|||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
s_arg *data;
|
t_arg *data;
|
||||||
s_philo *philo;
|
t_philo *philo;
|
||||||
|
|
||||||
if (check_arg(argc, argv))
|
if (check_arg(argc, argv))
|
||||||
{
|
{
|
||||||
|
|||||||
@ -29,20 +29,20 @@ typedef struct s_list
|
|||||||
int must_eat;
|
int must_eat;
|
||||||
long time_start;
|
long time_start;
|
||||||
int death[1];
|
int death[1];
|
||||||
} s_arg;
|
} t_arg;
|
||||||
|
|
||||||
typedef struct t_list
|
typedef struct t_list
|
||||||
{
|
{
|
||||||
int philo_id;
|
int philo_id;
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
s_arg *data;
|
t_arg *data;
|
||||||
int must_eat;
|
int must_eat;
|
||||||
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;
|
pthread_mutex_t *display;
|
||||||
|
|
||||||
} s_philo;
|
} t_philo;
|
||||||
|
|
||||||
//utils fonctions
|
//utils fonctions
|
||||||
long ft_atoi(const char *nptr);
|
long ft_atoi(const char *nptr);
|
||||||
|
|||||||
3
utils.c
3
utils.c
@ -43,6 +43,5 @@ long get_time(void)
|
|||||||
struct timeval time;
|
struct timeval time;
|
||||||
|
|
||||||
gettimeofday(&time, NULL);
|
gettimeofday(&time, NULL);
|
||||||
//printf("time : %ld\n", time.tv_sec * 1000 + time.tv_usec / 1000);
|
|
||||||
return (time.tv_sec * 1000 + time.tv_usec / 1000);
|
return (time.tv_sec * 1000 + time.tv_usec / 1000);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user