打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
在Linux中从/ proc文件系统获取硬件信息

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

char *params[9]  = {"/usr/bin/lshw", "-short", "-c", "disk",                   "-c", "memory", "-c", "processor", 0}; //cmd params filledexecv(params[0], params);

Linux命令:$sudo lshw -short -c磁盘-c处理器-c内存

$sudo lshw -short -c disk -c processor -c memoryH/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

我有两个问题:

>哪里可以找到解析/ proc中文件的指南
这些硬件信息?
>是否需要跟踪lshw的源代码以查找lshw的功能?

编辑:

Advanced Linux Programming的第7章是解析/ proc文件系统的指南.

解决方法:

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

与调用sysinfo()或sysconf()相比,解析/ proc / *速度慢且涉及更多

以下是一个小示例,为您提供有关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);}

您必须阅读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上找到执行该操作的公式).

But where to find the physical DIMM information as the report from $sudo lshw -short -c memory ?

您可以在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;}

(我不会在此代码中检查所有函数的返回值,因为返回值不会太长,但是您应该在程序中执行此操作).

这是一个解析文件/ 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;}

如果要保存更多字符串,请在double数组信息中分配更多空间,如果在read循环内,则将它们与else添加在一起.您可以使用/ proc /中的任何文件来获得所需的信息.

来源:https://www.icode9.com/content-3-556251.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
C语言面试题
wav2pcm
编程实现字符串长度测量
内核模块遍历进程--内核PS - ldd学习笔记 - ubuntuer zone
[收藏]C++ Tips(7)--堆内存分配 - 心如止水--coofucoo的专栏
/dev/hello
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服