linux取不到处理器的序列号,请大家帮我看看哪错了!

ankoe 2009-06-05 04:36:38
取不到处理器的序列号,请大家帮我看看哪错了!!
-------------------------
cpuid.h文件
-------------------------
#ifndef CPUID_H
#define CPUID_H
#include<iostream>
#include<string>
#include<memory.h>
#include<QString>
using namespace std;

#define SET_BIT(bit) (1UL<<bit)
#define CLR_BIT(bit) (~(1UL<<bit))

/*定义寄存器结构*/
typedef struct regStruct{ unsigned long int eax,ebx,ecx,edx; }Reg,*pReg;

class CPUID
{
public:
static CPUID *Instance();
void GetSerialNumber();
private:
Reg cpuID(unsigned long op);
static CPUID * m_pInstance;
QString ltos(unsigned long int num);
};

#endif // CPUID_H
-------------------------
cpudid.cpp文件
-------------------------

#include "cpuid.h"

CPUID* CPUID::m_pInstance = NULL;

CPUID * CPUID::Instance()
{
if (NULL == m_pInstance)
{
m_pInstance = new CPUID();
}

return m_pInstance;
}

Reg CPUID::cpuID(unsigned long op)
{
Reg r;
asm("cpuid;"
:"=a"(r.eax),
"=b"(r.ebx),
"=c"(r.ecx),
"=d"(r.edx)
:"a"(op)
);
return r;
}

void CPUID::GetSerialNumber(){
Reg r;
unsigned short int cpu_sn[6]; //存储序列号
QString sn;
int i;
r=this->cpuID(1);
memcpy((&cpu_sn) + 4, &r.eax, 4);
printf("CPU序列号是:%X-%X\n",r.edx,r.ecx);
if(r.edx & (1<<18))
{
r=this->cpuID(3);
memcpy((&cpu_sn) + 0, &r.ecx, 8);
for(i=5;i>=0;i--)
{
printf("%x",cpu_sn);
if(i) printf("-");
}
}
}

QString CPUID::ltos(unsigned long int num)
{
QString s=QString("");
char numchar;
do{
numchar=char(int('0')+(int)(num));
s=numchar+s;
num=num/10;
}while(num>0);
return(s);
}
--------------------------
main.cpp
-------------------------
#include<iostream>
#include "cpuid.h"
int main(){
CPUID::Instance()->GetSerialNumber();
string s;

return 0;
}

程序执行到
if(r.edx & (1<<18))
{
r=this->cpuID(3);
memcpy((&cpu_sn) + 0, &r.ecx, 8);
for(i=5;i>=0;i--)
{
//sn+=this->ltos(cpu_sn);
//sn+=cpu_sn
printf("%x",cpu_sn);
if(i) printf("-");
}
}
就跳过了.
查看了下英文帮助文档:
To determine if the processor serial number feature is supported, the program should set
the EAX register parameter value to “1” and then execute the CPUID instruction as
follows:
MOV EAX, 01H
CPUID
After execution of the CPUID instruction, the ECX and EDX register contains the Feature
Flags. If the PSN Feature Flags, (EDX register, bit 18) equals “1”, the processor serial
number feature is supported, and enabled. If the PSN Feature Flags equals “0”, the
processor serial number feature is either not supported, or disabled in a
Pentium III processor.
我的机子是cpu是奔腾双核T2390,环境Ubuntu 9.04+QT 4,CPU序列号无法取出来,快疯了,请高手帮忙看看,不胜感激。
...全文
247 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ankoe 2009-06-07
  • 打赏
  • 举报
回复
郁闷,用sudo dmidecode查看看后发现不支持序列号。

Handle 0x0004, DMI type 4, 35 bytes
Processor Information
Socket Designation: U2E1
Type: Central Processor
Family: Other
Manufacturer: Intel
ID: FD 06 00 00 FF FB EB BF
Version: CPU Version
Voltage: 3.3 V
External Clock: 533 MHz
Max Speed: 1860 MHz
Current Speed: 1860 MHz
Status: Populated, Enabled
Upgrade: ZIF Socket
L1 Cache Handle: 0x0005
L2 Cache Handle: 0x0006
L3 Cache Handle: Not Provided
Serial Number: Not Specified
Asset Tag: Not Specified
Part Number: Not Specified
liubuweiright 2009-06-05
  • 打赏
  • 举报
回复
学习..
oyljerry 2009-06-05
  • 打赏
  • 举报
回复
自己增加调试信息,看前面有没有代码什么问题,然后看获取cpu的信息,如果没有信息,很有可能并不能取得cpu的信息,并不是所有的cpu都能让你获取
「已注销」 2009-06-05
  • 打赏
  • 举报
回复
和权限没关系?
这种问题一般涉及权限吧。
lylm 2009-06-05
  • 打赏
  • 举报
回复
加一些print,或者用gdb调试一下,看为啥跳过

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧