gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:45:29 49 4
gpt4 key购买 nike

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

非常感谢示例代码。

最佳答案

Here是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));
}

然后哪个可以用作例如:

#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);

}

关于如何使用CPUID 指令的一个很好的引用在this Wikipedia article 中。 .

编辑 Wikipedia 文章称,序列号是在 Pentium III 中引入的,但由于隐私问题不再在以后的模型中实现。在 Linux 系统上,您可以通过以下方式检查此功能(PSN 位)是否存在:

grep -i --color psn /proc/cpuinfo

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

关于c++ - 在 Linux 中使用 C/C++ 获取机器序列号和 CPU ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6491566/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com