-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.c
121 lines (110 loc) · 3.27 KB
/
launcher.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* launcher.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: muerdoga <muerdoga@student.42kocaeli.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/31 15:02:08 by muerdoga #+# #+# */
/* Updated: 2023/02/10 15:48:39 by muerdoga ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void philo_eats(t_philosopher *philo)
{
t_rules *rules;
rules = philo->rules;
pthread_mutex_lock(&(rules->forks[philo->left_fork_id]));
action_print(rules, philo->id, "has taken a fork");
pthread_mutex_lock(&(rules->forks[philo->right_fork_id]));
action_print(rules, philo->id, "has taken a fork");
pthread_mutex_lock(&(rules->meal_check));
action_print(rules, philo->id, "is eating");
philo->t_last_meal = timestamp();
pthread_mutex_unlock(&(rules->meal_check));
smart_sleep(rules->time_eat, rules);
(philo->x_ate)++;
pthread_mutex_unlock(&(rules->forks[philo->left_fork_id]));
pthread_mutex_unlock(&(rules->forks[philo->right_fork_id]));
}
void *p_thread(void *void_philosopher)
{
int i;
t_philosopher *philo;
t_rules *rules;
i = 0;
philo = (t_philosopher *)void_philosopher;
rules = philo->rules;
if (philo->id % 2)
usleep(15000);
while (!(rules->dieded))
{
philo_eats(philo);
if (rules->dieded)
break ;
if (rules->nb_eat != -1 && rules->all_ate)
break ;
if (rules->nb_eat != -1 && philo->x_ate >= rules->nb_eat)
break ;
action_print(rules, philo->id, "is sleeping");
smart_sleep(rules->time_sleep, rules);
action_print(rules, philo->id, "is thinking");
i++;
}
return (NULL);
}
void exit_launcher(t_rules *rules, t_philosopher *philos)
{
int i;
i = -1;
while (++i < rules->nb_philo)
pthread_join(philos[i].thread_id, NULL);
i = -1;
while (++i < rules->nb_philo)
pthread_mutex_destroy(&(rules->forks[i]));
pthread_mutex_destroy(&(rules->writing));
}
void death_checker(t_rules *r, t_philosopher *p)
{
int i;
int j;
while (!(r->all_ate))
{
i = -1;
while (++i < r->nb_philo && !(r->dieded))
{
pthread_mutex_lock(&(r->meal_check));
if (time_diff(p[i].t_last_meal, timestamp()) > r->time_death)
{
action_print(r, i, "died");
r->dieded = 1;
j = -1;
while (++j < r->nb_philo)
pthread_mutex_unlock(&(r->forks[j]));
}
pthread_mutex_unlock(&(r->meal_check));
usleep(100);
}
if (r->dieded)
break ;
eat_control(r, p);
}
}
int launcher(t_rules *rules)
{
int i;
t_philosopher *phi;
i = 0;
phi = rules->philosophers;
rules->first_timestamp = timestamp();
while (i < rules->nb_philo)
{
if (pthread_create(&(phi[i].thread_id), NULL, p_thread, &(phi[i])))
return (1);
phi[i].t_last_meal = timestamp();
i++;
}
death_checker(rules, rules->philosophers);
exit_launcher(rules, phi);
return (0);
}