-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
104 lines (94 loc) · 2.43 KB
/
parser.c
File metadata and controls
104 lines (94 loc) · 2.43 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yaiba <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/03 16:54:37 by melkarmi #+# #+# */
/* Updated: 2021/10/19 02:38:11 by yaiba ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
void init_data(t_data *data, char **av, int ac)
{
data->num_philos = ft_atoi(av[1]);
data->time_todie = ft_atoi(av[2]);
data->time_toeat = ft_atoi(av[3]);
data->time_tosleep = ft_atoi(av[4]);
data->meals = -1;
data->philo_id = 0;
data->run = 1;
data->start_time = get_time();
if (ac == 6)
data->meals = ft_atoi(av[5]);
if (data->meals == 0)
data->meals = -1;
}
int check_args(char **av)
{
int i;
int j;
i = 1;
j = 0;
while (av[i])
{
j = 0;
while (av[i][j])
{
if (ft_isdigit(av[i][j]) == 0)
return (0);
j++;
}
i++;
}
return (1);
}
void init_mutex(t_data *data)
{
int i;
i = 1;
data->fork = NULL;
while (i <= data->num_philos)
{
addbackf(&data->fork, new_fork(i));
if (pthread_mutex_init(&data->fork->flock, NULL) != 0)
print("\n mutex init failed\n", NULL, 0, 2);
i++;
}
if (pthread_mutex_init(&data->lock, NULL) != 0)
print("\n mutex init failed\n", NULL, 0, 2);
if (pthread_mutex_init(&data->alive, NULL) != 0)
print("\n mutex init failed\n", NULL, 0, 2);
pthread_mutex_lock(&data->alive);
}
t_philo *create_philos(t_philo *philo, t_data *data)
{
t_philo *head;
int i;
i = 1;
while (i <= data->num_philos)
{
addback(&philo, new_philo(i, data));
i++;
}
head = philo;
while (head->next)
head = head->next;
head->next = philo;
return (philo);
}
void start_threads(t_philo *philos)
{
int i;
t_philo *philo;
philo = philos;
i = 1;
while (i <= philos->data->num_philos)
{
usleep(10);
pthread_create(&(philo->trd_id), NULL, &routine, philo);
i++;
philo = philo->next;
}
}