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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
#ifndef CODEXION_H
# define CODEXION_H
# include <pthread.h> // for pthread_*
# define MAX 2
typedef struct s_sim t_sim;
typedef struct s_queue t_queue;
typedef struct s_fork t_fork;
typedef struct s_philo
{
int id;
int ticket;
pthread_t thread_id;
t_fork *left_fork;
t_fork *right_fork;
long beginning_last_eat_time;
t_sim *sim;
int number_of_eats_done;
pthread_mutex_t mutex_number_of_eats_done;
pthread_mutex_t mutex_beginning_last_eat_time;
pthread_mutex_t mutex_ticket;
pthread_mutex_t mutex_id;
pthread_mutex_t mutex_sim;
} t_philo;
typedef struct s_ticket
{
int num;
} t_ticket;
typedef struct s_queue
{
t_ticket items[MAX];
int size;
} t_queue;
typedef struct s_fork
{
int id;
size_t ticket_next;
int last_released;
t_philo *left_philo;
t_philo *right_philo;
t_sim *sim;
pthread_mutex_t mutex;
pthread_mutex_t mutex_left_philo;
pthread_mutex_t mutex_right_philo;
pthread_mutex_t mutex_ticket_next;
pthread_mutex_t mutex_cond;
pthread_mutex_t mutex_sim;
pthread_mutex_t mutex_last_released;
pthread_cond_t cond_signal;
t_queue pq;
pthread_mutex_t mutex_pq;
} t_fork;
typedef struct s_sim
{
int number_of_philos;
int time_to_starve;
int time_to_eat;
int time_to_reflect;
int time_to_think;
int number_of_eat_required;
int fork_cooldown;
char *scheduler;
t_fork **forks;
t_philo **philos;
long start_time;
pthread_t monitor_thread;
int philo_starved;
int number_of_forks_actual;
int number_of_philos_actual;
pthread_mutex_t mutex_philo_starved;
pthread_mutex_t mutex_print_log;
pthread_mutex_t mutex_cooldown;
pthread_mutex_t mutex_start_time;
pthread_mutex_t mutex_number_of_eat_done;
pthread_mutex_t mutex_philos;
pthread_mutex_t mutex_number_of_philos;
pthread_mutex_t mutex_number_of_eat_required;
pthread_mutex_t mutex_time_to_starve;
pthread_mutex_t mutex_time_to_think;
pthread_mutex_t mutex_time_to_reflect;
pthread_mutex_t mutex_time_to_eat;
pthread_mutex_t mutex_scheduler;
pthread_mutex_t mutex_forks;
} t_sim;
long time_ms(void);
int check_if_philo_starved(t_sim *sim);
void print_log(t_philo *philo, char *msg, int flag);
int check_for_starves(t_sim *sim);
void print_usage(void);
void free_philos(t_sim *sim);
void free_forks(t_sim *sim);
t_fork *create_fork(t_sim *sim);
t_fork **create_arr_forks(t_sim *sim);
t_philo *create_philo(t_sim *sim);
t_philo **create_arr_philos(t_sim *sim);
void start_timer(t_sim *sim);
t_philo *who_is_near(t_philo *philo, t_fork *fork);
void *routine(void *arg);
void *monitoring_routine(void *arg);
void init_mutex(t_sim *sim);
void destroy_mutex(t_sim *sim);
int run_threads(t_sim *sim);
t_philo **assign_forks(t_sim *sim);
t_fork **assign_philos(t_sim *sim);
t_sim *create_sim(char **argv);
int valid_params(char **argv);
int main(int argc, char **argv);
int philo_routine(t_philo *philo,
t_fork *left_fork, t_fork *right_fork);
void swap(t_ticket a, t_ticket b);
int time_until_starve(t_philo *philo);
int get_id(t_philo *philo);
void eat(t_philo *philo);
void reflect_and_think(t_philo *philo);
int wait_for_fork(t_philo *philo, t_fork *fork);
void wait_for_longest_cooldown(t_philo *philo,
t_fork *left_fork, t_fork *right_fork);
void update_last_released(t_sim *sim,
t_fork *fork1, t_fork *fork2);
int get_rel_time(t_sim *sim);
void dequeue(t_fork *fork);
int queue_routine(t_philo *philo,
t_fork *f1, t_fork *f2);
void enqueue(t_fork *fork, t_ticket ticket);
void heapify(t_fork *fork);
#endif
|