从 C/C++ 获取 Mac OS X 上的进程示例程序
活动监视器中的示例进程功能非常有用。但是,我需要从另一个正在运行的进程(C/C++)或命令行对某个进程执行相同的操作(采样)。
有什么办法可以做到这一点吗?几天来我一直在谷歌上搜索这个,但没有任何运气。
The Sample Process feature in Activity Monitor is quite a useful thing. However, I need to do the same thing (take samples) of a certain process from another running process (C/C++) or a command line.
Is there any way to do this? I have been googling for this since a few days without any luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个命令行实用程序
示例
。示例:
它将获得与 Activity Monitor 完全相同的输出。
There is a command-line utility
sample
.Example:
It will get exactly the same output with Activity Monitor.
有一些命令行应用程序可以派上用场:
示例
和顶部
。如果你想编写自己的程序,可以使用
sysctl
系统调用< /a> 获取此类信息。然而,这是相当乏味的。我建议安装 procfs 文件系统(使用 MacFUSE 构建)。这将在
/proc
中创建一个新的“目录”,其中包含每个应用程序的许多有用信息(例如内存使用情况、CPU 使用情况、锁、打开的文件、套接字、线程等)。该网站提供了如何访问它的示例。然后您可以简单地编写对这些文件的访问权限的脚本。There are some few commandsline application that come in handy:
sample
andtop
.If you want to write your own program, you can use the
sysctl
system call to get such information. However, it's quite tedious.I would recommend installing procfs file system (built with MacFUSE). This would create a new "directory" at
/proc
that contains a lot of useful information for each application (e.g. memory usage, cpu usage, locks, opened files, sockets, threads, etc). The site gives a sample of how it can be accessed. Then you can simply script your access to those files.