3

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

Serge
  • 2,031
  • 3
  • 33
  • 56

1 Answers1

5

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;
}
tenfour
  • 36,141
  • 15
  • 83
  • 142
  • 2
    Credit where credit is due: http://stackoverflow.com/questions/14162059/mac-osx-how-to-know-if-app-automatically-launched-at-session-startup-login – trojanfoe Feb 24 '14 at 11:50
  • 1
    This is from my a codebase of a project I'm working on where the source is cited as http://www.objectpark.net/parentpid.html but the site is dead so I didn't bother. *EDIT: the site isn't dead apparently. Whatever; source added. – tenfour Feb 24 '14 at 12:08