-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_extra.c
More file actions
58 lines (52 loc) · 1.9 KB
/
Copy pathutils_extra.c
File metadata and controls
58 lines (52 loc) · 1.9 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_extra.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oabdelka <oabdelka@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/09 12:45:28 by oabdelka #+# #+# */
/* Updated: 2024/12/09 15:08:02 by oabdelka ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosopher.h"
void handle_philosopher_death(t_simulation *simulation, int i)
{
pthread_mutex_lock(&simulation->m_died);
simulation->died = 1;
pthread_mutex_unlock(&simulation->m_died);
pthread_mutex_lock(&simulation->print);
printf(
"%ld %d %s\n",
current_timestamp() - simulation->start_time,
simulation->philo[i].id,
"died."
);
pthread_mutex_unlock(&simulation->print);
}
void destroy_mutexes(t_simulation *simulation)
{
int i;
i = -1;
while (++i < simulation->num_philosophers)
{
pthread_mutex_destroy(&simulation->fork[i].mutex);
}
pthread_mutex_destroy(&simulation->meal);
pthread_mutex_destroy(&simulation->m_end);
pthread_mutex_destroy(&simulation->print);
pthread_mutex_destroy(&simulation->m_died);
pthread_mutex_destroy(&simulation->eat);
free(simulation->philo);
free(simulation->fork);
}
void terminate_simulation(t_simulation *simulation)
{
int i;
i = -1;
while (++i < simulation->num_philosophers)
{
pthread_join(simulation->philo[i].thread, NULL);
}
destroy_mutexes(simulation);
}