-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_philos.c
More file actions
107 lines (98 loc) · 3.09 KB
/
create_philos.c
File metadata and controls
107 lines (98 loc) · 3.09 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
105
106
107
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_philos.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yael-yas <yael-yas@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 17:00:50 by yael-yas #+# #+# */
/* Updated: 2025/04/04 16:03:38 by yael-yas ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
static int setup_resources(t_param *params, pthread_mutex_t **forks,
t_philosopher **philos, pthread_t **threads)
{
int i;
*threads = malloc(sizeof(pthread_t) * params->number_of_philos);
if (!*threads)
return (1);
*forks = malloc(sizeof(pthread_mutex_t) * params->number_of_philos);
if (!*forks)
{
free(*threads);
return (1);
}
i = -1;
while (++i < params->number_of_philos)
pthread_mutex_init(&(*forks)[i], NULL);
pthread_mutex_init(¶ms->check_dead, NULL);
pthread_mutex_init(¶ms->is_print, NULL);
pthread_mutex_init(¶ms->count_mutex, NULL);
*philos = initialize_philos(params, *forks);
if (!*philos)
{
free(*threads);
free(*forks);
return (1);
}
return (0);
}
void destroy_objects(t_param *params, pthread_mutex_t *forks,
t_philosopher *philos, pthread_t *threads)
{
int i;
i = -1;
while (++i < params->number_of_philos)
{
pthread_mutex_destroy(&philos[i].last_meal_mutex);
pthread_mutex_destroy(&forks[i]);
}
pthread_mutex_destroy(¶ms->is_print);
pthread_mutex_destroy(¶ms->count_mutex);
pthread_mutex_destroy(¶ms->check_dead);
free_elements(threads, forks, philos);
}
static int run_simulation(t_philosopher *philos, pthread_t *threads,
int num_philos)
{
int i;
pthread_t monitor_mutex;
i = -1;
philos->params->start_time = get_time();
while (++i < num_philos)
if (pthread_create(&threads[i], NULL, routine, &philos[i]))
return (1);
pthread_create(&monitor_mutex, NULL, ft_monitor, philos);
i = -1;
pthread_join(monitor_mutex, NULL);
while (++i < num_philos)
pthread_join(threads[i], NULL);
return (0);
}
int create_threads(int argc, char **argv)
{
t_param params;
pthread_mutex_t *forks;
pthread_t *threads;
t_philosopher *philos;
forks = NULL;
threads = NULL;
philos = NULL;
params = create_struct(argc, argv);
if (setup_resources(¶ms, &forks, &philos, &threads))
return (1);
if (params.number_of_philos == 1)
{
one_philo(philos, ¶ms, forks, threads);
destroy_objects(¶ms, forks, philos, threads);
return (0);
}
if (run_simulation(philos, threads, params.number_of_philos))
{
destroy_objects(¶ms, forks, philos, threads);
return (1);
}
destroy_objects(¶ms, forks, philos, threads);
return (0);
}