philosophers/philo/inc/philo.h
2025-01-24 16:43:46 +01:00

91 lines
2.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* philo.h :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:27:34 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:34 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#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>
# include <limits.h>
# define SUCCESS 1
# define FAILURE 0
# define START_DELAY 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 finished;
int start_time;
pthread_mutex_t *print_lock;
pthread_mutex_t *finished_lock;
pthread_mutex_t *forks;
pthread_t *monitor;
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;
pthread_mutex_t *death_lock;
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_death(t_philo *philo);
void philo_die(t_philo *philo);
int philo_died_while_sleeping(t_philo *philo, int time);
int philo_eat(t_philo *philo);
void *monitor_routine(void *arg);
int check_philos(t_rules *rules);
int ft_min(int a, int b);
#endif