-
Notifications
You must be signed in to change notification settings - Fork 0
/
philo.c
executable file
·136 lines (128 loc) · 3.36 KB
/
philo.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
125
126
127
128
129
130
131
132
133
134
135
136
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bposa <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/12 13:28:33 by bposa #+# #+# */
/* Updated: 2024/08/15 16:32:44 by bposa ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int action(t_action act, int arg, char *str, t_philo *p)
{
pthread_mutex_lock(p->dlock);
if (*p->death)
{
if (act == FORK)
pthread_mutex_unlock(p->forkone);
else if (act == FORKEAT)
{
pthread_mutex_unlock(p->forkone);
pthread_mutex_unlock(p->forktwo);
}
pthread_mutex_unlock(p->dlock);
return (DEATH);
}
printer(arg, str, p);
if (act == FORKEAT)
{
if (p->meals)
{
printer(arg, "is eating", p);
increment(&p->meals_had, &p->lmeallock);
lastmealset(p);
}
}
pthread_mutex_unlock(p->dlock);
return (SUCCESS);
}
int routine(t_philo *p)
{
if (p->runs && action(THINK, p->id, "is thinking", p))
return (DEATH);
if (!p->runs && p->id % 2 == 0)
ft_usleep(p->sleep_t / 3, p);
pthread_mutex_lock(p->forkone);
if (action(FORK, p->id, "has taken a fork", p))
return (DEATH);
if (!p->forktwo && lastmealset(p))
return (ifonlyonefork(p));
pthread_mutex_lock(p->forktwo);
if (action(FORKEAT, p->id, "has taken a fork", p))
return (DEATH);
if (ft_usleep(p->eat_t, p) != SUCCESS)
{
dropforks(p);
return (DEATH);
}
dropforks(p);
if (action(SLEEP, p->id, "is sleeping", p) || ft_usleep(p->sleep_t, p))
return (DEATH);
p->runs++;
return (SUCCESS);
}
void life(t_philo *p)
{
setter(&p->ready, SUCCESS, &p->readylock);
while (getter(&p->go, &p->golock) != GO)
usleep(200);
action(THINK, p->id, "is thinking", p);
while (!getter(p->death, p->dlock))
{
if (routine(p))
{
setter(p->death, p->id, p->dlock);
break ;
}
if (getter(&p->meals, &p->lmeallock) != -1
&& getter(&p->meals_had, &p->lmeallock) >= p->meals)
{
break ;
}
}
setter(&p->end, 1, &p->readylock);
}
void butler(t_data *d)
{
int i;
long long int t;
i = -1;
t = 0;
syncing(d);
while (!getter(&d->death, &d->dielock))
{
i = -1;
t = get_time_ms();
while (++i < d->n_philos)
{
if ((lastmealget(d->philo[i]) != 0
&& t - lastmealget(d->philo[i]) >= d->philo[i]->die_t)
|| checker(d, MEAL) == SUCCESS)
{
setter(&d->death, DEATH, &d->dielock);
break ;
}
}
if (i != d->n_philos)
break ;
}
if (checker(d, MEAL) != SUCCESS)
printf("%lld %d died\n", t - d->starttime, d->philo[i]->id);
}
int main(int argc, char **argv)
{
t_data *d;
if (validator(argc, argv) == ERROR)
return (ermsg(EVAL));
d = malloc(sizeof(t_data));
if (!d)
return (ermsg(EMALMUT));
if (initor(argv, d) == ERROR)
return (ermsg(EINIT));
d->initdone = 1;
while (endchecker(d) != SUCCESS)
usleep(200);
return (cleanerr(d, SUCCESS, d->n_philos));
}