如何在Linux上获取C中的CPU信息,例如内核数量? [重复]

问题描述 投票:9回答:9

这个问题在这里已有答案:

是否可以通过某些API或函数获取此类信息,而不是解析/proc/cpuinfo

c linux operating-system cpu
9个回答
10
投票

来自man 5 proc

   /proc/cpuinfo
          This is a collection of CPU and  system  architecture  dependent
          items,  for  each  supported architecture a different list.  Two
          common  entries  are  processor  which  gives  CPU  number   and
          bogomips;  a  system  constant  that is calculated during kernel
          initialization.  SMP machines have information for each CPU.

下面是示例代码,它将信息读取并打印到控制台,stolen from forums - 它实际上只是一个专门的cat命令。

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
   FILE *cpuinfo = fopen("/proc/cpuinfo", "rb");
   char *arg = 0;
   size_t size = 0;
   while(getdelim(&arg, &size, 0, cpuinfo) != -1)
   {
      puts(arg);
   }
   free(arg);
   fclose(cpuinfo);
   return 0;
}

请注意,如果您真的关心CPU数量与CPU核心数量,则需要解析并比较physical idcore idcpu cores以获得准确的结果。另请注意,如果htt中有flags,那么您运行的是超线程CPU,这意味着您的里程可能会有所不同。

另请注意,如果在虚拟机中运行内核,则只能看到专用于VM guest虚拟机的CPU内核。


5
投票

您可以将它用于大多数类型的Linux发行版

对于C代码

 num_cpus = sysconf( _SC_NPROCESSORS_ONLN );

(在QNX系统中,您可以使用num_cpus = sysinfo_numcpu()

对于shell脚本,您可以使用cat /proc/cpuinfo

或者在linux中使用lscpunproc命令


4
投票

libcpuid提供了一个简单的API,它将直接返回所有CPU功能,包括内核数量。要在运行时获取核心数,您可以执行以下操作:

#include <stdio.h>
#include <libcpuid.h>

int main(void)
{
    if (!cpuid_present()) {
        printf("Sorry, your CPU doesn't support CPUID!\n");
        return -1;
    }

    struct cpu_raw_data_t raw; 
    struct cpu_id_t data;     

    if (cpuid_get_raw_data(&raw) < 0) { 
        printf("Sorry, cannot get the CPUID raw data.\n");
        printf("Error: %s\n", cpuid_error());
        return -2;
    }

    if (cpu_identify(&raw, &data) < 0) {    
        printf("Sorrry, CPU identification failed.\n");
        printf("Error: %s\n", cpuid_error());
        return -3;
    }

    printf("Processor has %d physical cores\n", data.num_cores);
    return 0;
}

4
投票

阅读/proc/cpuinfo

样本输出

processor   : 0
model name  : Intel(R) Xeon(R) CPU           E5410  @ 2.33GHz
cache size  : 6144 KB
physical id : 0
siblings    : 4
core id     : 0
cpu cores   : 4
processor   : 1
model name  : Intel(R) Xeon(R) CPU           E5410  @ 2.33GHz
cache size  : 6144 KB
physical id : 0
siblings    : 4
core id     : 1
cpu cores   : 4
processor   : 2
model name  : Intel(R) Xeon(R) CPU           E5410  @ 2.33GHz
cache size  : 6144 KB
physical id : 0
siblings    : 4
core id     : 2
cpu cores   : 4
processor   : 3
model name  : Intel(R) Xeon(R) CPU           E5410  @ 2.33GHz
cache size  : 6144 KB
physical id : 0
siblings    : 4
core id     : 3
cpu cores   : 4

show_cpuinfo是实际实现/proc/cpuinfo功能的功能


1
投票

解析文件/ proc / cpuinfo。这将为您提供有关CPU的大量详细信息。将相关字段解压缩到C / C ++文件中。


1
投票

在源代码中添加以下行。

system("cat /proc/cpuinfo | grep processor | wc -l");

这将打印系统中的cpus数量。如果你想在你的程序中使用这个系统调用的输出而不是使用popen系统调用。


1
投票

不它不是。您必须解析cpuinfo文件,否则某些库会为您执行此操作。


1
投票

根据您的Linux风格,您将从/ proc / cpuid获得不同的结果。

这对我来说可以在CentOS上获得核心总数。

cat /proc/cpuinfo | grep -w cores | sed -e 's/\t//g' | awk '{print $3}' | xargs | sed -e 's/\ /+/g' | bc

这在Ubuntu中不起作用。对于Ubuntu,您可以使用以下命令。

nproc

0
投票

你见过这个shell命令“cat / proc / cpuinfo”的输出吗?我认为您可以获得所需的所有信息。要读取C程序中的信息,我更喜欢文件操作函数,如fopen,fgets等。

© www.soinside.com 2019 - 2024. All rights reserved.