-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphilo_jobs.c
44 lines (41 loc) · 1.65 KB
/
philo_jobs.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_jobs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rtavabil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/21 18:47:14 by rtavabil #+# #+# */
/* Updated: 2024/03/21 18:57:44 by rtavabil ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
void eat(t_philo *philo)
{
ft_mutex(&philo->first->fork, "lock");
ft_print(philo, "fork");
ft_mutex(&philo->second->fork, "lock");
ft_print(philo, "fork");
philo->eat_count++;
ft_mutex(&philo->mutex, "lock");
philo->last_eat = gettime("milli");
ft_mutex(&philo->mutex, "unlock");
ft_print(philo, "eat");
ft_usleep(philo->args->t_eat, philo->args);
if ((philo->eat_count > 0) && (philo->eat_count == philo->args->must_eat))
{
ft_mutex(&philo->mutex, "lock");
philo->full = 1;
ft_mutex(&philo->mutex, "unlock");
}
ft_mutex(&philo->first->fork, "unlock");
ft_mutex(&philo->second->fork, "unlock");
}
void think(t_philo *philo)
{
ft_print(philo, "think");
if (philo->args->num_phil % 2 == 1)
{
ft_usleep(philo->args->t_eat / 2, philo->args);
}
}