-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphilosophers.c
53 lines (49 loc) · 1.85 KB
/
philosophers.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philosophers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: azaher <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/13 18:45:35 by azaher #+# #+# */
/* Updated: 2023/06/13 18:45:35 by azaher ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/philosophers.h"
int initialize_values(t_data *vars)
{
vars->philos = malloc(vars->count * sizeof(t_philo));
if (!vars->philos)
return (1);
vars->forks = malloc(vars->count * sizeof(pthread_mutex_t));
if (!vars->forks)
{
free(vars->philos);
return (1);
}
if (initialize_mutexes(vars))
{
(free(vars->philos), free(vars->forks));
return (1);
}
if (initialize_philosophers(vars))
{
(free(vars->philos), free(vars->forks));
return (1);
}
return (0);
}
int main(int argc, char **argv)
{
t_data vars;
vars.count_mx_eat = 0;
vars.abort_cond = 0;
if (argc < 5 || argc > 6)
return (return_error(ERRMSG));
if (valid_args(argc, argv, &vars))
return (return_error("philo: Invalid arguments. \n"));
if (initialize_values(&vars))
return (return_error("philo: Error in value initialization. \n"));
if (start_philosophers(&vars))
return (return_error("philo: Error in thread initialization. \n"));
}