-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphilo_init.c
127 lines (119 loc) · 3.72 KB
/
philo_init.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alvutina <alvutina@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/29 11:26:15 by alvutina #+# #+# */
/* Updated: 2024/07/29 11:26:15 by alvutina ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
/*The `check_args` function verifies the number of arguments, ensures the sixth
argument (if present) is not empty, and checks that all arguments are
valid digits.*/
int check_args(int argc, char **argv)
{
if (argc != 5 && argc != 6)
{
printf("%s\n", ERROR2);
printf("%s\n", ERROR3);
return (1);
}
if (argc == 6)
{
if (argv[5][0] == '\0')
{
printf("%s", ERROR1);
return (1);
}
}
if (check_digit(argv))
{
printf("%s", ERROR1);
return (1);
}
return (0);
}
/*The `check_error` function validates simulation parameters, ensuring
all values are non-negative.*/
int check_error(t_dining_simulation *store, char **argv)
{
if (store->time_sleep < 0 || store->time_eat < 0 || store->time_die < 0
|| store->num_philo < 0)
return (1);
if (argv[5])
{
store->meal_limit = ft_atoi(argv[5]);
if (store->meal_limit < 0)
return (1);
}
else
store->meal_limit = false;
return (0);
}
/*The initialization function sets up the simulation, including configuring
parameters, allocating memory, and initializing mutexes.
It returns 1 on failure and 0 on success.*/
int initialization(char **argv, t_dining_simulation *store)
{
int i;
i = -1;
store->error = false;
store->finish_sim = false;
store->end_meal = 0;
store->num_philo = ft_atoi(argv[1]);
store->time_die = ft_atoi(argv[2]);
store->time_eat = ft_atoi(argv[3]);
store->time_sleep = ft_atoi(argv[4]);
if (check_error(store, argv))
{
printf("%s", ERROR1);
return (free(store), 1);
}
store->philo = ft_calloc(store->num_philo, (sizeof(t_philo)));
store->fork = ft_calloc(store->num_philo, (sizeof(pthread_mutex_t)));
pthread_mutex_init(&store->checks, NULL);
while (++i < store->num_philo)
pthread_mutex_init(&store->fork[i], NULL);
init_philosophers(store);
store->time_start = timestart();
return (0);
}
/*function initializes the philosopher array in the simulation,
setting each philosopher's ID, fork indices, and other attributes.*/
void init_philosophers(t_dining_simulation *store)
{
int i;
i = 0;
while (i < store->num_philo)
{
store->philo[i].id_num = i + 1;
store->philo[i].meal_eaten = 0;
store->philo[i].last_meal = 0;
store->philo[i].left_fork = i;
store->philo[i].right_fork = (i + 1) % store->num_philo;
store->philo[i].args = store;
i++;
}
}
/*Function cleans up resources by joining philosopher threads,
destroying mutexes, and freeing allocated memory.*/
void philo_destroy(t_dining_simulation *store)
{
int i;
i = 0;
if (store->num_philo > 1)
{
while (i < store->num_philo)
pthread_join(store->philo[i++].thread, NULL);
}
i = 0;
while (i < store->num_philo)
pthread_mutex_destroy(&store->fork[i++]);
pthread_mutex_destroy(&store->checks);
free(store->philo);
free(store->fork);
free(store);
}