Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yabtaour
GitHub Repository: yabtaour/Philosophers-42
Path: blob/master/philo/ft_check_dead.c
882 views
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* ft_check_dead.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: yabtaour <[email protected]> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2022/05/19 15:21:35 by yabtaour #+# #+# */
9
/* Updated: 2022/05/19 23:36:14 by yabtaour ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
#include "philosophers.h"
13
14
int ft_check_total_eat(t_data *data)
15
{
16
if (data->argc == 6 && data->total >= data->philos_num * data->must_eat)
17
return (1);
18
return (0);
19
}
20
21
int ft_check_dead(t_data *data)
22
{
23
int i;
24
25
while (1)
26
{
27
i = 1;
28
while (i <= data->philos_num)
29
{
30
if (ft_check_total_eat(data))
31
return (1);
32
data->time = ft_timestamp();
33
data->norm = data->philosopher[i - 1].last_meal;
34
if ((data->time - data->norm) >= data->time_to_die)
35
{
36
data->is_dead = 1;
37
pthread_mutex_lock(&data->output);
38
data->norm2 = data->time - data->birth;
39
printf("[%lld] Philosopher %d died \n", data->norm2, i);
40
return (1);
41
}
42
}
43
}
44
return (0);
45
}
46
47