-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphilo.c
137 lines (126 loc) · 3.11 KB
/
philo.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: azaghlou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 16:35:26 by azaghlou #+# #+# */
/* Updated: 2023/04/27 16:11:07 by azaghlou ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int main_check(int ac, char **av)
{
int i;
i = 1;
if (ac != 6 && ac != 5)
{
printf("you have to enter 5 or 4 arguments\n");
return (1);
}
while (i < ac)
{
if (check(av[i], i) == 1)
{
printf("Error\n");
return (1);
}
i++;
}
return (0);
}
void dead_and_eats_check(t_inf *p, int num, int i, int enter)
{
while (i < num && p[0].num_philos != 1)
{
pthread_mutex_lock(p[i].finish_eat_mut);
if (p[i].finish_the_eats[0] == p[i].num_philos)
{
pthread_mutex_unlock(p[i].finish_eat_mut);
break ;
}
pthread_mutex_unlock(p[i].finish_eat_mut);
pthread_mutex_lock(p[i].last_eat_mut);
if (enter == 0 && timenow() - p[i].time_of_last_eat >= p[i].time_to_die)
{
pthread_mutex_unlock(p[i].last_eat_mut);
enter = 1;
pthread_mutex_lock(p[i].dead_mut);
p[i].dead[0] = 1;
pthread_mutex_unlock(p[i].dead_mut);
dead(&p[i]);
break ;
}
pthread_mutex_unlock(p[i].last_eat_mut);
i++;
if (i == num)
i = 0;
}
}
int threads_join(pthread_t *threads, int num, t_inf *p)
{
int i;
int rc;
int enter;
void *rtrn;
i = 0;
enter = 0;
rtrn = malloc(sizeof(void) * num);
dead_and_eats_check(p, num, i, enter);
while (i < num)
{
rc = pthread_join(threads[i], &rtrn[i]);
i++;
}
free(rtrn);
return (0);
}
int free_fct(pthread_t *threads, t_inf *p)
{
free(p->forks);
free(p->dead_mut);
free(p->last_eat_mut);
free(p->finish_eat_mut);
free(p->dead);
free(p->finish_the_eats);
free(p);
free(threads);
return (1);
}
int create_threads(int num, int ac, char **av)
{
int i;
int rc;
t_inf *p;
pthread_t *threads;
i = 0;
p = malloc(sizeof(t_inf) * num_philos(0, 0));
threads = malloc(sizeof(pthread_t) * num - 1);
mutex_fill(&p[0], i, num);
fill_prmtr(&p[0], av, ac);
while (i < num)
{
rc = pthread_create(&threads[i], NULL, routine, &p[i]);
if (rc != 0)
return (free_fct(&threads[0], &p[0]));
i++;
usleep(1727);
}
if (threads_join(&threads[0], num, &p[0]) == 1)
return (free_fct(&threads[0], &p[0]));
return (free_fct(&threads[0], &p[0]));
}
int main(int ac, char **av)
{
int i;
int num;
i = 0;
num = ft_atoi(av[1]);
num_philos(num, 1);
if (main_check(ac, av) == 1)
return (1);
if (create_threads(num, ac, av) == 1)
return (1);
return (0);
}