-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_process.c
114 lines (107 loc) · 3.62 KB
/
ft_process.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_process.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edjavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/12 18:19:37 by edjavid #+# #+# */
/* Updated: 2021/11/25 22:52:50 by edjavid ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_philosopher.h"
int print_action(t_philo *philo, char *action)
{
pthread_mutex_lock(&philo->data->death_mutex);
if (philo->data->is_philo_dead == TRUE)
{
pthread_mutex_unlock(&philo->data->death_mutex);
return (FAILURE);
}
pthread_mutex_unlock(&philo->data->death_mutex);
pthread_mutex_lock(&philo->data->print_action);
printf("%ld Philosopher %d %s\n", actual_time() - philo->data->initial_time,
philo->id, action);
pthread_mutex_unlock(&philo->data->print_action);
return (SUCCESS);
}
int ft_sleep_think(t_philo *clone)
{
pthread_mutex_lock(&clone->data->eat_mutex);
clone->start_eating = TRUE;
pthread_mutex_unlock(&clone->data->eat_mutex);
clone->nb_eat++;
print_action(clone, PINK"started sleeping"END);
if (clone->data->ttd < clone->data->tts)
{
ft_usleep(clone->data->ttd);
print_action(clone, RED "died" END);
is_philo_dead(clone->data, TRUE);
return (FAILURE);
}
if (clone->data->optionnal == TRUE
&& clone->nb_eat == clone->data->cycle)
return (FAILURE);
clone->has_slept = TRUE;
ft_usleep(clone->data->tts);
print_action(clone, YELLOW "started thinking"END);
return (SUCCESS);
}
void ft_fork_eat(t_philo *clone)
{
pthread_mutex_lock(clone->right_fork);
print_action(clone, CYAN "has taken a fork"END);
pthread_mutex_lock(&clone->data->eat_mutex);
clone->start_eating = TRUE;
clone->eaten_time = actual_time();
pthread_mutex_unlock(&clone->data->eat_mutex);
print_action(clone, BLUE "started eating" END);
if (clone->data->ttd < clone->data->tte)
{
ft_usleep(clone->data->ttd);
print_action(clone, RED "died" END);
is_philo_dead(clone->data, TRUE);
}
ft_usleep(clone->data->tte);
pthread_mutex_unlock(clone->right_fork);
pthread_mutex_unlock(&clone->left_fork);
}
int ft_eating_time(t_philo *clone)
{
pthread_mutex_lock(&clone->left_fork);
print_action(clone, CYAN"has taken a fork"END);
if (!clone->right_fork)
{
ft_usleep(clone->data->ttd);
print_action(clone, RED "died" END);
pthread_mutex_unlock(&clone->left_fork);
is_philo_dead(clone->data, TRUE);
return (FAILURE);
}
ft_fork_eat(clone);
return (SUCCESS);
}
void *ft_live(void *philo)
{
t_philo *clone;
clone = (t_philo *)philo;
if (clone->id % 2 == 0)
ft_usleep(clone->data->tte / 10);
pthread_create(&clone->death, NULL, death_upcoming, (void *)clone);
while (is_philo_dead(clone->data, FALSE) == FALSE)
{
if ((clone->start_eating == FALSE)
&& (ft_eating_time(clone) == FAILURE))
return (NULL);
if ((clone->has_slept == FALSE) && (ft_sleep_think(clone) == FAILURE))
return (NULL);
pthread_mutex_lock(&clone->data->sleep_think_mutex);
if (clone->start_eating && clone->has_slept)
{
clone->start_eating = FALSE;
clone->has_slept = FALSE;
}
pthread_mutex_unlock(&clone->data->sleep_think_mutex);
}
pthread_exit(NULL);
}