从 Linux 中的 /proc 文件系统获取硬件信息

cha*_*hsu 4 c linux memory cpu disk

我使用execv运行lshw命令来获取 C 代码中的 CPU、磁盘和内存。但我想搜索另一种解决方案来从/proc或任何其他现有数据中获取这些信息。有什么建议吗?这是我的代码:

char *params[9]  = {"/usr/bin/lshw", "-short", "-c", "disk", 
                  "-c", "memory", "-c", "processor", 0}; //cmd params filled
execv(params[0], params);
Run Code Online (Sandbox Code Playgroud)

Linux命令:$ sudo lshw -short -c disk -c processor -c memory

$ sudo lshw -short -c disk -c processor -c memory
H/W path         Device     Class          Description
======================================================
/0/0                        memory         64KiB BIOS
/0/22                       memory         16GiB System Memory
/0/22/0                     memory         DIMM Synchronous [empty]
/0/22/1                     memory         DIMM Synchronous [empty]
/0/22/2                     memory         8GiB DIMM Synchronous 2133 MHz (0.5 ns)
/0/22/3                     memory         8GiB DIMM Synchronous 2133 MHz (0.5 ns)
/0/2a                       memory         256KiB L1 cache
/0/2b                       memory         1MiB L2 cache
/0/2c                       memory         6MiB L3 cache
/0/2d                       processor      Intel(R) Xeon(R) CPU D-1521 @ 2.40GHz
/0/1/0.0.0       /dev/sda   disk           16GB SATADOM-SH 3IE3
/0/2/0.0.0       /dev/sdb   disk           120GB Patriot Blaze
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

  • 在哪里可以找到解析文件以/proc获取这些硬件信息的指南?
  • 我需要追踪源代码lshw才能找到它的作用吗lshw

编辑:

《高级Linux编程》第7章是解析文件系统的指南/proc

MIG*_*-23 5

获取硬件信息的最佳方法是使用 sysconf() 和 sysctl*() 函数(Mac OS X、freebsd、openbsd)以及 Linux 上的 sysconf() 和 sysinfo()。

解析 /proc/* 比调用 sysinfo( ) 或 sysconf( ) 更慢且更复杂

下面是一个小示例,为您提供有关 Mac OS X 上的处理器和内存的一些信息:

#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main()
{
 char *p = NULL;
 size_t len;
 sysctlbyname("hw.model", NULL, &len, NULL, 0);
 p = malloc(len);
 sysctlbyname("hw.model", p, &len, NULL, 0);
 printf("%s\n", p);
 /* CTL_MACHDEP variables are architecture dependent so doesn't work 
 for every one */
 sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0);
 p = malloc(len);
 sysctlbyname("machdep.cpu.brand_string", p, &len, NULL, 0);
 printf("%s\n", p);
 int64_t mem;
 len = sizeof(mem);
 sysctlbyname("hw.memsize", &mem, &len, NULL, 0);
 printf("System Memory : %lld\n", mem);
 return (0);
}
Run Code Online (Sandbox Code Playgroud)

您必须阅读 man 3 sysctl,或者在 Linux 上阅读 man 2 sysconf 和 man 2 sysinfo。

一个有趣的链接:http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system#Other

您可以通过检索一些 sysctl 变量来计算 CPU 负载和使用情况,并自己进行数学计算(您可以在 google 上找到计算公式)。

但是在哪里可以找到 $ sudo lshw -short -c memory 报告中的物理 DIMM 信息?

您可以在 C 程序中执行命令,将其保存为字符串,如下所示:

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>

char *strjoin(char *s1, char *s2, int n)
{
    int i = strlen(s2);
    int j = 0;
    if ((s2 = realloc(s2, (i + n + 1))) == NULL)
            perror(0);
    while (j < n && s1[j])
    {
            s2[i] = s1[j];
            i++;
            j++;
    }
    s2[i] = 0;
    return (s2);
}

int main()
{
    pid_t father;
    char buf[500] = {0};
    char *str;
    char *argv[5] = {"/usr/bin/lshw", "-short", "-c", "memory"};
    int fd[2];
    int ret;

    if (pipe(fd) == -1)
    {
            perror(NULL);
            return -1;
    }
    father = fork();
    if (father == 0)
    {
            close(fd[1]);
            while ((ret = read(fd[0], buf, 500)))
            {
                    str = strjoin(buf, str, ret);
            }
            close(fd[0]);
    }
    else
    {
            close(fd[0]);
            execv(argv[0], argv);
            close(fd[1]);
            wait(0);
    }
    wait(0);
    printf("%s", str);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

(我没有检查此代码中所有函数的返回,以免返回太长,但您应该在程序中执行此操作)。

下面是一个解析文件 /proc/meminfo 以将我想要的 2 个字符串保存在双数组中,然后将它们打印出来的示例:

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

int main()
{
    FILE *f;
    char *line = NULL;
    ssize_t read;
    size_t len = 0;
    char    **info;
    int i = 0;

    info = malloc(3 * sizeof(char*));
    f = fopen("/proc/meminfo", "r");
    while ((read = getline(&line, &len, f)) != -1)
    {
            if (strstr(line, "MemTotal") != NULL)
                    info[i] = strdup(line);
            else if (strstr(line, "MemFree") != NULL)
                    info[i] = strdup(line);
            i++;
    }
    info[i] = 0;
    fclose(f);
    i = 0;
    while (info[i])
    {
            printf("%s", info[i]);
            free (info[i]);
            i++;
    }
    free (info);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果您想保存更多字符串,请在双数组信息中分配更多空间,并在读取循环内使用 else if 添加它们。您可以对 /proc/ 中的任何文件执行此操作,以获取所需的信息。