問題描述
我有一個具有以下成員函數的類:
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
有關更多詳細信息,請查看 top
和 lsof
的源代碼,它們是開源的(您需要注冊為 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 中以編程方式確定進程信息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!