-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils3.c
79 lines (71 loc) · 2.3 KB
/
utils3.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: crebelo- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/24 22:19:10 by crebelo- #+# #+# */
/* Updated: 2024/05/02 21:23:38 by crebelo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int took_too_long(t_philosophers *philo)
{
pthread_mutex_lock(&controler()->time);
if ((current_time() - philo->last_meal >= controler()->die_timer))
{
pthread_mutex_unlock(&controler()->time);
kill_philo(philo);
return (1);
}
pthread_mutex_unlock(&controler()->time);
return (0);
}
int print_logs(char *str, t_philosophers *philo)
{
unsigned int fixed_time;
if (stop_dinner())
return (0);
pthread_mutex_lock(&controler()->printer);
pthread_mutex_lock(&controler()->time);
fixed_time = controler()->start_time - philo->start_time;
printf(str, current_time()
- (philo->start_time - fixed_time), philo->id);
pthread_mutex_unlock(&controler()->time);
pthread_mutex_unlock(&controler()->printer);
return (1);
}
int eating(t_philosophers *philo)
{
struct timeval tv;
unsigned int start_time;
unsigned int current_time;
if (!gettimeofday(&tv, NULL))
{
start_time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
while (1)
{
if (gettimeofday(&tv, NULL))
return (1);
current_time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
if (current_time >= start_time + philo->eat_timer)
break ;
usleep(100);
}
}
else
return (1);
pthread_mutex_unlock(&controler()->forks[philo->lfork].fork);
pthread_mutex_unlock(&controler()->forks[philo->rfork].fork);
return (0);
}
void wait_to_eat(int eat, int sleep)
{
int res;
res = eat - sleep;
if (res < 0)
usleep(500);
else
usleep(res + 500);
}