-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.c
125 lines (114 loc) · 3.28 KB
/
algorithm.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* algorithm.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rtavabil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 16:25:05 by rtavabil #+# #+# */
/* Updated: 2024/03/21 18:57:18 by rtavabil ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
void *dinner(void *data)
{
t_philo *philo;
philo = (t_philo *)data;
wait_all(philo->args);
ft_mutex(&philo->args->args_mutex, "lock");
philo->args->num_th++;
ft_mutex(&philo->args->args_mutex, "unlock");
ft_mutex(&philo->mutex, "lock");
philo->last_eat = gettime("milli");
ft_mutex(&philo->mutex, "unlock");
while (!is_finished(philo->args))
{
if (get_bool(&philo->mutex, &philo->full))
break ;
eat(philo);
ft_print(philo, "sleep");
ft_usleep(philo->args->t_sleep, philo->args);
think(philo);
}
return (NULL);
}
int is_died(t_philo *philo)
{
long elapsed;
long t_to_die;
if (get_bool(&philo->mutex, &philo->full))
return (0);
elapsed = gettime("milli") - get_long(philo,
&philo->last_eat);
t_to_die = philo->args->t_die / 1e3;
if (elapsed > t_to_die)
return (1);
return (0);
}
void *monitor(void *data)
{
t_args *args;
int i;
args = (t_args *)data;
while (all_threads_run(args))
;
while (!is_finished(args))
{
i = 0;
while (i < args->num_phil && !is_finished(args))
{
if (is_died(args->philos + i))
{
ft_mutex(&args->args_mutex, "lock");
args->end = 1;
ft_mutex(&args->args_mutex, "unlock");
ft_print(args->philos + i, "died");
break ;
}
i++;
}
}
return (NULL);
}
void *one_philo(void *data)
{
t_philo *philo;
philo = (t_philo *)data;
wait_all(philo->args);
ft_mutex(&philo->args->args_mutex, "lock");
philo->args->num_th++;
ft_mutex(&philo->args->args_mutex, "unlock");
ft_mutex(&philo->mutex, "lock");
philo->last_eat = gettime("milli");
ft_mutex(&philo->mutex, "unlock");
ft_print(philo, "fork");
while (!is_finished(philo->args))
usleep(100);
return (NULL);
}
void algorithm(t_args *args)
{
int i;
i = -1;
if (args->must_eat == 0)
return ;
if (args->num_phil == 1)
ft_thread(&args->philos[0].thread, one_philo, \
&args->philos[0], "create");
else
while (++i < args->num_phil)
ft_thread(&args->philos[i].thread, dinner, \
&args->philos[i], "create");
ft_thread(&args->monitor, monitor, args, "create");
args->start = gettime("milli");
ft_mutex(&args->args_mutex, "lock");
args->all_ready = 1;
ft_mutex(&args->args_mutex, "unlock");
i = -1;
while (++i < args->num_phil)
ft_thread(&args->philos[i].thread, dinner, &args->philos[i], "join");
ft_mutex(&args->args_mutex, "lock");
args->end = 1;
ft_mutex(&args->args_mutex, "unlock");
ft_thread(&args->monitor, monitor, args, "join");
}