简体   繁体   English

如何在Mac OS X 10.6中确定内核位数?

[英]How to determine kernel bitness in Mac OS X 10.6?

I know that I can use the terminal and the system-profiler command to determine the current bitness of the kernel but I am trying to determine if there is a way to get that same information programmatically in objective-c. 我知道我可以使用终端和system-profiler命令来确定内核的当前位数,但我正在尝试确定是否有办法在objective-c中以编程方式获取相同的信息。

I have looked through Carbon's gestalt, but haven't seen anything that would tell me bitness of the kernel. 我查看了Carbon的格式塔,但没有看到任何可以告诉我内核的位数的东西。

Does anyone have any suggestions as to how I could do this? 有没有人对我如何做到这一点有任何建议? I need this info for a debugging report that gives a snapshot of the system at the time of the report. 我需要此信息用于调试报告,该报告在报告时提供系统的快照。

Thanks! 谢谢!

Update: One thing I have tried that is a trick I learned from the Windows world is to check the size of an int like: 更新:我尝试过的一件事是我从Windows世界中学到的一个技巧是检查int的大小,如:

sizeof(int*); //(4 = x86 8 = x64)

but I don't think this is a resolution because I think this will only give me an idea of what the actual program itself is running at and not the actual OS kernel. 但我不认为这是一个解决方案,因为我认为这只会让我了解实际程序本身运行的是什么,而不是实际的操作系统内核。 My understanding is that even though the OS kernel is running at 32 bit your program can still run at 64bit. 我的理解是,即使操作系统内核以32位运行,您的程序仍然可以在64位运行。

I have run across other forum posting similar to this one but none of them seem to come up with an answer other then using system_profiler. 我遇到了与此类似的其他论坛帖子,但似乎没有人在使用system_profiler之后得到答案。

See man 3 uname : It fills a utsname structure which includes a member machine , which is "x86_64" or "i386" on Intel platforms: 请参阅man 3 uname :它填充了一个utsname结构,其中包含一个成员machine ,在Intel平台上为"x86_64""i386"

struct utsname un;
int res = uname(&un);
if (res >= 0) {
    NSLog(@"%s", un.machine);
}

You could use sysctlbyname. 您可以使用sysctlbyname。 Dig around mach/machine.h to get possible values. 挖掘mach/machine.h以获得可能的值。

#include <mach/machine.h>
#include <sys/sysctl.h>

void example() 
{
   unsigned int cpuType;   
   size_t size = sizeof(cpuType);
   sysctlbyname("hw.cputype", &cpuType, &size, NULL, 0);

   bool is64 = cpuType & CPU_ARCH_ABI64;

   const char *cpu;

   switch (cpuType) {
      case CPU_TYPE_X86:
         cpu = "x86";
         break;
      case CPU_TYPE_X86_64:
         cpu = "x86_64";
         break;
      case CPU_TYPE_POWERPC:
         cpu = "ppc";
         break;
      case CPU_TYPE_POWERPC64:
         cpu = "ppc_64";
         break;
      case CPU_TYPE_SPARC:
         cpu = "sparc";
         break;
      default:
         cpu = "unknown";
         break;
   }
}

You should be able to read the system profiler information in from command line like this: 您应该能够从命令行中读取系统探查器信息,如下所示:

sys_profile = popen("system_profiler -xml", "r");

See the ProfileSystem example in Apple's documentation for how to parse it. 请参阅Apple文档中的ProfileSystem示例,了解如何解析它。

Software/System Software Overview/64-bit Kernel and Extensions is probably the key you want. 软件/系统软件概述/ 64位内核和扩展可能是您想要的关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM