-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·125 lines (110 loc) · 3.11 KB
/
main.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
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kdrissi- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/03 19:59:38 by kdrissi- #+# #+# */
<<<<<<< HEAD
/* Updated: 2021/10/10 00:08:53 by kdrissi- ### ########.fr */
=======
/* Updated: 2021/10/09 13:08:49 by kdrissi- ### ########.fr */
>>>>>>> 10e6f321463b0ef428fe88b13f55d6591c2bc4ad
/* */
/* ************************************************************************** */
#include "philo.h"
void print(t_philo *philo, char *str, int index)
{
t_time currently;
currently = now() - philo->start;
pthread_mutex_lock(&philo->print);
printf("%d time %d id %s\n",currently , index, str);
pthread_mutex_unlock(&philo->print);
}
void zzzz(t_time time)
{
t_time this_time;
this_time = now();
<<<<<<< HEAD
usleep((time * 1000 * 0.85));
while (now() <= this_time + time)
;
=======
usleep(time * 1000 * 0.85);
while (now() < this_time + time)
continue ;
>>>>>>> 10e6f321463b0ef428fe88b13f55d6591c2bc4ad
}
void eating(t_philo *philo, int index)
{
pthread_mutex_lock(philo->fork[index]);
print(philo, "has taken a fork");
pthread_mutex_lock(philo->fork[(index + 1) % philo->nbr); // ta da % philo.nbr;
print(philo, "has taken a fork");
philo->last_time_eated[index] = now();
print(philo, "is eating");
philo->is_eating[index] = 1;
zzzz(philo->time_to_eat);
philo->is_eating[index] = 0;
pthread_mutex_unlock(philo->fork[index]);
pthread_mutex_unlock(philo->fork[(index + 1) % philo->nbr]);
if (philo->check1[index]++ == philo->must_eat_count)
check2++;
}
void sleeping(t_philo *philo, int index)
{
print(philo, "is sleeping", index);
zzzz(philo->time_to_sleep);
}
void *life_of_a_philo(void *philo)
{
int index;
philo = (t_philo*)philo;
index = philo->index;
while (1)
{
eating(philo, index);
sleeping(philo, index);
print(philo, "is thinking", index);
}
}
t_time time_passed(t_time last_time_eated)
{
retun(now() - last_time_eated);
}
void checker(t_philo *philo)
{
int i;
i = 0;
while(1)
{
while (i < philo->nbr)
{
if(must_eat_count >= 0 && philo->check2 >= philo->nbr)
{
pthread_mutex_lock(&philo->print);
return;
}
if(!philo->is_eating[i] && time_passed(philo->last_time_eated[i]) > philo->time_to_die)
{
pthread_mutex_lock(&philo->print);
printf("%d time %d id died\n", now() - philo->start, i);
return;
}
i++;
}
i = 0;
}
}
int main(int ac, char **av)
{
t_philo *philo;
if (!get_input(ac, av, philo))
return(1);
if (!create(philo))
return(1);
checker(philo);
clear_all(philo);
return(0);
}