-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdead_or_alive.c
66 lines (60 loc) · 1.7 KB
/
dead_or_alive.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
62
63
64
65
66
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dead_or_alive.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ykoh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/15 05:47:06 by ykoh #+# #+# */
/* Updated: 2021/03/15 05:47:07 by ykoh ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
static int is_finish(int *finish)
{
int i;
i = 0;
while (i < g_table.number_of_philosophers)
{
if (finish[i] == 0)
return (0);
i++;
}
return (1);
}
static int is_dead(t_philo *p)
{
if (g_table.time_to_die <= get_time() - p->eat_time)
{
print_status(p, DEAD);
return (1);
}
return (0);
}
void dead_or_alive(void *arg)
{
t_philo *p;
int *finish;
int i;
p = arg;
finish = ft_calloc(g_table.number_of_philosophers, sizeof(int));
i = 0;
while (1)
{
if (i == g_table.number_of_philosophers)
i = 0;
if (is_dead(&p[i]))
break ;
if (g_table.number_of_times_each_philosopher_must_eat &&
p[i].cnt >= g_table.number_of_times_each_philosopher_must_eat)
finish[i] = 1;
if (is_finish(finish))
{
print_status(NULL, END);
break ;
}
usleep(USLEEP);
i++;
}
free(finish);
}