-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracking.c
68 lines (61 loc) · 1.51 KB
/
tracking.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
#include "philo.h"
int getting_thread_info(struct s_vars *var, struct s_track *scope, int i)
{
pthread_mutex_lock(var->mtx);
scope->status = var->status[i];
scope->thr = var->ids[i];
pthread_mutex_unlock(var->mtx);
return (1);
}
int terminate_thread_signal(struct s_vars *var, int num, int sig)
{
pthread_mutex_lock(var->mtx);
var->status[num] = sig;
pthread_mutex_unlock(var->mtx);
return (1);
}
int waiting_thread(struct s_vars *var, struct s_track *scope, int i)
{
pthread_join(scope->thr, NULL);
pthread_mutex_lock(var->mtx);
var->status[i] = JOINED;
pthread_mutex_unlock(var->mtx);
scope->joined++;
return (1);
}
int is_dead(struct s_vars *var, int i)
{
struct timeval current_time;
int res;
gettimeofday(¤t_time, NULL);
pthread_mutex_lock(var->mtx);
res = var->start[i].tv_sec && var->start[i].tv_usec
&& diff(¤t_time, &var->start[i]) >= var->die;
pthread_mutex_unlock(var->mtx);
return (res);
}
int tracking(struct s_vars *var, int n)
{
struct s_track scope;
int i;
memset(&scope, 0, sizeof(struct s_track));
while (scope.joined != n && scope.exiting < 400)
{
(i = -1) && usleep(4000);
while (++i < n)
{
getting_thread_info(var, &scope, i);
scope.exiting && terminate_thread_signal(var, i, QUIT);
scope.exiting && ++scope.exiting;
if (!scope.exiting && is_dead(var, i))
{
print_status(var, "died", i);
terminate_thread_signal(var, i, DEAD);
scope.exiting = 1;
}
if (scope.status == DONE)
waiting_thread(var, &scope, i);
}
}
return (0);
}