-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
72 lines (66 loc) · 2.12 KB
/
init.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: muerdoga <muerdoga@student.42kocaeli.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/31 15:02:04 by muerdoga #+# #+# */
/* Updated: 2023/02/10 14:50:17 by muerdoga ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int init_mutex(t_rules *rules)
{
int i;
i = rules->nb_philo;
while (--i >= 0)
{
if (pthread_mutex_init(&(rules->forks[i]), NULL))
return (1);
}
if (pthread_mutex_init(&(rules->writing), NULL))
return (1);
if (pthread_mutex_init(&(rules->meal_check), NULL))
return (1);
return (0);
}
int init_philosophers(t_rules *rules)
{
int i;
i = rules->nb_philo;
while (--i >= 0)
{
rules->philosophers[i].id = i;
rules->philosophers[i].x_ate = 0;
rules->philosophers[i].left_fork_id = i;
rules->philosophers[i].right_fork_id = (i + 1) % rules->nb_philo;
rules->philosophers[i].t_last_meal = 0;
rules->philosophers[i].rules = rules;
}
return (0);
}
int init_all(t_rules *rules, char **argv)
{
rules->nb_philo = ft_atoi(argv[1]);
rules->time_death = ft_atoi(argv[2]);
rules->time_eat = ft_atoi(argv[3]);
rules->time_sleep = ft_atoi(argv[4]);
rules->all_ate = 0;
rules->dieded = 0;
if (rules->nb_philo <= 0 || rules->time_death < 0 || rules->time_eat < 0
|| rules->time_sleep < 0 || rules->nb_philo > 250)
return (1);
if (argv[5])
{
rules->nb_eat = ft_atoi(argv[5]);
if (rules->nb_eat <= 0)
return (1);
}
else
rules->nb_eat = -1;
if (init_mutex(rules))
return (2);
init_philosophers(rules);
return (0);
}