-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeath_checker.c
51 lines (46 loc) · 1.75 KB
/
death_checker.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* death_checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stigkas <stigkas@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 11:08:52 by stigkas #+# #+# */
/* Updated: 2024/03/26 11:08:52 by stigkas ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/philo.h"
static bool philo_is_dead(t_philo *philo)
{
long time_passed;
if (get_bool(&philo->philo_mtx, philo->full))
return (false);
time_passed = getthetime(MLSEC) - \
get_long(&philo->philo_mtx, &philo->time_from_last_meal);
if (time_passed > (philo->table->time_to_die / 1e3))
return (true);
return (false);
}
void *is_dead(void *data)
{
t_table *table;
int i;
table = (t_table *)data;
while (!threads_running(&table->table_mtx, \
&table->nbr_of_threads_running, table->philo_nbr))
;
while (!simulation_is_finished(table))
{
i = 0;
while ((i < table->philo_nbr) && !simulation_is_finished(table))
{
if (philo_is_dead(table->philos + i))
{
set_bool(&table->table_mtx, &table->end_simulation, true);
display_status(DEAD, table->philos + i);
}
i++;
}
}
return (NULL);
}