-
Notifications
You must be signed in to change notification settings - Fork 0
/
cputracker.c
182 lines (158 loc) · 4.33 KB
/
cputracker.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/// @file cputracker.c
#include "cputracker.h"
#include "analyzer.h"
#include "printer.h"
#include "reader.h"
int g_nproc = 0;
static struct proc_stat *g_dataBuffer[BUFFER_SIZE];
pthread_mutex_t g_dataBufferMutex = PTHREAD_MUTEX_INITIALIZER;
sem_t g_dataFilledSpaceSemaphore;
sem_t g_dataLeftSpaceSemaphore;
static unsigned long *g_printBuffer[BUFFER_SIZE];
pthread_mutex_t g_printBufferMutex = PTHREAD_MUTEX_INITIALIZER;
sem_t g_printFilledSpaceSemaphore;
sem_t g_printLeftSpaceSemaphore;
static pthread_t readerThreadId;
static pthread_t analyzerThreadId;
static pthread_t printerThreadId;
static int working[THREADS];
static pthread_mutex_t watchdogMutex = PTHREAD_MUTEX_INITIALIZER;
/**
* @brief Signal handler
* @param signum signal number
*/
static void handler(int signum) {
(void)signum;
pthread_cancel(readerThreadId);
pthread_cancel(printerThreadId);
pthread_cancel(analyzerThreadId);
}
/**
* @brief Function retrieving the number of threads
*
* @param signum signal number
*
* @return 0 if the number of threads has been retrieved else -1
*/
static int get_nproc(int *nproc) {
*nproc = (int)sysconf(_SC_NPROCESSORS_ONLN);
if (*nproc == -1) {
return -1;
}
return 0;
}
/**
* @brief Function getting the semaphore value
*
* @param sem semaphore
*
* @return sem value
*/
static int get_semaphore_value(sem_t *sem) {
int sval;
sem_getvalue(sem, &sval);
return sval;
}
/**
* @brief Function getting item from data buffer
*
* @return proc_stat struct from data buffer
*/
struct proc_stat *get_item_from_data_buffer(void) {
int index = get_semaphore_value(&g_dataFilledSpaceSemaphore);
return g_dataBuffer[index];
}
/**
* @brief Function getting item from print buffer
*
* @return proc_stat struct from print buffer
*/
unsigned long *get_item_from_print_buffer(void) {
int index = get_semaphore_value(&g_printFilledSpaceSemaphore);
return g_printBuffer[index];
}
/**
* @brief Notify the watchdog that thread is alive
*
* @param id Thread ID
*/
void notify_watchdog(int id) {
pthread_mutex_lock(&watchdogMutex);
working[id] = 1;
pthread_mutex_unlock(&watchdogMutex);
}
/**
* @brief Watchdog function
*
* This function checks whether the threads are alive.
* If a thread does not speak for 2 seconds the watchdog kills the program
*/
static void watchdog(void) {
while (1) {
sleep(TIMEOUT);
pthread_mutex_lock(&watchdogMutex);
for (int i = 0; i < THREADS; i++) {
if (working[i] == 0) {
pthread_mutex_unlock(&watchdogMutex);
printf("Error: Thread not responding... Terminating\n");
handler(-1);
return;
}
working[i] = 0;
}
pthread_mutex_unlock(&watchdogMutex);
}
}
/**
* @brief Simple test function with asserts
*/
void main_test(void) {
int nproc;
if (get_nproc(&nproc) == 0) {
assert(nproc >= 1);
}
}
int main(void) {
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
if (get_nproc(&g_nproc) == -1) {
exit(EXIT_FAILURE);
}
g_nproc++;
for (int i = 0; i < BUFFER_SIZE; i++) {
g_dataBuffer[i] = malloc((unsigned long)g_nproc * sizeof(struct proc_stat));
g_printBuffer[i] = malloc((unsigned long)g_nproc * sizeof(unsigned long));
if (g_dataBuffer[i] == NULL || g_printBuffer[i] == NULL) {
exit(EXIT_FAILURE);
}
}
if (sem_init(&g_dataFilledSpaceSemaphore, 0, 0) ||
sem_init(&g_dataLeftSpaceSemaphore, 0, BUFFER_SIZE)) {
exit(EXIT_FAILURE);
}
if (sem_init(&g_printFilledSpaceSemaphore, 0, 0) ||
sem_init(&g_printLeftSpaceSemaphore, 0, BUFFER_SIZE)) {
exit(EXIT_FAILURE);
}
pthread_create(&readerThreadId, NULL, reader, NULL);
pthread_create(&analyzerThreadId, NULL, analyzer, NULL);
pthread_create(&printerThreadId, NULL, printer, NULL);
action.sa_handler = handler;
sigaction(SIGTERM, &action, NULL);
watchdog();
pthread_join(readerThreadId, NULL);
pthread_join(analyzerThreadId, NULL);
pthread_join(printerThreadId, NULL);
pthread_mutex_destroy(&g_dataBufferMutex);
sem_destroy(&g_dataFilledSpaceSemaphore);
sem_destroy(&g_dataLeftSpaceSemaphore);
pthread_mutex_destroy(&g_printBufferMutex);
sem_destroy(&g_printFilledSpaceSemaphore);
sem_destroy(&g_printLeftSpaceSemaphore);
pthread_mutex_destroy(&watchdogMutex);
for (int i = 0; i < BUFFER_SIZE; i++) {
free(g_dataBuffer[i]);
free(g_printBuffer[i]);
}
return 0;
}