以编程方式从Mac OS X获取处理器详细信息 [英] Programmatically get processor details from Mac OS X
问题描述
我在Mac OS X上运行的应用程序,需要检索有关其所运行的计算机的详细信息以报告系统信息.我需要的项目之一是有关计算机中安装的处理器的详细信息.
My application running on Mac OS X that needs to retrieve details about the machine it is running on to report for system information. One of the items I need is details about the processor(s) installed in the computer.
我的代码当前可以运行,但远非理想的解决方案,实际上我认为这是一个糟糕的解决方案,但是我没有运气来找到更好的解决方案.
My code currently works, but is far from an ideal solution, in fact I consider it a bad solution, but I have had no luck in finding a better one.
我当前报告的信息以及经过某种格式设置后的信息如下:
The information I report currently and after some formatting looks like:
处理器:Intel Core 2 Duo 2.1 GHz,Family 6 Model 23 Stepping 6
Processor: Intel Core 2 Duo 2.1 GHz, Family 6 Model 23 Stepping 6
我获得的所有信息都是通过从popen()调用的命令行实用程序获得的.处理器描述的可读部分来自"system_profiler"命令输出,而族",模型"和步进"值则来自"sysctl"命令.
All of the info I get is through command-line utilities called from a popen(). The readable part of the processor description is taken from the "system_profiler" command output and the Family, Model, and Stepping values are taken from the "sysctl" command.
这些命令行实用程序必须从某处获取信息. 我想知道是否有可用的编程界面来获取相同的信息?
These command-line utilities must be getting there information from somewhere. I'm wondering if there is an programmatic interface available to get this same info?
相关:
推荐答案
使用sysctlbyname
而不是sysctl
,例如
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/sysctl.h>
uint64_t get_cpu_freq(void)
{
uint64_t freq = 0;
size_t size = sizeof(freq);
if (sysctlbyname("hw.cpufrequency", &freq, &size, NULL, 0) < 0)
{
perror("sysctl");
}
return freq;
}
通过从命令行查看sysctl -a
的输出,可以获得可以传递给systctlbyname
的名称的列表.
You can get a list of the names that can be passed to systctlbyname
by looking at the output of sysctl -a
from the command line.
这篇关于以编程方式从Mac OS X获取处理器详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!