Reputation: 5066
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 it is.
Thanks!
Upvotes: 3
Views: 2470
Reputation: 64002
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.
Upvotes: 8