-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife_or_dead.c
61 lines (56 loc) · 2.15 KB
/
life_or_dead.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* life_or_dead.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyandriy <lyandriy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/02 17:40:16 by lyandriy #+# #+# */
/* Updated: 2023/10/26 19:15:42 by lyandriy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int check_dead(t_needle *needle)
{
pthread_mutex_lock(&needle->common_structure->mutex);
if (needle->common_structure->death_sign == 1)
{
pthread_mutex_unlock(&needle->common_structure->mutex);
return (0);
}
pthread_mutex_unlock(&needle->common_structure->mutex);
return (1);
}
int check_life(t_needle *needle)
{
uint64_t timer;
get_time(&timer);
if (needle->tt_die < (timer - needle->last_meal))
{
pthread_mutex_lock(&needle->common_structure->mutex);
if (needle->common_structure->death_sign == 0)
{
needle->common_structure->death_sign = 1;
needle->common_structure->time_die = (timer - needle->birth_time);
needle->common_structure->numb_philo = needle->my_number;
}
pthread_mutex_unlock(&needle->common_structure->mutex);
return (0);
}
return (1);
}
int check_life_2(t_needle *needle, uint64_t present_time)
{
pthread_mutex_lock(&needle->common_structure->mutex);
if (needle->common_structure->death_sign == 0)
{
needle->common_structure->death_sign = 1;
needle->common_structure->time_die
= (present_time - needle->birth_time);
needle->common_structure->numb_philo = needle->my_number;
pthread_mutex_unlock(&needle->common_structure->mutex);
return (0);
}
pthread_mutex_unlock(&needle->common_structure->mutex);
return (1);
}