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

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

示例代码受到高度赞赏。

回答:

这是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);

}

这篇Wikipedia文章中有关如何使用该CPUID指令的很好参考。

Wikipedia文章说,序列号是随Pentium

III一起引入的,但是由于隐私问题,以后的型号中不再使用该序列号。在Linux系统上,您可以通过执行以下操作检查此功能(PSN位)是否存在:

grep -i --color psn /proc/cpuinfo

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

以上是 在Linux中使用C / C ++获取机器序列号和CPU ID 的全部内容, 来源链接: utcz.com/qa/435077.html

回到顶部