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!
Well,
-[NSWorkspace runningApplications]will get you the list of user processes, represented byNSRunningApplicationinstances. AnNSRunningApplicationhas aprocessIdentifierproperty, holding thepid_tof its process, which you could then pass tops— this might be more efficient.Also take a look at Apple’s sample project called “AppList”; it demonstrates using
NSWorkspaceandNSRunningApplication, although I can’t remember if it displays CPU usage.