-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutine_utils.c
98 lines (90 loc) · 3.17 KB
/
routine_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* routine_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pscala <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/03 16:13:04 by pscala #+# #+# */
/* Updated: 2024/08/03 18:30:02 by pscala ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosopher.h"
void ft_wait_everyone(t_philo *philo)
{
pthread_mutex_lock(&philo->args->mutex_philo_in_queue);
philo->args->philo_in_queue++;
pthread_mutex_unlock(&philo->args->mutex_philo_in_queue);
while (1)
{
pthread_mutex_lock(&philo->args->mutex_philo_in_queue);
if (philo->args->philo_in_queue == philo->args->nb_of_philo)
{
if (philo->args->flag == -1)
{
philo->args->flag = 0;
philo->args->start = get_time();
}
pthread_mutex_lock(&philo->mealsmutex);
philo->last_meal = philo->args->start;
pthread_mutex_unlock(&philo->mealsmutex);
pthread_mutex_unlock(&philo->args->mutex_philo_in_queue);
break ;
}
pthread_mutex_unlock(&philo->args->mutex_philo_in_queue);
usleep(500);
}
}
int philo_pos(t_philo *philo)
{
if (philo->id == philo->args->nb_of_philo)
{
pthread_mutex_lock(&philo->fork);
if (is_he_dead(philo) == -1)
return (pthread_mutex_unlock(&philo->fork), -1);
ft_print(philo, "has taken a fork🍴\n", PURPLE);
pthread_mutex_lock(philo->nextfork);
if (is_he_dead(philo) == -1)
return (pthread_mutex_unlock(&philo->fork),
pthread_mutex_unlock(philo->nextfork), -1);
ft_print(philo, "has taken a fork🍴\n", PURPLE);
}
else
{
pthread_mutex_lock(philo->nextfork);
if (is_he_dead(philo) == -1)
return (pthread_mutex_unlock(philo->nextfork), -1);
ft_print(philo, "has taken a fork🍴\n", PURPLE);
pthread_mutex_lock(&philo->fork);
if (is_he_dead(philo) == -1)
return (pthread_mutex_unlock(&philo->fork),
pthread_mutex_unlock(philo->nextfork), -1);
ft_print(philo, "has taken a fork🍴\n", PURPLE);
}
return (0);
}
int case_one_philo(t_philo *philo)
{
long int time;
time = get_time();
ft_print(philo, "has taken a fork🍴\n", PURPLE);
while (get_time() - time < philo->args->time_to_die)
usleep(100);
return (-1);
}
int is_he_dead(t_philo *philo)
{
pthread_mutex_lock(&philo->args->deadmutex);
if (philo->args->dead == 1)
return (pthread_mutex_unlock(&philo->args->deadmutex), -1);
pthread_mutex_unlock(&philo->args->deadmutex);
return (0);
}
void ft_print(t_philo *philo, char *str, char *color)
{
long int time;
time = get_time() - philo->args->start;
pthread_mutex_lock(&philo->args->printmutex);
printf("%s[%ld] %d %s" RESET, color, time, philo->id, str);
pthread_mutex_unlock(&philo->args->printmutex);
}