Add files via upload
This commit is contained in:
commit
cde3e458fe
10 changed files with 806 additions and 0 deletions
93
philo/includes/philo.h
Normal file
93
philo/includes/philo.h
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/04 11:34:04 by rparodi #+# #+# */
|
||||
/* Updated: 2024/06/17 14:12:53 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_H
|
||||
# define PHILO_H
|
||||
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <stdio.h>
|
||||
# include <string.h>
|
||||
# include <sys/time.h>
|
||||
# include <pthread.h>
|
||||
# include <errno.h>
|
||||
# include <stdbool.h>
|
||||
# include "./types.h"
|
||||
|
||||
# define ARGS "Usage: ./philo <nb_philo> <t_death> <t_eat> <t_sleep> (<nb_eat>)"
|
||||
|
||||
# define GREEN "\033[32m"
|
||||
# define GREY "\033[0;90m"
|
||||
# define RED "\033[0;31m"
|
||||
# define BLUE "\033[34m"
|
||||
# define GOLD "\033[38;5;220m"
|
||||
# define END "\033[0m"
|
||||
|
||||
# ifndef MAXSUPPORT
|
||||
# define MAXSUPPORT 200
|
||||
# endif
|
||||
|
||||
# ifndef EATTIME
|
||||
# define EATTIME 3
|
||||
# endif
|
||||
|
||||
typedef struct s_philo
|
||||
{
|
||||
t_usize nb_philo;
|
||||
t_usize t_eat;
|
||||
t_usize t_death;
|
||||
t_usize t_sleep;
|
||||
t_i32 nb_eat;
|
||||
pthread_t thread;
|
||||
t_i32 id;
|
||||
t_i32 eating;
|
||||
t_i32 eating_count;
|
||||
t_usize t_last_eat;
|
||||
t_usize start_time;
|
||||
t_i32 *dead_check;
|
||||
t_mutex *r_fork;
|
||||
t_mutex *l_fork;
|
||||
t_mutex *print_lock;
|
||||
t_mutex *dead_lock;
|
||||
t_mutex *meal_lock;
|
||||
} t_philo;
|
||||
|
||||
typedef struct s_program
|
||||
{
|
||||
t_i32 dead_flag;
|
||||
t_mutex dead_lock;
|
||||
t_mutex meal_lock;
|
||||
t_mutex print_lock;
|
||||
t_philo *philos;
|
||||
} t_program;
|
||||
|
||||
t_usize ft_time(void);
|
||||
void ft_free(void *ptr);
|
||||
t_usize ft_atou_return(t_str str);
|
||||
t_error ft_sleeping(t_philo *philo);
|
||||
t_error ft_thinking(t_philo *philo);
|
||||
t_i32 main(t_i32 argc, t_str *argv);
|
||||
void ft_pause(size_t milliseconds);
|
||||
t_error ft_start_eating(t_philo *philo);
|
||||
void ft_exit(t_str msg, t_u8 status);
|
||||
t_error ft_ending_eating(t_philo *philo);
|
||||
void ft_logs(t_str msg, t_philo *philo);
|
||||
t_error ft_atou(const char *nptr, t_usize *value);
|
||||
t_error ft_init_fork(t_philo *philo, t_mutex *fork);
|
||||
t_error ft_init_thread(t_program *prog, t_mutex *forks);
|
||||
t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo);
|
||||
t_error ft_init(int argc, t_str *argv, t_program *program, t_philo *philo);
|
||||
t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks);
|
||||
void ft_destroy_exit(t_str msg, t_u8 status, t_program *prog, \
|
||||
t_mutex *forks);
|
||||
|
||||
#endif
|
||||
72
philo/includes/types.h
Normal file
72
philo/includes/types.h
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* types.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/03 14:31:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/06/09 14:32:57 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef TYPES_H
|
||||
# define TYPES_H
|
||||
|
||||
# include <stdbool.h>
|
||||
# include <stddef.h>
|
||||
# include <stdint.h>
|
||||
# include <unistd.h>
|
||||
# include <pthread.h>
|
||||
|
||||
/// @def signal that an error occured
|
||||
# define ERROR 1
|
||||
/// @def signal that no error occured
|
||||
# define NO_ERROR 0
|
||||
|
||||
/// @brief A mutex (using pthread)
|
||||
typedef pthread_mutex_t t_mutex;
|
||||
// @brief A thread (using pthread)
|
||||
typedef pthread_t t_thread;
|
||||
|
||||
/// @brief A string, null terminated
|
||||
typedef char *t_str;
|
||||
/// @brief A constant string, null terminated
|
||||
typedef const char *t_const_str;
|
||||
|
||||
/// @brief an unsigned 8 bit integer
|
||||
typedef uint8_t t_u8;
|
||||
/// @brief a signed 8 bit integer
|
||||
typedef int8_t t_i8;
|
||||
|
||||
/// @brief an unsigned 16 bit integer
|
||||
typedef uint16_t t_u16;
|
||||
/// @brief a signed 16 bit integer
|
||||
typedef int16_t t_i16;
|
||||
|
||||
/// @brief an unsigned 32 bit integer
|
||||
typedef uint32_t t_u32;
|
||||
/// @brief a signed 32 bit integer
|
||||
typedef int32_t t_i32;
|
||||
|
||||
/// @brief an unsigned 64 bit integer
|
||||
typedef uint64_t t_u64;
|
||||
/// @brief a signed 64 bit integer
|
||||
typedef int64_t t_i64;
|
||||
|
||||
/// @brief a signed integer that can hold a pointer
|
||||
typedef ssize_t t_isize;
|
||||
/// @brief an unsigned integer that can hold a pointer
|
||||
typedef size_t t_usize;
|
||||
|
||||
/// @brief a 32 bit floating point number
|
||||
typedef float t_f32;
|
||||
/// @brief a 64 bit floating point number
|
||||
typedef double t_f64;
|
||||
|
||||
/// @brief a boolean value that represents an error
|
||||
/// @note true is an error, false is no error
|
||||
typedef bool t_error;
|
||||
typedef bool t_bool;
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue