If you're having trouble with the PID thing, this might help you get started:
Code:
#include <sys/param.h>
#include <sys/user.h>
#include <sys/sysctl.h>
struct kinfo_proc *procs = NULL;
int i, rt;
size_t size = 0;
int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
rt = sysctl(mib, 3, NULL, &size, NULL, 0);
procs = realloc(procs, size);
rt = sysctl(mib, 3, procs, &size, NULL, 0);
for (i = 0; i < (size / sizeof(struct kinfo_proc)); i++)
printf("%d %s\n", procs[i].kp_proc.p_pid, procs[i].kp_proc.p_comm);
free(procs);