⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.cpp

📁 一个能读取cpu个数和ID的代码
💻 CPP
字号:
// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
//测试CPU核心个数
#if !defined (_WIN32) && !defined (_WIN64)
#define LINUX
#include <sysconf.h>
#else
#define WINDOWS
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string>
#include <iostream>
using namespace std;
#endif

unsigned core_count()
{
  unsigned count = 1; // 至少一个
  #if defined (LINUX)
  count = sysconf(_SC_NPROCESSORS_CONF);
  #elif defined (WINDOWS)
  SYSTEM_INFO si;
  GetSystemInfo(&si);
  count = si.dwNumberOfProcessors;
  
  #endif
  return count;
}
void GetCpuID(char *cpuinfo, int count)
{
	while(count)
	{
	unsigned long s1,s2;
	char vendor_id[]="------------";
	char cpusec[40];
	char cpuid[80];
	char *p;
	_asm
	{
		xor eax,eax
		cpuid
		mov dword ptr vendor_id,ebx
		mov dword ptr vendor_id[+4],edx
		mov dword ptr vendor_id[+8],ecx
	}
		//printf("%s-",vendor_id);
		_asm
		{
		mov eax,01h
		xor edx,edx
		cpuid
		mov s1,edx
		mov s2,eax
		}
		sprintf(cpusec,"%08X-%08X-",s1,s2);
		
		strcpy(cpuid,cpusec);
		_asm
		{
			mov eax,01h
			xor ecx,ecx
			xor edx,edx
			cpuid
			mov s1,ebx
			mov s2,ecx
		}
		sprintf(cpusec,"%08X-%08X\n",s1,s2);
		strcat(cpuid,cpusec);
		//printf("%s",cpuid);
		p = strstr(cpuinfo,cpuid);
		if(p)
		{
			continue;
		}
		else
		{
			strcat(cpuinfo,cpuid);
			count= count-1;
		}
	}
	
}


void main()
{
  unsigned sz = core_count();
  std::cout << sz << (1 == sz ? "core" : "cores") << '\n';
  char cpuinfo[256];
  memset(cpuinfo,0,256);
  GetCpuID(cpuinfo,sz);
  printf("%s",cpuinfo);
  
  getchar();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -