-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphilo.c
134 lines (124 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amaach <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/02 09:23:50 by amaach #+# #+# */
/* Updated: 2021/11/05 12:03:09 by amaach ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int initialisation(char **argv, int argc)
{
t_infos *info;
info = statlist();
if (ft_is_alldigit(argv[1]) && ft_is_alldigit(argv[2])
&& ft_is_alldigit(argv[3]) && ft_is_alldigit(argv[4]))
{
info->number = ft_atoi(argv[1]);
info->die = ft_atoi(argv[2]);
info->eat = ft_atoi(argv[3]);
info->sleep = ft_atoi(argv[4]);
if (argc == 6)
{
if (ft_is_alldigit(argv[5]))
info->pme = ft_atoi(argv[5]);
else
return (1);
}
else
info->pme = -22;
}
else
return (1);
return (0);
}
int check_death(void)
{
t_infos *info;
unsigned long long test;
unsigned long long full;
unsigned long long i;
full = 0;
i = 0;
info = statlist();
while (i < info->number)
{
test = ft_gettime() - info->philo[i].last_eat;
if ((info->die <= (ft_gettime() - info->philo[i].last_eat))
&& (info->philo[i].eating == 0))
{
printf("%lld %lld died\n", ft_gettime(), i);
return (1);
}
if (info->pme >= 0)
full += (info->philo[i].nb_meals >= info->pme);
if (full == info->number)
return (1);
i++;
}
return (0);
}
int beggin(unsigned long long i, int *id)
{
t_infos *info;
info = statlist();
while (i < info->number)
{
pthread_mutex_init(&info->is_eating[i], NULL);
pthread_mutex_init(&info->forks[i++], NULL);
}
i = -1;
while (++i < info->number)
{
*id = i;
info->philo[i].last_eat = ft_gettime();
pthread_create(&info->philo[i].thread, NULL, &routine, id);
usleep(100);
}
while (1)
{
if (check_death())
return (1);
}
i = 0;
while (i < info->number)
pthread_join(info->philo[i++].thread, NULL);
return (0);
}
void free_shit(t_infos *info, int *id)
{
free(info->philo);
free(info->forks);
free(info->is_eating);
free(id);
}
int main(int argc, char **argv)
{
t_infos *info;
int i;
int *id;
i = 0;
info = statlist();
id = malloc(sizeof (int));
if (argc == 5 || argc == 6)
{
if (initialisation(argv, argc))
return (1);
info->philo = malloc((sizeof (t_philosophers)) * info->number);
info->forks = malloc((sizeof (pthread_mutex_t)) * info->number);
info->is_eating = malloc((sizeof (pthread_mutex_t)) * info->number);
if (beggin(0, id))
{
free_shit(info, id);
return (1);
}
}
else
{
printf("Number of args is unavailable\n");
return (1);
}
}