The way I achieve this is simply by iterating through every process identifier (to a certain extent). (This does require higher permissions though like sudo to get info on root processes)
#include <sys/proc_info.h>
#define SHOW_ZOMBIES 0
extern int proc_pidinfo(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize);
void iterateProcesses() {
pid_t maxPID = 100000;
for (pid_t cPID = 0; cPID < maxPID+1; cPID++) {
if (!(getpgid(cPID)>=0)) {continue;} // Skips if PID is invalid
// Now we can get some information
struct proc_taskallinfo info;
proc_pidinfo(cPID, PROC_PIDTASKALLINFO, SHOW_ZOMBIES, (user_addr_t)&info, sizeof(struct proc_taskallinfo));
char *proc_name = (char *)info.pbsd.pbi_name;
printf("Found a Process Called %s With a PID of %d\n",proc_name,cPID);
// Now if we want to do some more stuff with it we can get its task.
task_t task;
kern_return_t ret = task_for_pid(current_task(),cPID,&task);
if (ret != KERN_SUCCESS) {continue;} // Can't access task. skip
// Now we have the task. We can do some stuff with it...
task_suspend(task);
}
}
Anyways I hope this helps someone. ;)