-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocstats.c
More file actions
48 lines (43 loc) · 1.03 KB
/
procstats.c
File metadata and controls
48 lines (43 loc) · 1.03 KB
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
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
int main() {
// Read cpuinfo to show CPU, Cores (assigned to VM) and clock frequency
FILE *fp1 = fopen("/proc/cpuinfo", "r");
assert(fp1 != NULL);
size_t n1 = 0;
char *line1 = NULL;
while (getline(&line1, &n1, fp1) > 0) {
if (strstr(line1, "model name") || strstr(line1, "cpu cores") || strstr(line1, "cpu MHz")) {
printf("%s", line1);
}
}
free(line1);
fclose(fp1);
// read meminfo to show ram available and toral
FILE *fp2 = fopen("/proc/meminfo", "r");
assert(fp2 != NULL);
size_t n2 = 0;
char *line2 = NULL;
while (getline(&line2, &n2, fp2) > 0) {
if (strstr(line2, "MemTotal:") || strstr(line2, "MemFree:")) {
printf("%s", line2);
}
}
free(line2);
fclose(fp2);
// Show OS Version
FILE *fp3 = fopen("/proc/version", "r");
assert(fp3 != NULL);
size_t n3 = 0;
char *line3 = NULL;
while (getline(&line3, &n3, fp3) > 0) {
printf("%s", line3);
}
free(line3);
fclose(fp3);
return errno;
}