-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmang_action.c
111 lines (104 loc) · 2.84 KB
/
mang_action.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mang_action.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aaljaber <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 20:09:49 by aaljaber #+# #+# */
/* Updated: 2022/04/23 19:46:20 by aaljaber ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void eating(t_philo *dumb)
{
if (f_died_lock(dumb))
print_for_dumb(dumb, "is eating", BMAG, 'l');
dumb->sim = what_is_time_now(dumb);
while (42)
{
what_is_time_now(dumb);
if (!check_death(dumb))
break ;
if (dumb->t_now - dumb->t_stop > 0)
break ;
usleep (1000);
}
dumb->yes_eat = 1;
mang_fork((void *)dumb, 3);
if (dumb->m_info->num_eat != 0)
dumb->e++;
dumb->f_left = 0;
dumb->f_right = 0;
// printf("%d\n",dumb->t_now - dumb->sim);
check_death2(dumb);
}
void sleeping(t_philo *dumb)
{
if (f_died_lock(dumb))
print_for_dumb(dumb, "is sleeping", BGRN, 'l');
dumb->yes_eat = 0;
count_for_dumb(dumb, 's');
while (42)
{
what_is_time_now(dumb);
if (!check_death(dumb))
break ;
if (dumb->t_now - dumb->t_stop > 0)
break ;
usleep(1000);
}
dumb->yes_sleep = 1;
check_death(dumb);
}
void thinking(t_philo *dumb)
{
check_death(dumb);
if (f_died_lock(dumb))
print_for_dumb(dumb, "is thinking", BYEL, 'l');
dumb->yes_sleep = 0;
dumb->yes_think = 1;
}
void dying(t_philo *dumb)
{
count_for_dumb(dumb, 'd');
while (42)
{
if (!check_death(dumb))
break ;
if (mang_fork((void *)dumb, 1))
{
calc_tstamp(dumb, 'n');
break ;
}
what_is_time_now(dumb);
pthread_mutex_lock(&dumb->m_info->d_mut);
if (dumb->t_now - dumb->t_stop >= 0 && dumb->m_info->f_died != 1)
{
dumb->m_info->f_died = 1;
pthread_mutex_unlock(&dumb->m_info->d_mut);
print_for_dumb(dumb, "is died", BRED, 'd');
break ;
}
pthread_mutex_unlock(&dumb->m_info->d_mut);
usleep(1000);
}
}
void check_death2(t_philo *dumb)
{
int i;
what_is_time_now(dumb);
if ((dumb->t_now - dumb->sim) + dumb->m_info->time_sleep >= dumb->m_info->time_die)
{
i = dumb->m_info->time_die - (dumb->t_now - dumb->sim);
if (i > 0)
usleep(i * 1000);
if (f_died_lock(dumb))
{
pthread_mutex_lock(&dumb->m_info->d_mut);
dumb->m_info->f_died = 1;
pthread_mutex_unlock(&dumb->m_info->d_mut);
print_for_dumb(dumb, "is died", BRED, 'd');
}
}
}