-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
70 lines (65 loc) · 2.24 KB
/
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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: astein <astein@student.42lisboa.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/31 17:20:27 by astein #+# #+# */
/* Updated: 2023/08/10 16:38:32 by astein ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
static void put_params(t_table *table)
{
if (PUT_MORE_INFOS)
{
pthread_mutex_lock(&table->m_print);
printf("===========================================\n");
printf("\tnum_philos:\t\t%d\n", table->num_philos);
printf("\tdur_die:\t\t%ld\n", table->dur_die);
printf("\tdur_eat:\t\t%ld\n", table->dur_eat);
printf("\tdur_sleep:\t\t%ld\n", table->dur_sleep);
printf("\ttimes_philo_must_eat:\t%d\n", table->times_philo_must_eat);
printf("===========================================\n");
pthread_mutex_unlock(&table->m_print);
}
}
int main(int argc, char **argv)
{
t_table *table;
if (argc < 5 || argc > 6)
put_exit_msg(NULL, "wrong number of arguments\n", FALSE);
table = malloc(sizeof(t_table));
if (!table)
put_exit_msg(NULL, "malloc for table failed\n", FALSE);
ini_table(table, argc, argv);
put_params(table);
ini_philos(table);
set_dinner_start(table, TRUE);
usleep(1000);
while (1)
{
if (check_if_any_philo_died(table))
exit_dining(table, FALSE);
if (check_if_all_philo_have_eaten_enough(table))
{
put_msg_id(table->philos, MSG_ALL_EAT, NO_FORK);
exit_dining(table, TRUE);
}
usleep(10);
}
}
void exit_dining(t_table *table, t_bool success)
{
if (!table)
exit(EXIT_FAILURE);
set_dinner_end(table, TRUE);
join_philos(table);
if (table)
free_table(table);
if (success)
exit(EXIT_SUCCESS);
else
exit(EXIT_FAILURE);
}