Skip to content

Commit 99f04e2

Browse files
committed
Fix warnings for clang
1 parent faac42e commit 99f04e2

File tree

9 files changed

+45
-46
lines changed

9 files changed

+45
-46
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
CC = gcc -g
2-
CFLAGS = -O3 -Wall -Werror
2+
CFLAGS = -O3 -Wall -Werror -pthread
3+
# CC = clang
4+
# CFLAGS = -Weverything -pthread
35

46
OBJS = reader.o cputracker.o analyzer.o printer.o
57

analyzer.c

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

4-
unsigned long average_cpu_usage(struct proc_stat prev,
4+
static unsigned long average_cpu_usage(struct proc_stat prev,
55
struct proc_stat current) {
66
unsigned long prevIdle = prev.idle + prev.iowait;
77
unsigned long idle = current.idle + current.iowait;
@@ -20,9 +20,10 @@ unsigned long average_cpu_usage(struct proc_stat prev,
2020
return (total - idle) * 100 / total;
2121
}
2222

23-
void *analyzer() {
24-
struct proc_stat *prev = malloc(g_nproc * sizeof(struct proc_stat));
25-
unsigned long *avg = malloc(g_nproc * sizeof(unsigned long));
23+
void *analyzer(void) {
24+
struct proc_stat *prev = malloc((unsigned long)g_nproc * sizeof(struct proc_stat));
25+
unsigned long *avg = malloc((unsigned long)g_nproc * sizeof(unsigned long));
26+
unsigned long *bufforAvg;
2627
struct proc_stat *stats = NULL;
2728
while (1) {
2829
sem_wait(&g_dataFilledSpaceSemaphore);
@@ -45,13 +46,13 @@ void *analyzer() {
4546

4647
pthread_mutex_lock(&g_printBufferMutex);
4748

48-
unsigned long *bufforAvg = get_item_from_print_buffer();
49-
memcpy(bufforAvg, avg, g_nproc * sizeof(unsigned long));
49+
bufforAvg = get_item_from_print_buffer();
50+
memcpy(bufforAvg, avg, (unsigned long)g_nproc * sizeof(unsigned long));
5051

5152
pthread_mutex_unlock(&g_printBufferMutex);
5253

5354
sem_post(&g_printFilledSpaceSemaphore);
5455
}
5556
free(prev);
5657
free(avg);
57-
}
58+
}

analyzer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
#define ANALYZER_H
33
#include "cputracker.h"
44

5-
void *analyzer();
6-
unsigned long average_cpu_usage(struct proc_stat prev, struct proc_stat next);
7-
#endif
5+
void *analyzer(void);
6+
#endif

cputracker.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,56 @@
33
#include "printer.h"
44
#include "reader.h"
55

6+
volatile sig_atomic_t done = 0;
7+
68
int g_nproc = 0;
7-
struct proc_stat *g_dataBuffer[BUFFER_SIZE];
9+
static struct proc_stat *g_dataBuffer[BUFFER_SIZE];
810
pthread_mutex_t g_dataBufferMutex = PTHREAD_MUTEX_INITIALIZER;
911
sem_t g_dataFilledSpaceSemaphore;
1012
sem_t g_dataLeftSpaceSemaphore;
1113

12-
unsigned long *g_printBuffer[BUFFER_SIZE];
14+
static unsigned long *g_printBuffer[BUFFER_SIZE];
1315
pthread_mutex_t g_printBufferMutex = PTHREAD_MUTEX_INITIALIZER;
1416
sem_t g_printFilledSpaceSemaphore;
1517
sem_t g_printLeftSpaceSemaphore;
1618

17-
pthread_t readerThreadId;
18-
pthread_t analyzerThreadId;
19-
pthread_t printerThreadId;
19+
static pthread_t readerThreadId;
20+
static pthread_t analyzerThreadId;
21+
static pthread_t printerThreadId;
2022

21-
int get_nproc(int *nproc) {
22-
*nproc = sysconf(_SC_NPROCESSORS_ONLN);
23+
static int get_nproc(int *nproc) {
24+
*nproc = (int)sysconf(_SC_NPROCESSORS_ONLN);
2325
if (*nproc == -1) {
2426
return -1;
2527
}
2628
return 0;
2729
}
2830

29-
int get_semaphore_value(sem_t *sem) {
31+
static int get_semaphore_value(sem_t *sem) {
3032
int sval;
3133
sem_getvalue(sem, &sval);
3234
return sval;
3335
}
3436

35-
struct proc_stat *get_item_from_data_buffer() {
37+
struct proc_stat *get_item_from_data_buffer(void) {
3638
int index = get_semaphore_value(&g_dataFilledSpaceSemaphore);
3739
return g_dataBuffer[index];
3840
}
3941

40-
unsigned long *get_item_from_print_buffer() {
42+
unsigned long *get_item_from_print_buffer(void) {
4143
int index = get_semaphore_value(&g_printFilledSpaceSemaphore);
4244
return g_printBuffer[index];
4345
}
4446

45-
int main() {
47+
int main(void) {
4648
if (get_nproc(&g_nproc) == -1) {
4749
exit(EXIT_FAILURE);
4850
}
4951
g_nproc++;
5052

5153
for (int i = 0; i < BUFFER_SIZE; i++) {
52-
g_dataBuffer[i] = malloc(g_nproc * sizeof(struct proc_stat));
53-
g_printBuffer[i] = malloc(g_nproc * sizeof(unsigned long));
54+
g_dataBuffer[i] = malloc((unsigned long)g_nproc * sizeof(struct proc_stat));
55+
g_printBuffer[i] = malloc((unsigned long)g_nproc * sizeof(unsigned long));
5456
if (g_dataBuffer[i] == NULL || g_printBuffer[i] == NULL) {
5557
exit(EXIT_FAILURE);
5658
}
@@ -86,4 +88,4 @@ int main() {
8688
free(g_printBuffer[i]);
8789
}
8890
return 0;
89-
}
91+
}

cputracker.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,17 @@ struct proc_stat {
3333
// operating systems under the control of the Linux kernel).
3434
};
3535

36+
extern volatile sig_atomic_t done;
37+
3638
extern int g_nproc;
37-
extern struct proc_stat *g_dataBuffer[];
3839
extern pthread_mutex_t g_dataBufferMutex;
3940
extern sem_t g_dataFilledSpaceSemaphore;
4041
extern sem_t g_dataLeftSpaceSemaphore;
4142

42-
extern unsigned long *g_printBuffer[BUFFER_SIZE];
4343
extern pthread_mutex_t g_printBufferMutex;
4444
extern sem_t g_printFilledSpaceSemaphore;
4545
extern sem_t g_printLeftSpaceSemaphore;
4646

47-
int get_nproc(int *nproc);
48-
int get_semaphore_value(sem_t *sem);
49-
unsigned long *get_item_from_print_buffer();
50-
struct proc_stat *get_item_from_data_buffer();
51-
#endif
47+
unsigned long *get_item_from_print_buffer(void);
48+
struct proc_stat *get_item_from_data_buffer(void);
49+
#endif

printer.c

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

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

printer.h

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

5-
void *printer();
6-
#endif
5+
void *printer(void);
6+
#endif

reader.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#include "reader.h"
22

3-
int get_proc_stats(struct proc_stat *stats) {
3+
static int get_proc_stats(struct proc_stat *stats) {
44
FILE *file = fopen("/proc/stat", "r");
55
char line[1024];
66
for (int thread = 0; thread < g_nproc; thread++) {
7-
fgets(line, sizeof(line), file);
8-
if (strncmp(line, "cpu", 3) != 0) {
9-
perror("Reading thread info failed");
7+
if (fgets(line, sizeof(line), file) == NULL ||
8+
strncmp(line, "cpu", 3) != 0) {
109
fclose(file);
1110
return -1;
1211
}
@@ -21,7 +20,7 @@ int get_proc_stats(struct proc_stat *stats) {
2120
return 0;
2221
}
2322

24-
void *reader() {
23+
void *reader(void) {
2524
struct proc_stat *stats = NULL;
2625
while (1) {
2726

@@ -41,4 +40,4 @@ void *reader() {
4140
sem_post(&g_dataFilledSpaceSemaphore);
4241
usleep(READ_DELAY);
4342
}
44-
}
43+
}

reader.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@
44

55
#define READ_DELAY 50000
66

7-
int get_proc_stats(struct proc_stat *stats);
8-
void *reader();
9-
10-
#endif
7+
void *reader(void);
8+
#endif

0 commit comments

Comments
 (0)