Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 it is.

Thanks!

like image 557
aroooo Avatar asked Mar 22 '26 20:03

aroooo


1 Answers

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.

like image 127
jscs Avatar answered Mar 25 '26 11:03

jscs