1

So I want to be able to get the full path of a running process (which I have the process ID for) without using any commands on the command line. Anyone has any ideas on how to do this?

I do have the PID, is there any function that by passing the PID can return the full path of that process as a char *?

4

2 回答 2

6

用于readlink("/proc/<pid>/exe", buf, bufsize)获取<pid>的可执行文件的路径。这适用于 Linux,只要procfs可用(通常是)。

示例用法:

int get_exe_for_pid(pid_t pid, char *buf, size_t bufsize) {
    char path[32];
    sprintf(path, "/proc/%d/exe", pid);
    return readlink(path, buf, bufsize);
}

返回-1失败并设置errno

于 2012-09-19T23:42:35.563 回答
0

男人 3 真实路径

这会扩展所有符号链接和目录相关标记。可悲的是,它是 GNU 特定的,我将它-std=gnu99printf("%s\n", realpath("/proc/self/exe", NULL)).

于 2013-11-06T17:45:59.720 回答