-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphilo.h
More file actions
90 lines (80 loc) · 2.72 KB
/
philo.h
File metadata and controls
90 lines (80 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dyarkovs <dyarkovs@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 13:59:06 by dyarkovs #+# #+# */
/* Updated: 2024/07/03 19:01:32 by dyarkovs ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILO_H
# define PHILO_H
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
#include <sys/time.h>
#define YELLOW "\033[0;33m"
#define RED "\033[0;31m"
#define GREEN "\033[0;32m"
#define RE "\033[0m"
// #include "libft.h"
typedef pthread_mutex_t mtx_t;
typedef struct s_philo t_philo;
typedef enum s_act
{
EAT,
SLEEP,
THINK,
FORK,
DIE
} t_act;
typedef struct s_philosophers
{
int n_philos;
t_philo *philo_arr;
mtx_t *fork_arr;
long die_time;
long eat_time;
long sleep_time;
int n_meals;
int full_philos;
bool is_dead;
long prog_start_time;
mtx_t print_mtx;
mtx_t check_mtx;
} t_philosophers;
typedef struct s_philo
{
pthread_t thread;
mtx_t *fork1;
mtx_t *fork2;
int id;
int meals_ate;
bool is_full;
long ate_last_time;
t_philosophers *data;
} t_philo;
//utils
int err_check(int ac, char **av);
int is_digit_loop(char *s);
long ft_atol(const char *str);
void print_state(t_philo *philo, t_act act);
bool monitor_usleep(int mcs, t_philo *philo);
bool check_dead(t_philo *philo);
bool check_full(t_philosophers *data);
long gettimeofday_in_mcs();
long gettimefromstart_ms(long start);
bool init_prog(char **av, t_philosophers *data);
void init_philos(t_philosophers *data);
void create_forks(mtx_t **arr, int n);
void create_threads(t_philosophers *data);
void *philo_routine(void *philo);
bool philo_eat(t_philo *philo);
bool philo_sleep(t_philo *philo);
void stop_prog(t_philosophers *data);
#endif