-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
133 lines (123 loc) · 3.63 KB
/
main.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: crebelo- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/24 22:19:10 by crebelo- #+# #+# */
/* Updated: 2024/05/02 21:22:27 by crebelo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int cancel_dinner(t_philosophers *philo)
{
pthread_mutex_lock(&controler()->meals);
if (controler()->all_philos_ate == philo->max_philos)
{
pthread_mutex_unlock(&controler()->meals);
pthread_mutex_lock(&controler()->waiter);
controler()->stop_dinner = 1;
pthread_mutex_unlock(&controler()->waiter);
return (1);
}
pthread_mutex_unlock(&controler()->meals);
pthread_mutex_lock(&controler()->time);
if ((current_time() - philo->last_meal >= controler()->die_timer))
{
pthread_mutex_unlock(&controler()->time);
return (kill_philo(philo));
}
pthread_mutex_unlock(&controler()->time);
return (0);
}
void *dead_philo(void *arg)
{
t_philosophers *philo;
int i;
philo = (t_philosophers *)arg;
if (philo->id % 2 != 0)
usleep(1);
while (1)
{
i = 0;
while (i < philo->max_philos)
{
pthread_mutex_lock(&controler()->philo_on);
if (!philo->on)
{
pthread_mutex_unlock(&controler()->philo_on);
break ;
}
pthread_mutex_unlock(&controler()->philo_on);
usleep(100);
if (cancel_dinner(&(philo[i])))
return (NULL);
i++;
}
}
return (NULL);
}
void *routine(void *arg)
{
t_philosophers *philo;
philo = (t_philosophers *)arg;
pthread_mutex_lock(&controler()->philo_on);
philo->on = true;
pthread_mutex_unlock(&controler()->philo_on);
if (took_too_long(philo))
return (NULL);
if (philo->id % 2 == 0
|| (philo->max_philos % 2 != 0 && philo->id == philo->max_philos))
wait_to_eat((philo->eat_timer * 1000), (philo->sleep_timer * 1000));
while (!stop_dinner())
{
if (!philo_eat(philo))
break ;
if (!philo_sleep(philo))
break ;
print_logs("%lld %d is thinking\n", philo);
wait_to_eat((philo->eat_timer * 1000), (philo->sleep_timer * 1000));
}
return (NULL);
}
int create_threads(t_philosophers *philos)
{
int i;
pthread_t th;
i = 0;
controler()->start_time = current_time();
while (i < controler()->max_philos)
{
if (pthread_create(&philos[i].philo_th, NULL, routine, &philos[i]) != 0)
return (printf("Error: issue with pthread create.\n"));
i++;
}
if (pthread_create(&th, NULL, dead_philo, philos) != 0)
return (printf("Error: issue with pthread create.\n"));
if (pthread_join(th, NULL) != 0)
return (printf("Error: issue with pthread join.\n"));
i = 0;
while (i < controler()->max_philos)
{
if (pthread_join(philos[i].philo_th, NULL) != 0)
return (printf("Error: issue with pthread join.\n"));
i++;
}
return (0);
}
int main(int argc, char **argv)
{
t_philosophers *philos;
if (parsing(argc, argv) != 0)
return (1);
philos = malloc(ft_atoi(argv[1]) * sizeof(t_philosophers));
if (!philos)
return (1);
if (!init_philos(philos, argv))
return (clean_memory(philos));
if (create_threads(philos) != 0)
return (clean_memory(philos));
destroy_mutexes(philos);
return (0);
}