blob: 534ccc4be37707b5f26903e535d028c008c25d95 (
plain)
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
|
#include "codexion.h"
# include <stdlib.h> // for atoi(), malloc()
/*
fork's last used is a negative value of waiting time so first philos can
start compiling at the start of the simulation
*/
t_fork *create_fork(t_sim *sim)
{
t_fork *fork;
t_queue pq;
fork = (t_fork *)malloc(sizeof(t_fork));
if (!fork)
{
free_forks(sim);
free(sim);
return (NULL);
}
fork->last_released = -1 * sim->fork_cooldown;
fork->ticket_next = 1;
fork->sim = sim;
pq.size = 0;
fork->pq = pq;
return (fork);
}
t_fork **create_arr_forks(t_sim *sim)
{
t_fork **forks;
t_fork *fork;
int i;
int number_of_philos;
i = 0;
number_of_philos = sim->number_of_philos;
forks = (t_fork **)malloc((sizeof(t_fork *) * (number_of_philos
+ 1)));
if (!forks)
{
free(sim);
return (NULL);
}
while (number_of_philos-- > 0)
{
fork = create_fork(sim);
fork->id = i + 1;
forks[i++] = fork;
}
forks[i] = NULL;
sim->number_of_forks_actual = i;
sim->forks = forks;
return (forks);
}
t_philo **assign_forks(t_sim *sim)
{
t_philo **philos;
t_fork **forks;
int number_of_philos;
int i;
philos = sim->philos;
forks = sim->forks;
number_of_philos = sim->number_of_philos;
i = 0;
while (philos[i])
{
philos[i]->left_fork = forks[i];
philos[i]->right_fork = forks[(i + 1) % number_of_philos];
i++;
}
return (philos);
}
|