简体   繁体   中英

Get given process name and ppid programmatically

On Linux and other POSIX (?) operating systems - how to get name and parent pid of given (by pid) process? Currently I'm reading /proc but OSX doesn't have it. On OSX I'm using libproc but that doesn't work on Linux:

int ret = proc_pidpath (pid, pathbuf, sizeof(pathbuf));
if ( ret <= 0 ) {
    _dbg("   %s\n", strerror(errno));
    return false;
}

Unfortunately, when it comes to process info, every OS does things differently. If you only need to know about user-launched apps, look into the NSWorkspace class's runningApplicationWithProcessIdentifier: method. Otherwise, you'll have to use a platform-specific sysctl as described here:

https://developer.apple.com/legacy/library/qa/qa2001/qa1123.html#//apple_ref/doc/uid/DTS10001671

See also this stack overflow question:

Programmatically check if a process is running on Mac

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM