开发者

How to get running process with Objective-C

I have to develop a Cocoa program that will monitor a specific process. If that process goes under 50% of CPU usage, I have to send an email to 开发者_开发技巧the administrator. Of course, this will be a Mac OS X application.

Someone have tips or examples for me? I searched the web and I didn't find anything.


What you are asking is actually not at all straightforward.

As CajunLuke said, you'll want to take advantage of the POSIX underpinnings of the system.

The problem, though, is that you will need to take the average CPU use over some window of time and average that across the multiple samples, potentially tossing the samples that are outside of some standard deviation.

The issue is that a CPU use sample is necessarily an average over a period of time and, if that period of time is really short, then you'll see occasionally samples that are wildly low (or high).

Grab the source to the top command from the open source repositories (I believe top will be in the Darwin OSS releases) and have a look at that.


Since Objective-C is a superset of C, you can use whatever standard POSIX C method you'd use to do that on Linux or other UNIX.

A way (not a good way) would be to call out to ps or top and then process the result.

If you have a *nix app or script already that does this, it should work with just a recompile on OS X.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜