獲取linux可用記憶體 剩餘記憶體

zzcmx2008發表於2012-04-28
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <string>
using namespace std;
int main(int argc,char** agrv)
{
        FILE *fd;
        char buff[256];
        int total = 0;
        int free = 0;
        float use=0;

        fd = fopen ("/proc/meminfo", "r");

        fgets (buff, sizeof(buff), fd);
        sscanf (buff, "%*s %d %*s", &total);

        fgets (buff, sizeof(buff), fd);
        sscanf (buff, "%*s %d %*s", &free);
        use=(float)((total-free) /(total * 0.01));//百分比

        fclose(fd);    
       cout<<"use:"<<use<<endl;
       return 0;
}



相關文章