-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
61 lines (52 loc) · 1.74 KB
/
utils.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rblondia <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/15 11:22:40 by rblondia #+# #+# */
/* Updated: 2022/01/26 14:38:26 by rblondia ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/philo.h"
long long get_time(void)
{
struct timeval time;
gettimeofday(&time, NULL);
return ((time.tv_sec * (u_int64_t) 1000) + (time.tv_usec / 1000));
}
int should_be_dead(t_philosopher *philosopher)
{
return ((!is_state(philosopher, EATING) && get_time() > philosopher->limit)
|| is_state(philosopher, DEAD));
}
int is_full(t_philosopher *philosopher)
{
return (philosopher->settings.must_eat_time > 0
&& philosopher->meals >= philosopher->settings.must_eat_time);
}
int everyone_is_full(t_app *app)
{
size_t i;
size_t full;
i = -1;
full = 0;
while (app->philosophers[++i])
{
if (is_full(app->philosophers[i]))
full++;
}
return (full == app->settings.philosophers);
}
int anyone_dead(t_app *app)
{
size_t i;
i = -1;
while (app->philosophers[++i])
{
if (should_be_dead(app->philosophers[i]))
return (1);
}
return (0);
}