73 lines
1.8 KiB
C
73 lines
1.8 KiB
C
#ifndef PHILO_H
|
|
# define PHILO_H
|
|
# include <pthread.h>
|
|
# include <stdio.h>
|
|
# include <unistd.h>
|
|
# include <stdlib.h>
|
|
# include <string.h>
|
|
# include <stddef.h>
|
|
# include <sys/time.h>
|
|
|
|
|
|
# define SUCCESS 1
|
|
# define FAILURE 0
|
|
|
|
# define ERROR_USAGE "Usage: ./philo number_of_philosophers time_to_die " \
|
|
"time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]\n"
|
|
# define ERROR_MUTEX "Can not create mutex"
|
|
# define ERROR_MALLOC "Malloc Failure"
|
|
# define ERROR_THREADS "Can not create threads"
|
|
|
|
typedef struct s_rules
|
|
{
|
|
int n_philos;
|
|
int time_to_die;
|
|
int time_to_eat;
|
|
int time_to_sleep;
|
|
int n_must_eat;
|
|
int death;
|
|
int start_time;
|
|
pthread_mutex_t *print_lock;
|
|
pthread_mutex_t *death_lock;
|
|
pthread_mutex_t *forks;
|
|
struct s_philo *philos;
|
|
|
|
} t_rules;
|
|
|
|
typedef struct s_philo
|
|
{
|
|
int id;
|
|
pthread_t *pid;
|
|
pthread_mutex_t *l_fork;
|
|
pthread_mutex_t *r_fork;
|
|
int death;
|
|
int last_meal;
|
|
t_rules *rules;
|
|
int n_eat;
|
|
} t_philo;
|
|
|
|
// memset, printf, malloc, free, write,
|
|
// usleep, gettimeofday, pthread_create,
|
|
// pthread_detach, pthread_join, pthread_mutex_init,
|
|
// pthread_mutex_destroy, pthread_mutex_lock,
|
|
// pthread_mutex_unlock
|
|
|
|
int ph_atoi(const char *nptr, int *res);
|
|
int get_time(void);
|
|
void free_philos(t_rules *rules);
|
|
int destroy_mutexes(t_rules *rules);
|
|
int create_philos(t_rules *rules);
|
|
int parse_arguments(int argc,char *argv[], t_rules *rules);
|
|
int create_mutexes(t_rules *rules);
|
|
int create_threads(t_rules *rules);
|
|
void print_status(t_philo *philo, char *status);
|
|
void *philo_routine(void *philo);
|
|
int join_threads(t_rules *rules);
|
|
void print_rules(t_rules *rules);
|
|
int check_alive(t_philo *philo);
|
|
|
|
|
|
|
|
|
|
|
|
#endif |