From 9cd458dc915e730108ef2e3daf5af18119b7c54f Mon Sep 17 00:00:00 2001 From: TomatenMarc <marc.feger@uni-duesseldorf.de> Date: Thu, 24 May 2018 21:55:34 +0200 Subject: [PATCH] Add programm to read meminfo --- Read_System_Info/read_sys.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Read_System_Info/read_sys.c diff --git a/Read_System_Info/read_sys.c b/Read_System_Info/read_sys.c new file mode 100644 index 0000000..5996a8a --- /dev/null +++ b/Read_System_Info/read_sys.c @@ -0,0 +1,36 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +#define SIZE 1000 + +int main(void){ + + int file = open("/proc/meminfo", O_RDONLY); + if(file == -1){ + perror("open error"); + return -1; + } + char *buffer = malloc(SIZE*sizeof(char)); + int rd = read(file, buffer, SIZE); + if(rd == -1){ + perror("read error"); + free(buffer); + close(file); + return -1; + } + + char mem_total[SIZE]; + char mem_free[SIZE]; + char mem_cach[SIZE]; + + sscanf(buffer, "%*s %s %*s %*s %s %*s %*s %*s %*s %*s %*s %*s %*s %s", mem_total, mem_free, mem_cach); + printf("MemTotal: %s\nMemFree: %s\nCached: %s\n", mem_total, mem_free, mem_cach); + + free(buffer); + close(file); + return 0; +} -- GitLab