Skip to content

Commit 34f38f6

Browse files
committed
Add signal handler
1 parent 99f04e2 commit 34f38f6

File tree

8 files changed

+38
-11
lines changed

8 files changed

+38
-11
lines changed

.gdb_history

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
run
2+
next
3+
next
4+
quit
5+
quit
6+
run
7+
quit
8+
quit
9+
run
10+
quit
11+
run
12+
quit

analyzer.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "cputracker.h"
33

44
static unsigned long average_cpu_usage(struct proc_stat prev,
5-
struct proc_stat current) {
5+
struct proc_stat current) {
66
unsigned long prevIdle = prev.idle + prev.iowait;
77
unsigned long idle = current.idle + current.iowait;
88

@@ -20,11 +20,13 @@ static unsigned long average_cpu_usage(struct proc_stat prev,
2020
return (total - idle) * 100 / total;
2121
}
2222

23-
void *analyzer(void) {
23+
void *analyzer(void *arg) {
2424
struct proc_stat *prev = malloc((unsigned long)g_nproc * sizeof(struct proc_stat));
2525
unsigned long *avg = malloc((unsigned long)g_nproc * sizeof(unsigned long));
2626
unsigned long *bufforAvg;
2727
struct proc_stat *stats = NULL;
28+
pthread_cleanup_push(free, prev);
29+
pthread_cleanup_push(free, avg);
2830
while (1) {
2931
sem_wait(&g_dataFilledSpaceSemaphore);
3032

@@ -53,6 +55,7 @@ void *analyzer(void) {
5355

5456
sem_post(&g_printFilledSpaceSemaphore);
5557
}
56-
free(prev);
57-
free(avg);
58+
pthread_cleanup_pop(1);
59+
pthread_cleanup_pop(1);
60+
return arg;
5861
}

analyzer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
#define ANALYZER_H
33
#include "cputracker.h"
44

5-
void *analyzer(void);
5+
void *analyzer(void *arg);
66
#endif

cputracker.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "printer.h"
44
#include "reader.h"
55

6-
volatile sig_atomic_t done = 0;
7-
86
int g_nproc = 0;
97
static struct proc_stat *g_dataBuffer[BUFFER_SIZE];
108
pthread_mutex_t g_dataBufferMutex = PTHREAD_MUTEX_INITIALIZER;
@@ -20,6 +18,12 @@ static pthread_t readerThreadId;
2018
static pthread_t analyzerThreadId;
2119
static pthread_t printerThreadId;
2220

21+
void handler(int signum) {
22+
pthread_cancel(readerThreadId);
23+
pthread_cancel(printerThreadId);
24+
pthread_cancel(analyzerThreadId);
25+
}
26+
2327
static int get_nproc(int *nproc) {
2428
*nproc = (int)sysconf(_SC_NPROCESSORS_ONLN);
2529
if (*nproc == -1) {
@@ -72,6 +76,11 @@ int main(void) {
7276
pthread_create(&analyzerThreadId, NULL, analyzer, NULL);
7377
pthread_create(&printerThreadId, NULL, printer, NULL);
7478

79+
struct sigaction action;
80+
memset(&action, 0, sizeof(struct sigaction));
81+
action.sa_handler = handler;
82+
sigaction(SIGTERM, &action, NULL);
83+
7584
pthread_join(readerThreadId, NULL);
7685
pthread_join(analyzerThreadId, NULL);
7786
pthread_join(printerThreadId, NULL);

printer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "printer.h"
22

3-
void *printer(void) {
3+
void *printer(void *arg) {
44
unsigned long *avg = NULL;
55
while (1) {
66
sem_wait(&g_printFilledSpaceSemaphore);
@@ -22,4 +22,5 @@ void *printer(void) {
2222
sem_post(&g_printLeftSpaceSemaphore);
2323
sleep(1);
2424
}
25+
return arg;
2526
}

printer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
#define PRINTER_H
33
#include "cputracker.h"
44

5-
void *printer(void);
5+
void *printer(void *arg);
66
#endif

reader.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static int get_proc_stats(struct proc_stat *stats) {
2020
return 0;
2121
}
2222

23-
void *reader(void) {
23+
void *reader(void *arg) {
2424
struct proc_stat *stats = NULL;
2525
while (1) {
2626

@@ -32,6 +32,7 @@ void *reader(void) {
3232

3333
if (get_proc_stats(stats) == -1) {
3434
pthread_mutex_unlock(&g_dataBufferMutex);
35+
sem_post(&g_dataLeftSpaceSemaphore);
3536
continue;
3637
}
3738

@@ -40,4 +41,5 @@ void *reader(void) {
4041
sem_post(&g_dataFilledSpaceSemaphore);
4142
usleep(READ_DELAY);
4243
}
44+
return arg;
4345
}

reader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
#define READ_DELAY 50000
66

7-
void *reader(void);
7+
void *reader(void *arg);
88
#endif

0 commit comments

Comments
 (0)