简体   繁体   English

以编程方式获取给定的进程名称和ppid

[英]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? 在Linux和其他POSIX(?)操作系统上-如何获取给定(通过pid)进程的名称和父pid? Currently I'm reading /proc but OSX doesn't have it. 目前,我正在读取/ proc,但OSX没有它。 On OSX I'm using libproc but that doesn't work on Linux: 在OSX上,我使用libproc,但在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. 如果只需要了解用户启动的应用程序,请查看NSWorkspace类的runningApplicationWithProcessIdentifier:方法。 Otherwise, you'll have to use a platform-specific sysctl as described here: 否则,您将必须使用特定于平台的sysctl,如下所述:

https://developer.apple.com/legacy/library/qa/qa2001/qa1123.html#//apple_ref/doc/uid/DTS10001671 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 以编程方式检查进程是否在Mac上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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