-
Notifications
You must be signed in to change notification settings - Fork 0
/
philo.c
88 lines (81 loc) · 2.54 KB
/
philo.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iel-moha <iel-moha@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/16 00:26:44 by iel-moha #+# #+# */
/* Updated: 2022/09/24 18:32:01 by iel-moha ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void error_handling(int i, char *s)
{
if (i != 0)
{
printf("%s failed \n", s);
return ;
}
}
void mutex_print(int v, t_vars *var, char *text)
{
error_handling(pthread_mutex_lock(&var->is_ded), "mutex");
if (var->is_philo_dead != 1)
{
error_handling(pthread_mutex_lock(&var->print), "mutex");
printf("%lld %d %s \n", gettimenow() - var->tstart, v + 1, text);
error_handling(pthread_mutex_unlock(&var->print), "mutex");
}
error_handling(pthread_mutex_unlock(&var->is_ded), "Mutex");
}
void init_mutex(t_vars *var)
{
var->i = 0;
var->forks = malloc(sizeof(pthread_mutex_t) * var->tab[0]);
if (var->forks == NULL)
{
printf("Malloc error \n");
return ;
}
while (var->i < var->tab[0])
{
error_handling(pthread_mutex_init
(&var->forks[var->i], NULL), "Mutex init");
var->i++;
}
error_handling(pthread_mutex_init(&var->print, NULL), "Mutex init");
error_handling(pthread_mutex_init(&var->is_ded, NULL), "Mutex init");
}
void init_mystruct(t_vars *var)
{
var->is_philo_dead = 0;
var->i = 0;
var->tstart = gettimenow();
var->im = 2147483647;
if (var->forks == NULL)
error_malloc();
}
int main(int ac, char **av)
{
t_vars *var;
if ((ac == 5 || ac == 6) && is_digit(av) == 1)
{
var = malloc(sizeof(t_vars) * 1);
var->tab = malloc((ac) * sizeof(int));
if (var == NULL || var->tab == NULL)
error_malloc();
var->result = 1;
var->result = args_atoi(var, av, ac);
init_mystruct(var);
if (var->tab[0] < 1 || var->result == 0 || (ac == 6 && var->tab[4] < 1))
{
printf("Arguments are not valid. Try Again. 2\n");
return (0);
}
init_mutex(var);
create_philo(var);
}
else
printf("Arguments are not valid. Try Again. 3 \n");
}