在 Darwin/OSX 中以编程方式确定进程信息

Determine Process Info Programmatically in Darwin/OSX(在 Darwin/OSX 中以编程方式确定进程信息)
本文介绍了在 Darwin/OSX 中以编程方式确定进程信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个具有以下成员函数的类:

I have a class with the following member functions:


/// caller pid
virtual pid_t Pid() const = 0; 

/// physical memory size in KB
virtual uint64_t Size() const = 0;  

/// resident memory for this process
virtual uint64_t Rss() const = 0; 

/// cpu used by this process
virtual double PercentCpu() const = 0; 

/// memory used by this process
virtual double PercentMemory() const = 0; 

/// number of threads in this process
virtual int32_t Lwps() const = 0; 

这个类的职责是返回有关调用者的进程信息.物理内存大小可以很容易地通过 sysctl 调用来确定,而 pid 是微不足道的,但除了在 ps 或 top 上调用 popen 并解析输出之外,其余的调用都让我无法接受——这是不可接受的.任何帮助将不胜感激.

This class' duty is to return process information about caller. Physical memory size can easily determined by a sysctl call, and pid is trivial, but the remaining calls have eluded me, aside from invoking a popen on ps or top and parsing the output - which isn't acceptable. Any help would be greatly appreciated.

要求:
在 g++ 4.0 上编译
没有 obj-c
OSX 10.5

Requirements:
Compiles on g++ 4.0
No obj-c
OSX 10.5

推荐答案

进程信息来自pidinfo:

cristi:~ diciu$ grep proc_pidinfo /usr/include/libproc.h

int proc_pidinfo(int pid, int flavor, uint64_t arg,  void *buffer, int buffersize);

cpu 负载来自host_statistics:

cristi:~ diciu$ grep -r host_statistics /usr/include/

/usr/include/mach/host_info.h:/* host_statistics() */

/usr/include/mach/mach_host.defs:routine host_statistics(

/usr/include/mach/mach_host.h:/* Routine host_statistics */

/usr/include/mach/mach_host.h:kern_return_t host_statistics

有关更多详细信息,请查看 toplsof 的源代码,它们是开源的(您需要注册为 Apple 开发人员,但这是免费的):

For more details, check out sources for top and lsof, they are open source (you need to register as an Apple developer but that's free of charge):

https://opensource.apple.com/source/top/top-111.20.1/libtop.c.auto.html

后期所有这些接口都是特定于版本的,因此您在编写生产代码 (libproc.h) 时需要考虑到这一点:

Later edit: All these interfaces are version specific, so you need to take that into account when writing production code (libproc.h):

/*
 * This header file contains private interfaces to obtain process information.
 * These interfaces are subject to change in future releases.
 */

这篇关于在 Darwin/OSX 中以编程方式确定进程信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Returning a pointer of a local variable C++(返回局部变量 C++ 的指针)
Inline function linkage(内联函数联动)
Which is more efficient: Return a value vs. Pass by reference?(哪个更有效:返回值与通过引用传递?)
Why is std::function not equality comparable?(为什么 std::function 不具有可比性?)
C++ overload resolution(C++ 重载解析)
When to Overload the Comma Operator?(什么时候重载逗号运算符?)