-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphilo_utils.c
55 lines (47 loc) · 1.55 KB
/
philo_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: azaghlou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 15:07:07 by azaghlou #+# #+# */
/* Updated: 2023/04/27 00:51:14 by azaghlou ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
long timenow(void)
{
struct timeval tv;
gettimeofday(&tv, 0);
return ((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
}
void my_usleep(int i)
{
long init_time;
init_time = timenow();
while (timenow() < init_time + i)
usleep(200);
return ;
}
int print(t_inf *p, char *s)
{
pthread_mutex_lock(p->print_mut);
pthread_mutex_lock(p->dead_mut);
if (p->dead[0] > 0)
{
pthread_mutex_unlock(p->dead_mut);
return (1);
}
pthread_mutex_unlock(p->dead_mut);
printf("%ld %d %s", timenow() - p->initial_time, p->id + 1, s);
pthread_mutex_unlock(p->print_mut);
return (0);
}
int num_philos(int i, int j)
{
static int sttc;
if (j == 1)
sttc = i - 1;
return (sttc);
}