-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.c
More file actions
92 lines (82 loc) · 2.61 KB
/
monitor.c
File metadata and controls
92 lines (82 loc) · 2.61 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* monitor.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanmetol <sanmetol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/12 13:02:33 by sanmetol #+# #+# */
/* Updated: 2025/02/12 13:03:08 by sanmetol ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void log_status(char *str, t_philo *philo, int id)
{
size_t time;
pthread_mutex_lock(philo->mutex_log);
time = get_current_time_ms() - philo->start_time;
if (!dead_loop(philo))
printf("\033[1;38;2;96;168;235m%zums\033[0m\t \033[1;38;2;255;165;0m\
[%d]\033[0m\t\033[1;38;2;238;232;224m%s\033[0m\n", time, id, str);
pthread_mutex_unlock(philo->mutex_log);
}
static int philo_dead(t_philo *philo, size_t time_to_die)
{
pthread_mutex_lock(philo->mutex_meal);
if (get_current_time_ms() - philo->last_meal_time >= time_to_die)
return (pthread_mutex_unlock(philo->mutex_meal), 1);
pthread_mutex_unlock(philo->mutex_meal);
return (0);
}
static int philos_full(t_philo *philos)
{
int i;
int is_full;
i = 0;
is_full = 0;
if (philos[0].input.n_times_must_eat == -1)
return (0);
while (i < philos[0].input.n_of_philos)
{
pthread_mutex_lock(philos[i].mutex_meal);
if (philos[i].meals_eaten >= philos[i].input.n_times_must_eat)
is_full++;
pthread_mutex_unlock(philos[i].mutex_meal);
i++;
}
if (is_full == philos[0].input.n_of_philos)
{
pthread_mutex_lock(philos[0].mutex_dead);
*philos->dead = 1;
pthread_mutex_unlock(philos[0].mutex_dead);
return (1);
}
return (0);
}
static int check_dead(t_philo *philos)
{
int i;
i = 0;
while (i < philos[0].input.n_of_philos)
{
if (philo_dead(&philos[i], philos[i].input.time_to_die))
{
log_status("died", &philos[i], philos[i].id);
pthread_mutex_lock(philos[0].mutex_dead);
*philos->dead = 1;
pthread_mutex_unlock(philos[0].mutex_dead);
return (1);
}
i++;
}
return (0);
}
void *monitor_philo(void *ptr)
{
t_philo *philos;
philos = (t_philo *)ptr;
while (1)
if (check_dead(philos) == 1 || philos_full(philos) == 1)
break ;
return (ptr);
}