-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
114 lines (104 loc) · 2.75 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ykoh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/15 05:43:44 by ykoh #+# #+# */
/* Updated: 2021/03/15 05:43:45 by ykoh ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
static void destroy(t_fork *f, t_philo *p)
{
int i;
i = 0;
while (i < g_table.number_of_philosophers)
{
pthread_join(p[i].thread, NULL);
i++;
}
free(p);
p = NULL;
i = 0;
while (i < g_table.number_of_philosophers)
{
pthread_mutex_destroy(&(f[i].mutex));
i++;
}
free(f);
f = NULL;
pthread_mutex_destroy(&(g_table.print));
}
static t_philo *init_philo(t_fork *f)
{
int i;
t_philo *p;
p = ft_calloc(g_table.number_of_philosophers, sizeof(t_philo));
i = 0;
while (i < g_table.number_of_philosophers)
{
p[i].index = i;
p[i].eat_time = g_table.genesis;
p[i].left = &f[i];
if (i != (i + 1) % g_table.number_of_philosophers)
p[i].right = &f[(i + 1) % g_table.number_of_philosophers];
i++;
}
return (p);
}
static t_fork *init_fork(void)
{
int i;
t_fork *f;
f = ft_calloc(g_table.number_of_philosophers, sizeof(t_fork));
i = 0;
while (i < g_table.number_of_philosophers)
{
pthread_mutex_init(&(f[i].mutex), NULL);
i++;
}
return (f);
}
static int init_table(int argc, char *argv[])
{
if (!(argc == 5 || argc == 6))
return (0);
if ((g_table.number_of_philosophers = ft_atoi(argv[1])) < 2 ||
(g_table.time_to_die = ft_atoi(argv[2])) <= 0 ||
(g_table.time_to_eat = ft_atoi(argv[3])) <= 0 ||
(g_table.time_to_sleep = ft_atoi(argv[4])) <= 0)
return (0);
if (argv[5])
{
if ((g_table.number_of_times_each_philosopher_must_eat =\
ft_atoi(argv[5])) < 1)
return (0);
}
g_table.genesis = get_time();
pthread_mutex_init(&(g_table.print), NULL);
return (1);
}
int main(int argc, char *argv[])
{
int i;
t_fork *f;
t_philo *p;
if (!init_table(argc, argv))
{
write(2, "Error\n", 6);
return (1);
}
f = init_fork();
p = init_philo(f);
i = 0;
while (i < g_table.number_of_philosophers)
{
pthread_create(&(p[i].thread), NULL, lifecycle, &p[i]);
i++;
}
dead_or_alive(p);
destroy(f, p);
return (0);
}