Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use below code. <code>getBSDProcessList</code> function will return <code>NSArray</code> of running process.<a href="https://stackoverflow.com/a/3772594/944634">[1]</a></p> <pre><code>static int GetBSDProcessList(kinfo_proc **procList, size_t *procCount) // Returns a list of all BSD processes on the system. This routine // allocates the list and puts it in *procList and a count of the // number of entries in *procCount. You are responsible for freeing // this list (use "free" from System framework). // On success, the function returns 0. // On error, the function returns a BSD errno value. { int err; kinfo_proc * result; bool done; static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; // Declaring name as const requires us to cast it when passing it to // sysctl because the prototype doesn't include the const modifier. size_t length; // assert( procList != NULL); // assert(*procList == NULL); // assert(procCount != NULL); *procCount = 0; // We start by calling sysctl with result == NULL and length == 0. // That will succeed, and set length to the appropriate length. // We then allocate a buffer of that size and call sysctl again // with that buffer. If that succeeds, we're done. If that fails // with ENOMEM, we have to throw away our buffer and loop. Note // that the loop causes use to call sysctl with NULL again; this // is necessary because the ENOMEM failure case sets length to // the amount of data returned, not the amount of data that // could have been returned. result = NULL; done = false; do { assert(result == NULL); // Call sysctl with a NULL buffer. length = 0; err = sysctl( (int *) name, (sizeof(name) / sizeof(*name)) - 1, NULL, &amp;length, NULL, 0); if (err == -1) { err = errno; } // Allocate an appropriately sized buffer based on the results // from the previous call. if (err == 0) { result = malloc(length); if (result == NULL) { err = ENOMEM; } } // Call sysctl again with the new buffer. If we get an ENOMEM // error, toss away our buffer and start again. if (err == 0) { err = sysctl( (int *) name, (sizeof(name) / sizeof(*name)) - 1, result, &amp;length, NULL, 0); if (err == -1) { err = errno; } if (err == 0) { done = true; } else if (err == ENOMEM) { assert(result != NULL); free(result); result = NULL; err = 0; } } } while (err == 0 &amp;&amp; ! done); // Clean up and establish post conditions. if (err != 0 &amp;&amp; result != NULL) { free(result); result = NULL; } *procList = result; if (err == 0) { *procCount = length / sizeof(kinfo_proc); } assert( (err == 0) == (*procList != NULL) ); return err; } + (NSArray*)getBSDProcessList { NSMutableArray *ret = [NSMutableArray arrayWithCapacity:1]; kinfo_proc *mylist; size_t mycount = 0; mylist = (kinfo_proc *)malloc(sizeof(kinfo_proc)); GetBSDProcessList(&amp;mylist, &amp;mycount); int k; for(k = 0; k &lt; mycount; k++) { kinfo_proc *proc = NULL; proc = &amp;mylist[k]; NSString *fullName = [[self infoForPID:proc-&gt;kp_proc.p_pid] objectForKey:(id)kCFBundleNameKey]; NSLog(@"fullName %@", fullName); if (fullName != nil) { [ret addObject:fullName]; } } free(mylist); return ret; } + (NSDictionary *)infoForPID:(pid_t)pid { NSDictionary *ret = nil; ProcessSerialNumber psn = { kNoProcess, kNoProcess }; if (GetProcessForPID(pid, &amp;psn) == noErr) { CFDictionaryRef cfDict = ProcessInformationCopyDictionary(&amp;psn,kProcessDictionaryIncludeAllInformationMask); ret = [NSDictionary dictionaryWithDictionary:(NSDictionary *)cfDict]; CFRelease(cfDict); } return ret; } </code></pre> <p>Take a look at <a href="http://developer.apple.com/legacy/mac/library/#qa/qa2001/qa1123.html" rel="nofollow noreferrer">Technical Q&amp;A QA1123</a> (Getting List of All Processes on Mac OS X)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload