gpt4 book ai didi

macos - 如何获取 OS X 上进程的完整路径?

转载 作者:行者123 更新时间:2023-12-02 02:16:02 29 4
gpt4 key购买 nike

我知道我可以使用 ps 获取进程的 PID,但是如何找到该进程的完整路径?

最佳答案

OS X has the libproc library, which can be used to gather different process informations. In order to find the absolute path for a given PID, the following code can be used:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libproc.h>

int main (int argc, char* argv[])
{
pid_t pid; int ret;
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];

if ( argc > 1 ) {
pid = (pid_t) atoi(argv[1]);
ret = proc_pidpath (pid, pathbuf, sizeof(pathbuf));
if ( ret <= 0 ) {
fprintf(stderr, "PID %d: proc_pidpath ();\n", pid);
fprintf(stderr, " %s\n", strerror(errno));
} else {
printf("proc %d: %s\n", pid, pathbuf);
}
}

return 0;
}

编译和运行示例(上面的代码存储在pathfind.c中,32291是我试图查找路径信息的进程的pid):

$ cc pathfind.c -o pathfind
$ ./pathfind 32291
proc 32291: /some/path/your-binary-app

请参阅此博文: http://astojanov.wordpress.com/2011/11/16/mac-os-x-resolve-absolute-path-using-process-pid/

关于macos - 如何获取 OS X 上进程的完整路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14805896/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com