Skip to content

Commit dc650a8

Browse files
committed
Add analyzer thread
1 parent 837f7e2 commit dc650a8

File tree

8 files changed

+53
-11
lines changed

8 files changed

+53
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ reader
66
reader.o
77
cputracker.o
88
cputracker
9+
analyzer.o

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
CC = gcc -g
22
CFLAGS = -O3 -Wall -Werror
33

4-
OBJS = reader.o cputracker.o
4+
OBJS = reader.o cputracker.o analyzer.o
55

66
reader: $(OBJS)
77
$(CC) $(CFLAGS) -o cputracker $(OBJS)
88

99
reader.o: reader.c reader.h cputracker.h
10+
analyzer.o: analyzer.c analyzer.h cputracker.h
1011
cputracker.o: cputracker.c reader.h cputracker.h
1112

1213
format:

analyzer.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "analyzer.h"
2+
#include "cputracker.h"
3+
#include <stdio.h>
4+
int x = 0;
5+
6+
void *analyzer() {
7+
while (1) {
8+
sem_wait(&g_filledSpaceSemaphore);
9+
pthread_mutex_lock(&g_bufferMutex);
10+
11+
struct proc_stat *stats = remove_item();
12+
13+
pthread_mutex_unlock(&g_bufferMutex);
14+
sem_post(&g_leftSpaceSemaphore);
15+
16+
for (int i = 0; i < g_nproc; i++) {
17+
printf("%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", stats[i].name,
18+
stats[i].user, stats[i].nice, stats[i].system, stats[i].idle,
19+
stats[i].iowait, stats[i].irq, stats[i].softirq, stats[i].steal,
20+
stats[i].guest, stats[i].guest_nice);
21+
}
22+
free(stats);
23+
}
24+
}

analyzer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#ifndef ANALYZER_H
2+
#define ANALYZER_H
3+
4+
void *analyzer();
5+
#endif

cputracker.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "cputracker.h"
2+
#include "analyzer.h"
23
#include "reader.h"
3-
#include <stdio.h>
4-
#include <stdlib.h>
54

65
int g_nproc = 0;
76
struct proc_stat *g_buffer[BUFFER_SIZE];
@@ -33,6 +32,15 @@ int put_item(struct proc_stat *stats) {
3332
return 0;
3433
}
3534

35+
struct proc_stat *remove_item() {
36+
int indexToRemove = get_semaphore_value(&g_filledSpaceSemaphore);
37+
if (indexToRemove < 0) {
38+
return NULL;
39+
}
40+
struct proc_stat *stats = g_buffer[indexToRemove];
41+
return stats;
42+
}
43+
3644
int main() {
3745
if (get_nproc(&g_nproc) == -1) {
3846
exit(EXIT_FAILURE);
@@ -44,6 +52,11 @@ int main() {
4452
}
4553

4654
g_nproc++;
47-
reader();
55+
pthread_t readerThreadId;
56+
pthread_t analyzerThreadId;
57+
pthread_create(&readerThreadId, NULL, reader, NULL);
58+
pthread_create(&analyzerThreadId, NULL, analyzer, NULL);
59+
pthread_join(readerThreadId, NULL);
60+
pthread_join(analyzerThreadId, NULL);
4861
return 0;
4962
}

cputracker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <stdio.h>
99
#include <stdlib.h>
1010
#include <string.h>
11+
#include <unistd.h>
1112

1213
#define BUFFER_SIZE 100
1314

@@ -41,4 +42,5 @@ extern sem_t g_leftSpaceSemaphore;
4142
int get_nproc();
4243
int get_semaphore_value(sem_t *sem);
4344
int put_item(struct proc_stat *stats);
45+
struct proc_stat *remove_item();
4446
#endif

reader.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "reader.h"
22
#include "cputracker.h"
3-
#include <pthread.h>
4-
#include <semaphore.h>
5-
#include <stdio.h>
63

74
struct proc_stat *get_proc_stats() {
85
FILE *file = fopen("/proc/stat", "r");
@@ -28,11 +25,11 @@ struct proc_stat *get_proc_stats() {
2825
return stats;
2926
}
3027

31-
void reader() {
28+
void *reader() {
3229
struct proc_stat *stats = NULL;
3330
while (1) {
3431
if ((stats = get_proc_stats()) == NULL) {
35-
return;
32+
return NULL;
3633
}
3734

3835
sem_wait(&g_leftSpaceSemaphore);

reader.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#ifndef READER_H
22
#define READER_H
3-
#include <unistd.h>
43

5-
void reader();
4+
void *reader();
65
struct proc_stat *get_proc_stats();
76

87
#endif

0 commit comments

Comments
 (0)