你如何确定C ++中的Linux系统RAM的数量? [英] How do you determine the amount of Linux system RAM in C++?

查看:153
本文介绍了你如何确定C ++中的Linux系统RAM的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚写了下面的C ++函数来编程确定系统安装了多少RAM。它工作,但在我看来,应该有一个更简单的方法来做到这一点。有人可以告诉我是否缺少某些内容?

I just wrote the following C++ function to programatically determine how much RAM a system has installed. It works, but it seems to me that there should be a simpler way to do this. Can someone tell me if I'm missing something?

getRAM()
{
    FILE* stream = popen( "head -n1 /proc/meminfo", "r" );
    std::ostringstream output;
    int bufsize = 128;

    while( !feof( stream ) && !ferror( stream ))
    {
        char buf[bufsize];
        int bytesRead = fread( buf, 1, bufsize, stream );
        output.write( buf, bytesRead );
    }
    std::string result = output.str();

    std::string label, ram;
    std::istringstream iss(result);
    iss >> label;
    iss >> ram;

    return ram;
}

首先,我使用 popen -n1 / proc / meminfo),从系统中获取meminfo文件的第一行。该命令的输出如下所示:

First, I'm using popen("head -n1 /proc/meminfo") to get the first line of the meminfo file from the system. The output of that command looks like


MemTotal:775280 kB

MemTotal: 775280 kB


b $ b

一旦我得到一个 istringstream 中的输出,它很简单的tokenize它得到我想要的信息。我的问题是,有一个更简单的方法来读取这个命令的输出?

Once I've got that output in an istringstream, it's simple to tokenize it to get at the information I want. My question is, is there a simpler way to read in the output of this command? Is there a standard C++ library call to read in the amount of system RAM?

推荐答案

在Linux上,您可以使用函数 sysinfo ,它设置以下结构中的值:

On Linux, you can use the function sysinfo which sets values in the following struct:

   #include <sys/sysinfo.h>

   int sysinfo(struct sysinfo *info);

   struct sysinfo {
       long uptime;             /* Seconds since boot */
       unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
       unsigned long totalram;  /* Total usable main memory size */
       unsigned long freeram;   /* Available memory size */
       unsigned long sharedram; /* Amount of shared memory */
       unsigned long bufferram; /* Memory used by buffers */
       unsigned long totalswap; /* Total swap space size */
       unsigned long freeswap;  /* swap space still available */
       unsigned short procs;    /* Number of current processes */
       unsigned long totalhigh; /* Total high memory size */
       unsigned long freehigh;  /* Available high memory size */
       unsigned int mem_unit;   /* Memory unit size in bytes */
       char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */
   };

如果你只想使用C ++的函数> sysinfo ),我建议使用 std :: ifstream std :: string

If you want to do it solely using functions of C++ (i would stick to sysinfo), i recommend taking a C++ approach using std::ifstream and std::string:

unsigned long get_mem_total() {
    std::string token;
    std::ifstream file("/proc/meminfo");
    while(file >> token) {
        if(token == "MemTotal:") {
            unsigned long mem;
            if(file >> mem) {
                return mem;
            } else {
                return 0;       
            }
        }
        // ignore rest of the line
        file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    return 0; // nothing found
}

这篇关于你如何确定C ++中的Linux系统RAM的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆