-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.c
More file actions
72 lines (65 loc) · 2.32 KB
/
init.c
File metadata and controls
72 lines (65 loc) · 2.32 KB
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: sanmetol <sanmetol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/12 13:02:01 by sanmetol #+# #+# */
/* Updated: 2025/02/12 13:03:01 by sanmetol ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
static void init_input(t_philo *philo, char **argv)
{
philo->input.time_to_die = ft_atol(argv[2]);
philo->input.time_to_eat = ft_atol(argv[3]);
philo->input.time_to_sleep = ft_atol(argv[4]);
philo->input.n_of_philos = ft_atol(argv[1]);
if (argv[5])
philo->input.n_times_must_eat = ft_atol(argv[5]);
else
philo->input.n_times_must_eat = -1;
}
void init_philo(t_philo *philos, t_sim *simulation, pthread_mutex_t *forks,
char **argv)
{
int i;
i = 0;
while (i < (int)ft_atol(argv[1]))
{
philos[i].id = i + 1;
philos[i].meals_eaten = 0;
init_input(&philos[i], argv);
philos[i].start_time = get_current_time_ms();
philos[i].last_meal_time = get_current_time_ms();
philos[i].mutex_log = &simulation->mutex_log;
philos[i].mutex_dead = &simulation->mutex_dead;
philos[i].mutex_meal = &simulation->mutex_meal;
philos[i].dead = &simulation->dead_flag;
philos[i].l_fork = &forks[i];
if (i == 0)
philos[i].r_fork = &forks[philos[0].input.n_of_philos - 1];
else
philos[i].r_fork = &forks[i - 1];
i++;
}
}
void init_forks(pthread_mutex_t *forks, int n_philo)
{
int i;
i = 0;
while (i < n_philo)
{
pthread_mutex_init(&forks[i], NULL);
i++;
}
}
void init_simulation(t_sim *simulation, t_philo *philos)
{
simulation->dead_flag = 0;
simulation->philos = philos;
pthread_mutex_init(&simulation->mutex_meal, NULL);
pthread_mutex_init(&simulation->mutex_log, NULL);
pthread_mutex_init(&simulation->mutex_dead, NULL);
}