Question

I have PID of some process and need to get parent process id. How to get it using objective c?

Was it helpful?

Solution

Original source: http://www.objectpark.net/parentpid.html

#include <sys/sysctl.h>

#define OPProcessValueUnknown UINT_MAX

int ProcessIDForParentOfProcessID(int pid)
{
    struct kinfo_proc info;
    size_t length = sizeof(struct kinfo_proc);
    int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
    if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
        return OPProcessValueUnknown;
    if (length == 0)
        return OPProcessValueUnknown;
    return info.kp_eproc.e_ppid;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top