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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/220323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:57:04  来源:igfitidea点击:

Determine Process Info Programmatically in Darwin/OSX

c++cmacosoperating-systemdarwin

提问by anemitz

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; 

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.

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

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

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

采纳答案by diciu

Process info comes from pidinfo:

进程信息来自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 load comes from host_statistics:

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

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

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

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

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

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

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

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

回答by DGentry

Since you say no Objective-C we'll rule out most of the MacOS frameworks.

既然你说没有 Objective-C,我们将排除大多数 MacOS 框架。

You can get CPU time using getrusage(), which gives the total amount of User and System CPU time charged to your process. To get a CPU percentage you'd need to snapshot the getrusage values once per second (or however granular you want to be).

您可以使用 getrusage() 获取 CPU 时间,它给出了对您的进程收取的用户和系统 CPU 时间的总量。要获得 CPU 百分比,您需要每秒对 getrusage 值进行一次快照(或者您想要的粒度)。

#include <sys/resource.h>

struct rusage r_usage;

if (getrusage(RUSAGE_SELF, &r_usage)) {
    /* ... error handling ... */
}

printf("Total User CPU = %ld.%ld\n",
        r_usage.ru_utime.tv_sec,
        r_usage.ru_utime.tv_usec);
printf("Total System CPU = %ld.%ld\n",
        r_usage.ru_stime.tv_sec,
        r_usage.ru_stime.tv_usec);

There is an RSS field in the getrusage structure, but is appears to always be zero in MacOS X 10.5. Michael Knightwrote a blog post several years ago about how to determine the RSS.

getrusage 结构中有一个 RSS 字段,但在 MacOS X 10.5 中似乎始终为零。Michael Knight几年前写了一篇关于如何确定 RSS 的博客文章。

回答by hamed

You can use below code for process info in mac OS:

您可以在 mac OS 中使用以下代码获取进程信息:

void IsInBSDProcessList(char *name)    { 
  assert( name != NULL); 
  kinfo_proc *result; 
  size_t count = 0; 
  result = (kinfo_proc *)malloc(sizeof(kinfo_proc)); 
  if(GetBSDProcessList(&result,&count) == 0) { 
    for (int i = 0; i < count; i++) { 
      kinfo_proc *proc = NULL; 
      proc = &result[i]; 
      }
  } 
  free(result);
}

kinfo_proc struct contains all the information about a process.such as Process identifier (pid), process group, process status and etc.

kinfo_proc 结构体包含进程的所有信息,例如进程标识符(pid)、进程组、进程状态等。

回答by Mark Bessey

I think most of these values are available in the Mach API, but it's been a while since I've poked around in there. Alternatively, you could just look at the source code for the "ps" or "top" commands, and see how they do it.

我认为这些值中的大部分在 Mach API 中都可用,但我已经有一段时间没有在那里闲逛了。或者,您可以查看“ps”或“top”命令的源代码,看看它们是如何执行的。

回答by Menkboy

Most of this info can be gotten from GetProcessInformation().

大多数信息都可以从GetProcessInformation() 获得

By the way, why virtual methods for functions that return processwide info?

顺便说一句,为什么返回进程范围信息的函数的虚拟方法?

This is CARBONonly, and won't work with cocoa

这只是,不适用于可可