关闭

linux 系统信息查询,内存,闪存,cpu个数查询

标签: linux 内存闪存cpu个数查询
140人阅读 评论(0) 收藏 举报
分类:
</pre><pre name="code" class="plain">

#include <stdio.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <sys/statfs.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
</pre><pre name="code" class="cpp">//获取系统名称,类似shell命令  uname -a
string getUname()
{
    struct utsname u;
    uname(&u);
    char buf[256] = {0};
    sprintf(buf, "%s %s %s %s %s", u.sysname, u.nodename, u.release, u.version, u.machine);
    return string(buf);
}
//获取系统cpu个数
unsigned int getCpuCoreNumber()
{
    return sysconf(_SC_NPROCESSORS_CONF);
}
//获取使用的cpu个数
unsigned int getCpuCoreUseage()
{
    return sysconf(_SC_NPROCESSORS_ONLN);
}
unsigned int getPhyTotalPages()
{
    return sysconf(_SC_PHYS_PAGES);
}
unsigned int getPhyAvailablePages()
{
    return sysconf(_SC_AVPHYS_PAGES);
}
//获取系统名字
string getHostname()
{
    char host[32] = {0};
    gethostname(host, sizeof(host));
    return string(host);
}
//获取内存大小
string getRamTotalSize()
{
    struct sysinfo s_info;
    sysinfo(&s_info);
    char buf[100] = {0};
    sprintf(buf,"%luMB", s_info.totalram/1024/1024);
    return string(buf);
}
<pre name="code" class="cpp">//获取剩余内存大小

string getRamAvailSize()
{
    struct sysinfo s_info;
    sysinfo(&s_info);
    char buf[100] = {0};
    sprintf(buf,"%luMB", s_info.freeram/1024/1024);
    return string(buf);
}
//获取根目录挂载空间大小
string getRomTotalSize()
{
    struct statfs diskInof;
    statfs("/", &diskInof);
    unsigned long long blocksize = diskInof.f_bsize;
    unsigned long long totalsize = blocksize * diskInof.f_blocks;


    char buf[100] = {0};
    sprintf(buf, "%lluMB", totalsize / 1000 / 1000 );
    return string(buf);
}
//获取根目录可用空间
string getRomAvailSize()
{
    struct statfs diskInof;
    if (statfs("/", &diskInof) != 0)
    {
        return string("");
    }
    unsigned long long blocksize = diskInof.f_bsize;
    unsigned long long freeDisk = diskInof.f_bfree * blocksize;
    unsigned long long availableDisk = blocksize * diskInof.f_bavail;


    char buf[100] = {0};
    sprintf(buf, "%lluMB", availableDisk / 1000 / 1000 );
    return string(buf);
}
//获取sdcard 挂载空间大小,路径根据自己挂载目录指定
string getSDcardTotalSize()
{
    struct statfs diskInof;
    if (statfs("/media/mmcblk1", &diskInof) != 0)
    {
        return string("0MB");
    }
    unsigned long long blocksize = diskInof.f_bsize;
    unsigned long long totalsize = blocksize * diskInof.f_blocks;


    char buf[100] = {0};
    sprintf(buf, "%lluMB", totalsize / 1000 / 1000 );
    return string(buf);
}
//获取SD卡可用空间大小
string getSDcardAvailSize()
{
    struct statfs diskInof;
    if (statfs("/media/mmcblk1", &diskInof) != 0)
    {
        return string("0MB");
    }
    unsigned long long blocksize = diskInof.f_bsize;
    unsigned long long freeDisk = diskInof.f_bfree * blocksize;
    unsigned long long availableDisk = blocksize * diskInof.f_bavail;


    char buf[100] = {0};
    sprintf(buf, "%lluMB", availableDisk / 1000 / 1000 );
    return string(buf);
}

   
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:743次
    • 积分:30
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档