我想知道如何在GNU Linux上可靠地获得处理器序列号(PSN)。
现在我正在使用这个
#include <stdio.h> #include <cpuid.h> unsigned int level = 1; unsigned eax = 3 /* processor serial number */, ebx = 0, ecx = 0, edx = 0; __get_cpuid(level, &eax, &ebx, &ecx, &edx); // byte swap int first = ((eax >> 24) & 0xff) | ((eax << 8) & 0xff0000) | ((eax >> 8) & 0xff00) | ((eax << 24) & 0xff000000); int last = ((edx >> 24) & 0xff) | ((edx << 8) & 0xff0000) | ((edx >> 8) & 0xff00) | ((edx << 24) & 0xff000000); printf("PSN: %08X%08X", first, last);
它给了我PSN: A7060200FFFBEBBF
,
与之匹配
sudo dmidecode | grep -P '^\s+ID: ([0-9A-F]{2} ){7}[0-9A-F]{2}$'
输出: ID: A7 06 02 00 FF FB EB BF
我只testing了英特尔酷睿i处理器,所以也许它只适用于这种types的CPU。
我知道“序列号”在相同的CPU型号上是相同的,因此不是唯一的。
此外,我期待着实现这一目标的方式,即不依赖执行shell命令和parsing输出。
你可以使用popen,然后解析结果
unsigned char *pk = new unsigned char[100]; FILE *source = popen("lscpu", "r"); while (!feof(source)) { fread(pk, 100, 1, source); for(int i=0;i<100;++i) { printf("%c",pk[i]); } printf("\n"); } pclose(source);