-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.c
116 lines (108 loc) · 3.48 KB
/
actions.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
115
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* actions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: crebelo- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/20 10:54:42 by crebelo- #+# #+# */
/* Updated: 2024/05/02 20:34:00 by crebelo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int grab_forks(t_philosophers *philo)
{
pthread_mutex_lock(&controler()->forks[philo->rfork].fork);
if (!print_logs("%d %d has taken a fork\n", philo)
|| controler()->max_philos == 1)
{
pthread_mutex_unlock(&controler()->forks[philo->rfork].fork);
return (0);
}
pthread_mutex_lock(&controler()->forks[philo->lfork].fork);
if (!print_logs("%d %d has taken a fork\n", philo))
{
pthread_mutex_unlock(&controler()->forks[philo->lfork].fork);
pthread_mutex_unlock(&controler()->forks[philo->rfork].fork);
return (0);
}
return (1);
}
int philo_eat(t_philosophers *philo)
{
if (!grab_forks(philo))
return (0);
if (!print_logs("%d %d is eating\n", philo))
{
pthread_mutex_unlock(&controler()->forks[philo->lfork].fork);
pthread_mutex_unlock(&controler()->forks[philo->rfork].fork);
return (0);
}
pthread_mutex_lock(&controler()->time);
philo->last_meal = current_time();
pthread_mutex_unlock(&controler()->time);
if (eating(philo))
{
pthread_mutex_unlock(&controler()->forks[philo->lfork].fork);
pthread_mutex_unlock(&controler()->forks[philo->rfork].fork);
return (0);
}
philo->meals_ate++;
pthread_mutex_lock(&controler()->meals);
if (philo->meals_ate == philo->max_meals)
controler()->all_philos_ate++;
pthread_mutex_unlock(&controler()->meals);
return (1);
}
int philo_sleep(t_philosophers *philo)
{
struct timeval tv;
unsigned int start_time;
unsigned int current_time;
if (!print_logs("%d %d is sleeping\n", philo))
return (0);
if (gettimeofday(&tv, NULL))
return (0);
start_time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
while (1)
{
if (gettimeofday(&tv, NULL))
return (0);
current_time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
if (current_time >= start_time + philo->sleep_timer)
break ;
usleep(100);
}
return (1);
}
int kill_philo(t_philosophers *philo)
{
unsigned int fixed_time;
pthread_mutex_lock(&controler()->waiter);
if (!controler()->stop_dinner)
{
controler()->stop_dinner = 1;
pthread_mutex_unlock(&controler()->waiter);
pthread_mutex_lock(&controler()->printer);
pthread_mutex_lock(&controler()->time);
fixed_time = controler()->start_time - philo->start_time;
printf("%d %d died\n",
(current_time() - (philo->start_time - fixed_time)), philo->id);
pthread_mutex_unlock(&controler()->time);
pthread_mutex_unlock(&controler()->printer);
}
else
pthread_mutex_unlock(&controler()->waiter);
return (1);
}
int stop_dinner(void)
{
pthread_mutex_lock(&controler()->waiter);
if (controler()->stop_dinner)
{
pthread_mutex_unlock(&controler()->waiter);
return (1);
}
pthread_mutex_unlock(&controler()->waiter);
return (0);
}