-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.c
72 lines (67 loc) · 2.18 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: buthor <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/22 15:55:23 by buthor #+# #+# */
/* Updated: 2021/09/22 20:42:32 by buthor ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int check_args(int ac, char **av)
{
int number_of_word;
int number_of_letter;
number_of_word = 0;
if (ac != 6 && ac != 5)
return (FALSE);
while (++number_of_word != ac)
{
number_of_letter = 0;
if (av[number_of_word][number_of_letter] == '0')
return (FALSE);
while (av[number_of_word][number_of_letter] != END_OF_STRING)
{
if (ft_isdigit(av[number_of_word][number_of_letter]) == FALSE)
return (FALSE);
number_of_letter++;
}
}
return (TRUE);
}
int check_death(t_data *data, long long start_time, int index, int qty_ate_idx)
{
while (index != data->args.qty_of_philos)
{
if (data->philo[index].time_before_eat > 0)
{
if (data->philo[index].time_to_die <= get_time()
- data->philo[index].time_before_eat)
{
pthread_mutex_lock(&(data->printf));
printf(DEATH, data->philo[index].id, get_time() - start_time);
return (TRUE);
}
if (data->philo[index].number_of_eat == ATE)
qty_ate_idx++;
}
if (qty_ate_idx == data->args.qty_of_philos)
{
pthread_mutex_lock(&(data->printf));
printf("Philosophers are full of %lld ms!\n", get_time()
- start_time);
return (TRUE);
}
index++;
}
return (FALSE);
}
void check_leaks_and_free(t_data *data)
{
if (data->forks != NULL)
free(data->forks);
if (data->philo != NULL)
free(data->philo);
}