在Linux中使用C/C++获取机器序列号和CPU ID

boo*_*oom 21 c c++ linux

如何在Linux系统中获取机器序列号和CPU ID?

示例代码非常感谢.

And*_*ner 21

以下是Linux内核似乎使用的内容:

static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
                                unsigned int *ecx, unsigned int *edx)
{
        /* ecx is often an input as well as an output. */
        asm volatile("cpuid"
            : "=a" (*eax),
              "=b" (*ebx),
              "=c" (*ecx),
              "=d" (*edx)
            : "0" (*eax), "2" (*ecx));
}
Run Code Online (Sandbox Code Playgroud)

哪一个可以用作例如:

#include <stdio.h>

int main(int argc, char **argv)
{
  unsigned eax, ebx, ecx, edx;

  eax = 1; /* processor info and feature bits */
  native_cpuid(&eax, &ebx, &ecx, &edx);

  printf("stepping %d\n", eax & 0xF);
  printf("model %d\n", (eax >> 4) & 0xF);
  printf("family %d\n", (eax >> 8) & 0xF);
  printf("processor type %d\n", (eax >> 12) & 0x3);
  printf("extended model %d\n", (eax >> 16) & 0xF);
  printf("extended family %d\n", (eax >> 20) & 0xFF);

  /* EDIT */
  eax = 3; /* processor serial number */
  native_cpuid(&eax, &ebx, &ecx, &edx);

  /** see the CPUID Wikipedia article on which models return the serial 
      number in which registers. The example here is for 
      Pentium III */
  printf("serial number 0x%08x%08x\n", edx, ecx);

}
Run Code Online (Sandbox Code Playgroud)

本维基百科文章中有关如何使用该CPUID指令的良好参考.

编辑维基百科的文章说,序列号是在奔腾III中引入的,但由于隐私问题,后来的版本不再实施.在Linux系统上,您可以通过执行以下操作来检查是否存在此功能(PSN位):

grep -i --color psn /proc/cpuinfo
Run Code Online (Sandbox Code Playgroud)

如果没有显示任何内容,则表明您的系统不支持处理器序列号.


cpp*_*ist 17

GCC中有一个cpuinfo.h包含.这是安全的,使用它.

示例(我有GCC 4.7+并且很高兴在这里使用"auto"):

#include <cpuid.h>
#include <iostream>
#include <map>
#include <string>

using namespace std;

struct CPUVendorID {
    unsigned int ebx;
    unsigned int edx;
    unsigned int ecx;

    string toString() const {
        return string(reinterpret_cast<const char *>(this), 12);
    }
};

int main() {
    unsigned int level = 0;
    unsigned int eax = 0;
    unsigned int ebx;
    unsigned int ecx;
    unsigned int edx;

    __get_cpuid(level, &eax, &ebx, &ecx, &edx);

    CPUVendorID vendorID { .ebx = ebx, .edx = edx, .ecx = ecx };

    map<string, string> vendorIdToName;
    vendorIdToName["GenuineIntel"] = "Intel";
    vendorIdToName["AuthenticAMD"] = "AMD";
    vendorIdToName["CyrixInstead"] = "Cyrix";
    vendorIdToName["CentaurHauls"] = "Centaur";
    vendorIdToName["SiS SiS SiS "] = "SiS";
    vendorIdToName["NexGenDriven"] = "NexGen";
    vendorIdToName["GenuineTMx86"] = "Transmeta";
    vendorIdToName["RiseRiseRise"] = "Rise";
    vendorIdToName["UMC UMC UMC "] = "UMC";
    vendorIdToName["Geode by NSC"] = "National Semiconductor";

    string vendorIDString = vendorID.toString();

    auto it = vendorIdToName.find(vendorIDString);
    string vendorName = (it == vendorIdToName.end()) ? "Unknown" : it->second;

    cout << "Max instruction ID: " << eax << endl;
    cout << "Vendor ID: " << vendorIDString << endl;
    cout << "Vendor name: " << vendorName << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出:

$ make
g++ --std=c++11 main.cc -o cpuid
$ ./cpuid 
Max instruction ID: 6
Vendor ID: GenuineIntel
Vendor name: Intel
Run Code Online (Sandbox Code Playgroud)


Tho*_*ger 6

关于您可以从中提取的处理器的信息/proc/cpuinfo.

要获得序列号,您应该看一下dmidecode.我现在没有看到那里,但dmidecode能够显示序列号,所以我会从那里开始.