-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphilo_func2.c
More file actions
58 lines (55 loc) · 1.89 KB
/
philo_func2.c
File metadata and controls
58 lines (55 loc) · 1.89 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_func2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jaehwkim <jaehwkim@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/04 15:26:48 by jaehwkim #+# #+# */
/* Updated: 2022/07/04 21:58:34 by jaehwkim ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void philo_print(t_philo *philo, int state)
{
pthread_mutex_lock(philo->print);
printf("%lld %d ", get_time() - philo->info->time_of_start, philo->number);
if (state == EAT)
{
printf("is eating");
if (philo->info->finish_line)
printf(" - have eaten %d times", philo->eat_cnt);
printf("\n");
}
else if (state == SLEEP)
printf("is sleeping\n");
else if (state == THINK)
printf("is thinking\n");
else if (state == FORK_L)
printf("has taken fork on the left\n");
else if (state == FORK_R)
printf("has taken fork on the right\n");
else if (state == DEAD)
{
printf("died\n");
return ;
}
pthread_mutex_unlock(philo->print);
}
void philo_take_fork(t_philo *philo)
{
if (philo->number % 2 == 0)
{
pthread_mutex_lock(philo->left_fork);
philo_print(philo, FORK_L);
pthread_mutex_lock(philo->right_fork);
philo_print(philo, FORK_R);
}
else
{
pthread_mutex_lock(philo->right_fork);
philo_print(philo, FORK_R);
pthread_mutex_lock(philo->left_fork);
philo_print(philo, FORK_L);
}
}