-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphilo.c
96 lines (86 loc) · 2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ytaya <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/06 17:14:50 by ytaya #+# #+# */
/* Updated: 2022/02/07 23:45:31 by ytaya ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
t_philosopher *ft_lstnew(int id, t_program_args *args)
{
t_philosopher *new;
new = malloc(sizeof(t_philosopher));
pthread_mutex_init(&new->fork, NULL);
new->id = id;
new->next = new;
new->n_meals = 0;
new->args = args;
new->iseating = 0;
return (new);
}
t_philosopher *ft_lstlast(t_philosopher *head)
{
t_philosopher *tmp;
tmp = head;
while (1 && head)
{
head = head->next;
if (head->next == tmp)
return (head);
}
return (head);
}
void ft_lstadd_back(t_philosopher **lst, t_philosopher *new)
{
t_philosopher *last;
if (lst)
{
if (new)
{
last = ft_lstlast(*lst);
last->next = new;
new->next = (*lst);
new->next = (*lst);
}
}
else
(*lst) = new;
}
void ft_args_init(t_program_args *args, char **argv)
{
args->n_philo = atoi(argv[0]);
args->t_die = atoi(argv[1]);
args->t_eat = atoi(argv[2]);
args->t_sleep = atoi(argv[3]);
args->stop = 0;
if (argv[4])
args->n_meals = atoi(argv[4]);
else
args->n_meals = -1;
args->t_initphilo = ft_time_now();
pthread_mutex_init(&args->write, NULL);
}
int main(int argc, char **argv)
{
t_program_args *args;
t_philosopher *node;
args = malloc(sizeof(t_program_args));
if (argc >= 5 && argc <= 6)
{
ft_args_init(args, &argv[1]);
node = ft_philos_init(args);
ft_create_threads(node);
ft_routine_manager(node);
}
else
{
printf("Usage: ./philo number_of_philosophers time_to_die \
time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]\n");
return (1);
}
return (0);
}