-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
41 lines (38 loc) · 878 Bytes
/
main.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
#include "philo.h"
static int parser(t_sim *sim, int argc, char **argv)
{
if (argc < 5 || argc > 6)
return (error("invalid argument", NULL));
if (argc == 5)
sim->total_meals = -1;
return ((ft_atoi(&sim->num, argv[1]) || !sim->num || \
ft_atoi(&sim->to_die, argv[2]) || ft_atoi(&sim->to_eat, argv[3]) || \
ft_atoi(&sim->to_sleep, argv[4]) || \
(argc == 6 && ft_atoi(&sim->total_meals, argv[5]))) ? \
error("invalid argument", NULL) : 0);
}
int main(int argc, char **argv)
{
t_sim sim;
size_t i;
memset(&sim, 0, sizeof(t_sim));
if (parser(&sim, argc, argv))
return (1);
if (argc == 6 && !sim.total_meals)
return (0);
if (setup(&sim))
return (1);
if (simulation(&sim))
{
i = 0;
while (i < sim.num)
{
if (pthread_join(*sim.threads[i], NULL) && \
error("thread error", NULL))
break ;
i++;
}
}
error(NULL, &sim);
return (0);
}