Skip to content
Snippets Groups Projects
Commit 9cd458dc authored by Marc Feger's avatar Marc Feger
Browse files

Add programm to read meminfo

parent 73015b9f
No related branches found
No related tags found
Loading
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment