Skip to content

Commit e024f48

Browse files
committed
Add semaphore's
1 parent 25c83c8 commit e024f48

File tree

4 files changed

+74
-19
lines changed

4 files changed

+74
-19
lines changed

cputracker.c

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
11
#include "cputracker.h"
22
#include "reader.h"
3+
#include <stdio.h>
34
#include <stdlib.h>
45

56
int nproc = 0;
7+
struct proc_stat *BUFFER[BUFFER_SIZE];
8+
pthread_mutex_t bufferMutex = PTHREAD_MUTEX_INITIALIZER;
9+
sem_t filledSpaceSemaphore;
10+
sem_t leftSpaceSemaphore;
11+
12+
int get_nproc(int *nproc) {
13+
*nproc = sysconf(_SC_NPROCESSORS_ONLN);
14+
if (*nproc == -1) {
15+
perror("Reading number of threads failed");
16+
return -1;
17+
}
18+
return 0;
19+
}
20+
21+
int get_semaphore_value(sem_t *sem) {
22+
int sval;
23+
sem_getvalue(sem, &sval);
24+
return sval;
25+
}
26+
27+
int put_item(struct proc_stat *stats) {
28+
int index = get_semaphore_value(&filledSpaceSemaphore);
29+
if (index > BUFFER_SIZE) {
30+
return -1;
31+
}
32+
BUFFER[index] = stats;
33+
return 0;
34+
}
635

736
int main() {
8-
if (get_nproc(&nproc) == -1)
37+
if (get_nproc(&nproc) == -1) {
38+
exit(EXIT_FAILURE);
39+
}
40+
41+
if (sem_init(&filledSpaceSemaphore, 0, 0) ||
42+
sem_init(&leftSpaceSemaphore, 0, BUFFER_SIZE)) {
943
exit(EXIT_FAILURE);
44+
}
45+
1046
nproc++;
11-
print_stats();
47+
reader();
1248
return 0;
1349
}

cputracker.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
#define CPUTRACKER_H
33

44
#include <assert.h>
5+
#include <pthread.h>
6+
#include <semaphore.h>
7+
#include <signal.h>
58
#include <stdio.h>
69
#include <stdlib.h>
710
#include <string.h>
811

12+
#define BUFFER_SIZE 100
13+
914
#define EXIT_FAILURE 1
1015
#define EXIT_SUCCESS 0
1116

12-
extern int nproc;
13-
1417
struct proc_stat {
1518
char name[256];
1619
unsigned long user, // Time spent with normal processing in user mode.
@@ -29,4 +32,13 @@ struct proc_stat {
2932
// operating systems under the control of the Linux kernel).
3033
};
3134

35+
extern int nproc;
36+
extern struct proc_stat *BUFFER[];
37+
extern pthread_mutex_t bufferMutex;
38+
extern sem_t filledSpaceSemaphore;
39+
extern sem_t leftSpaceSemaphore;
40+
41+
int get_nproc();
42+
int get_semaphore_value(sem_t *sem);
43+
int put_item(struct proc_stat *stats);
3244
#endif

reader.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
#include "reader.h"
22
#include "cputracker.h"
3+
#include <pthread.h>
4+
#include <semaphore.h>
35
#include <stdio.h>
46

5-
int get_nproc(int *nproc) {
6-
*nproc = sysconf(_SC_NPROCESSORS_ONLN);
7-
if (*nproc == -1) {
8-
perror("Reading number of threads failed");
9-
return -1;
10-
}
11-
return 0;
12-
}
13-
147
struct proc_stat *get_proc_stats() {
158
FILE *file = fopen("/proc/stat", "r");
169
char line[1024];
@@ -20,6 +13,7 @@ struct proc_stat *get_proc_stats() {
2013
// assert(strncmp(line, "cpu", 3) == 0);
2114
if (strncmp(line, "cpu", 3) != 0) {
2215
perror("Reading thread info failed");
16+
fclose(file);
2317
free(stats);
2418
return NULL;
2519
}
@@ -34,8 +28,23 @@ struct proc_stat *get_proc_stats() {
3428
return stats;
3529
}
3630

37-
int print_stats() {
38-
struct proc_stat *stats = get_proc_stats();
31+
void reader() {
32+
struct proc_stat *stats = NULL;
33+
while (1) {
34+
if ((stats = get_proc_stats()) == NULL) {
35+
return;
36+
}
37+
38+
sem_wait(&leftSpaceSemaphore);
39+
40+
pthread_mutex_lock(&bufferMutex);
41+
42+
put_item(stats);
43+
44+
pthread_mutex_unlock(&bufferMutex);
45+
46+
sem_wait(&filledSpaceSemaphore);
47+
}
3948

4049
for (int i = 0; i < nproc; i++) {
4150
printf("%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", stats[i].name,
@@ -44,6 +53,5 @@ int print_stats() {
4453
stats[i].guest, stats[i].guest_nice);
4554
}
4655

47-
free(stats);
48-
return 0;
56+
// free(stats);
4957
}

reader.h

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

5-
int print_stats();
5+
void reader();
66
struct proc_stat *get_proc_stats();
7-
int get_nproc();
87

98
#endif

0 commit comments

Comments
 (0)