使用C/C++/Java获取基本硬件信息
我正在尝试读取有关该设备(在 Solaris 计算机中)的一些基本信息,例如“否”。 CPU 数量,没有。 HDD 数量,没有。网络适配器等...
据我了解,我们无法直接从Java获取这些信息。 (如果我错了请纠正我)。
因此,解决方法是调用 JNI 调用。
现在,我如何用 C/C++ 编写这个代码? C 中是否有现有的系统调用/方法来获取这些信息?
我试图避免在 C 中使用 system() 方法,因为我想将信息存储为字符串。
我知道有 iostat、kstat、ifconfig 等 shell 命令...但是如何在我的 C/C++ 程序中模拟它们?
谢谢,
I am trying to read some basic information about the device (in Solaris machine) like no. of CPUs, no. of HDDs, no. of Network adapters etc ...
From what I understand, we cannot get these information directly from Java. (Correct me if I am wrong).
So, the workaround is by calling JNI calls.
Now, how do I code this in C/C++ ? Is there any existing system call/ method in C to get these information ?
I am trying to avoid using system() method in C because I want to store the information as a string.
I am aware that there are shell commands like iostat, kstat, ifconfig etc ... But how do I simulate them in my C/C++ program ?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
/proc 文件系统 有大量信息供您解析。 iostat 例如读取/proc/diskstats。那里还有更多内容,大多数 Linux 实用程序只是从这里读取信息。
如果您继续使用 Linux 或Solaris,您甚至不需要 JNI 来获取所有信息。
The /proc filesystem has lots of information for you to parse. iostat e.g. reads /proc/diskstats. There is much more there, and most of linuxs utilitiy programs just read the information from here.
You don't even need JNI to get all the info, at least, if you stay on linux or solaris.
对于 Java 解决方案,JNI 的替代方案是使用 Java 的
Runtime.exec()
执行系统命令。这是一个简单的示例:请注意,这有缺点:
For a Java solution, an alternative to JNI would be to use Java's
Runtime.exec()
to execute system commands. Here's a simple example:Note that this has disadvantages:
要在 JNI 中进行编码(绑定到 C),
一种标记为“本机”的方法。
javac
编译“.java”文件到“.class”文件
javah
工具提取C/C++ 兼容头文件来自
编译后的“.class”文件。
提取的所有方法
头文件,并将它们编译成
共享对象库(
.so
用于unix / linux,
.dll
用于 Windows)。静态加载共享对象
任何实例之前的库
创建的。
至于如何获得所需的数据,请参阅任意数量的 Windows 或 UNIX 系统编程参考。如果针对 Linux 平台,大多数工具都是开源的,因此您可以查看源代码来确定这些工具如何获取其数据(假设您可以忍受此类任务可能对您的代码施加的许可证限制) 。
To code this in JNI (bound to C)
one method marked as "native".
javac
to compile the ".java"file to a ".class" file
javah
tool to extract a C/ C++ compatible header file from
the compiled ".class" file.
all the methods in the extracted
header file, and compile them into a
shared object library (
.so
forunix / linux,
.dll
for windows).to statically load the shared object
library before any instances are
created.
As far as how you can get the desired data, refer to any number of systems programming references for Windows or UNIX. If targeting the Linux platform, most of the tools are open source, so you can look at the source code to determine how the tools obtained their data (assuming that you can live with the license restrictions that such a task might place on your code).