Reputation: 2129
I have PID of some process and need to get parent process id. How to get it using objective c?
Upvotes: 3
Views: 3128
Reputation: 36896
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;
}
Upvotes: 5