-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexit.c
90 lines (82 loc) · 2.26 KB
/
exit.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: plpelleg <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/27 14:55:09 by plpelleg #+# #+# */
/* Updated: 2021/12/03 19:48:31 by plpelleg ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int ft_all_have_eaten(t_philo *philo)
{
int i;
i = -1;
while (++i < philo->parameters->n_philo)
if (philo[i].num_times_eaten < philo[i].parameters->n_times_to_eat)
break ;
if (i == philo->parameters->n_philo)
return (1);
return (0);
}
void *ft_end_check(void *philosopher)
{
int i;
t_philo *philo;
struct timeval now;
i = -1;
philo = (t_philo *)philosopher;
while (++i || 1)
{
gettimeofday(&now, NULL);
if ((ft_get_time(now) - (ft_get_time(philo[i].last_time_eaten))
) > (long unsigned int)philo[i].parameters-> time_to_die)
{
ft_print(&philo[i], 5);
break ;
}
else if (philo->parameters->n_times_to_eat > -1
&& ft_all_have_eaten(philo))
{
ft_print(&philo[i], 6);
break ;
}
if (i == philo[i].parameters->n_philo - 1)
i = -1;
}
return (NULL);
}
void ft_set_error(t_param *parameters)
{
write(1, "PARAMETER ERROR\n", 17);
parameters -> error = 1;
}
int ft_exit(t_param *parameters, pthread_mutex_t *forks,
pthread_t *threads, t_philo *philosophers)
{
int i;
i = -1;
if (threads)
{
while (++i < parameters->n_philo)
pthread_detach(threads[i]);
free(threads);
}
i = -1;
if (parameters)
{
pthread_mutex_destroy(¶meters->print);
free(parameters);
}
if (forks)
{
while (++i < parameters->n_philo)
pthread_mutex_destroy(&forks[i]);
free(forks);
}
if (philosophers)
free(philosophers);
return (0);
}