开发者

Efficiently Getting A List of Processes & Names

Right now I'm doing something like this:

NSString *pullProcesses = @"ps axco pid,pcpu,user,command";
system([pullProcesses UTF8String]);
NSLog(pullProcesses);

It's a bit inefficient though, pulling about 15% CPU each time it's called. Is there any efficient way for me to get a list of processes and the amount of CPU it's using?

Furthermore, is there a way to break that list of processes down into ones that only belong to the user and are not system processes?

I'm hearing that NSTask may work, but this isn't checking all of the user's processes, only the ones active in the dock.

I'm also reading things about NSWorkSpace, but I'm not sure what the best way to do i开发者_开发知识库t is.

Thanks!


Well, -[NSWorkspace runningApplications] will get you the list of user processes, represented by NSRunningApplication instances. An NSRunningApplication has a processIdentifier property, holding the pid_t of its process, which you could then pass to ps -- this might be more efficient.

Also take a look at Apple's sample project called "AppList"; it demonstrates using NSWorkspace and NSRunningApplication, although I can't remember if it displays CPU usage.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜