-
Notifications
You must be signed in to change notification settings - Fork 0
/
philo.c
71 lines (64 loc) · 1.52 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
#include "philo.h"
void take_fork(t_philo *philo)
{
pthread_mutex_lock(&(philo->left_fork));
pthread_mutex_lock(&philo->datas->pprint);
printf("%llu %d has taken a fork \n",
get_tick_count() - philo->datas->start_time, philo->who);
pthread_mutex_unlock(&philo->datas->pprint);
one_philo(philo);
pthread_mutex_lock((philo->right_fork));
pthread_mutex_lock(&philo->datas->pprint);
printf("%llu %d has taken a fork \n",
get_tick_count() - philo->datas->start_time, philo->who);
pthread_mutex_unlock(&philo->datas->pprint);
}
void ft_sleep_time(int ttime, t_philo *philo)
{
t_time time;
time = get_tick_count();
while (get_tick_count() - time < (t_time)ttime && !check(philo))
usleep(100);
}
void free_data(t_data *data)
{
long i;
i = -1;
while (++i < data->total_philos)
{
pthread_mutex_destroy(&(data->philo[i].left_fork));
usleep(50);
}
free(data->philo);
pthread_mutex_destroy(&(data->dead));
pthread_mutex_destroy(&(data->m_stop));
pthread_mutex_destroy(&(data->m_eat));
pthread_mutex_destroy(&(data->pprint));
free(data);
}
int main(int argc, char **argv)
{
t_data *data;
if (argc < 5 || argc > 6)
{
printf("Invalid arguments.\n");
exit(0);
}
data = malloc(sizeof(t_data));
ft_set_args(argc, argv, data);
data->philo = malloc(sizeof(t_philo) * data->total_philos);
ft_set_philos(data);
while (1)
{
if (ft_dead_check(data->philo) != -1)
break ;
if (data->eat_count_max == -2)
{
printf("There's no food left in the pot. \n");
break ;
}
usleep(500);
}
free_data(data);
return (0);
}