sobota, 22 grudnia 2018

Use /proc/meminfo (gcc)

#include <stdio.h>
#include <string.h>



int main(int argc, char **argv)
{
    int i;
    char *file;
    char end;
    FILE *m_File;
    char buffer_F[5][1024];
    size_t read_Buff[5];
    unsigned int mem_Total,mem_Free,mem_Shared,swap_Total,swap_Free;
    char *info_mem_Total,*info_mem_Free,*info_mem_Shared,*info_swap_Total,
    *info_swap_Free;
    file="/proc/meminfo";
    end='\0';
    m_File=fopen(file,"r");
    for(i=0;i<5;i++)
    {
     read_Buff[i]=fread(buffer_F[i],1,sizeof(buffer_F[i]),m_File);
    }
    fclose(m_File);
    for(i=0;i<5;i++)
    {
     buffer_F[i][read_Buff[i]]=end;
    }
    info_mem_Total=strstr(buffer_F[0],"MemTotal");
    sscanf(info_mem_Total,"MemTotal : %u",&mem_Total);
    info_mem_Free=strstr(buffer_F[1],"MemFree");
    sscanf(info_mem_Free,"MemFree : %u",&mem_Free);
    info_mem_Shared=strstr(buffer_F[2],"MemShared");
    sscanf(info_mem_Shared,"MemShared : %u",&mem_Shared);
    info_swap_Total=strstr(buffer_F[3],"SwapTotal");
    sscanf(info_swap_Total,"SwapTotal: %u",&swap_Total);
    info_swap_Free=strstr(buffer_F[4],"SwapFree");
    sscanf(info_swap_Free,"SwapFree: %u",&swap_Free);
    printf("\nMemTotal: %u",mem_Total);   
    printf("\nMemFree : %u",mem_Free);
    printf("\nMemShared : %u",mem_Shared);
    printf("\nSwapTotal: %u",swap_Total);
    printf("\nswap_Free: %u",swap_Free);
   
   
    return 0;
}

Brak komentarzy:

Prześlij komentarz