niedziela, 13 października 2019

Data from /proc/meminfo (Linux C++)


#include <iostream>
#include <string.h>
#include <stdio.h>
#define size_B 1024
using namespace std;

struct mem_I
{
double Slab;
double SReclaimable;
double SUnreclaim;
double KernelStack;
double PageTables;

};
char mem_I_Val1[5][20]={"Slab","Sreclaimable","SUnreclaim","KernelStack","PageTables"};
char mem_I_Val2[5][20]={"Slab : %f","Sreclaimable : %f","SUnreclaim %f","KernelStack %f","PageTables %f"};

float write_Value(char *text1, char *text2)
{
size_t read_Size;
float result;
char  buff[size_B];
char *temp;
FILE *test_File;
test_File=fopen("/proc/meminfo","r");
read_Size=fread(buff,1,sizeof(buff),test_File);
fclose(test_File);
buff[read_Size]='\0';
temp=strstr(buff,text1);
if(temp==NULL)
return 0;
sscanf(temp,text2,&result);
return result;


}

int main(int argc, char **argv)
{
int i;
float results[5];
for(i=0;i<5;i++)
{
results[i]=write_Value(mem_I_Val1[i],mem_I_Val2[i]);
}
for(i=0;i<5;i++)
{
cout<<mem_I_Val1[i]<<" - "<<results[i]<<" kB"<<endl;
}
return 0;
}

Brak komentarzy:

Prześlij komentarz