-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmang_dumb.c
119 lines (110 loc) · 3.06 KB
/
mang_dumb.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mang_dumb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aaljaber <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 20:41:58 by aaljaber #+# #+# */
/* Updated: 2022/04/21 20:52:09 by aaljaber ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void init_dumb(t_philo *dumb, t_main *main, int i)
{
dumb->id = i;
dumb->m_info = main;
dumb->f_left = 0;
dumb->f_right = 0;
dumb->yes_eat = 0;
dumb->yes_sleep = 0;
dumb->yes_think = 0;
dumb->t_now = 0;
dumb->t_crnt = 0;
dumb->t_stop = 0;
dumb->t_start = 0;
dumb->m_info->count = dumb->m_info->num_eat;
if (dumb->m_info->num_eat)
dumb->m_info->count--;
dumb->e = 0;
dumb->t_crnt = 0;
dumb->sim = 0;
dumb->right = dumb->id;
dumb->r = 0;
if (dumb->id == 0)
dumb->left = (dumb->id + dumb->m_info->num_philo) - 1;
else
dumb->left = dumb->id - 1;
dumb->l = 0;
}
void pre_dumb(t_philo *dumb, t_main *main)
{
int i;
i = -1;
while (++i < main->num_philo)
init_dumb(&dumb[i], main, i);
}
void create_dumb(t_main *main, t_philo *dumb)
{
struct timeval now;
int i;
i = -1;
main->l = main->num_philo;
gettimeofday(&now, NULL);
main->t_initial = (now.tv_sec * 1000) + (now.tv_usec / 1000);
if (main->num_philo % 2 != 0)
main->l--;
while (++i < main->l)
{
++i;
pthread_create(&main->philo[i], NULL, &life_begin, &dumb[i]);
}
usleep(1000);
i = -2;
if (main->num_philo % 2 == 0)
main->l--;
else if (main->num_philo % 2 != 0)
main->l++;
while (++i < main->l)
{
++i;
pthread_create(&main->philo[i], NULL, &life_begin, &dumb[i]);
}
}
/* Threads will return 1 in case of death or 0 in case of success
I didn't malloc it cuz it's static ^.^ */
/* I'm using a pointer from the main function
so no static and no malloc needed
|| CHECK IF U NEED MALLOC */
int dumb_exist(t_main *main)
{
t_philo *dumb;
int *death;
int i;
main->philo = (pthread_t *)malloc(sizeof(pthread_t) * main->num_philo);
dumb = (t_philo *)malloc(sizeof(t_philo) * main->num_philo);
pre_dumb(dumb, main);
main->phil = dumb;
create_dumb(main, dumb);
i = -1;
while (++i < main->num_philo)
pthread_join(main->philo[i], (void *)&death);
if (*death == 1 || death == NULL || *death == 0)
{
free(dumb);
free(main->philo);
return (0);
}
return (0);
}
int f_died_lock(t_philo *dumb)
{
pthread_mutex_lock(&dumb->m_info->d_mut);
if (dumb->m_info->f_died)
{
pthread_mutex_unlock(&dumb->m_info->d_mut);
return (0);
}
pthread_mutex_unlock(&dumb->m_info->d_mut);
return (1);
}